Maintenance Cron: admins see a heartbeat in the top bar when cron is not running, is switched off, or a job failed, is stuck or has fallen behind; the new Cron Watch job emails the admins or set addresses when a problem starts, once a day while it lasts and when it is fixed; health.php answers OK or 503 for an uptime monitor given its key in the address or an X-Health-Key header; the Alerts card sets the emails, sends a test and makes a new key; database 3.7.9 adds the cron alert settings; 26.11.9 changelog entries

This commit is contained in:
johnnyq
2026-11-17 18:43:26 -05:00
parent 4078fa5f25
commit 22f9315c81
14 changed files with 695 additions and 2 deletions

View File

@@ -509,6 +509,66 @@ $status_looks = [
</ul>
</div>
<?php if (cronHealthReady()) {
$alert_settings = cronHealthSettings();
$alert_to = (string) ($alert_settings['config_cron_alert_to'] ?? '');
$alert_admins = $alert_to === '' ? cronHealthRecipients('') : [];
$health_url = cronHealthUrl();
$watch_on = !empty($jobs['cron_watch']['enabled']); ?>
<!-- Alerts: the warning in the top bar, the Cron Watch job's emails and the health address (functions/cron_health.php) -->
<div class="card mb-3" data-cron-alerts>
<div class="card-header py-3">
<h3 class="card-title"><i class="fas fa-fw fa-bell me-2"></i>Alerts</h3>
</div>
<div class="card-body small">
<p class="text-body-secondary">
When cron stops, or a job fails, gets stuck or falls behind, admins see <i class="fas fa-heartbeat text-warning" aria-hidden="true"></i> in the top bar.
The Cron Watch job also emails about it - when it starts, once a day while it lasts, and when it's fixed.
</p>
<?php if (!$watch_on) { ?>
<p class="text-warning-emphasis" data-cron-watch-off><i class="fas fa-fw fa-exclamation-triangle me-1"></i>Cron Watch is off, so no alerts are emailed - turn it on in Jobs.</p>
<?php } elseif (!emailReady()) { ?>
<p class="text-warning-emphasis" data-cron-alerts-no-mail><i class="fas fa-fw fa-exclamation-triangle me-1"></i>No mail server is set up, so no alerts are emailed - <a href="settings_mail.php">Settings &gt; Mail</a>.</p>
<?php } ?>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $csrf ?>">
<div class="form-check form-switch mb-2">
<input class="form-check-input" type="checkbox" role="switch" id="cronAlertEmail" name="cron_alert_email" value="1" <?= intval($alert_settings['config_cron_alert_email'] ?? 1) === 1 ? 'checked' : '' ?>>
<label class="form-check-label" for="cronAlertEmail">Email alerts</label>
</div>
<label class="form-label" for="cronAlertTo">Send to</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-envelope"></i></span>
<input type="text" class="form-control" id="cronAlertTo" name="cron_alert_to" maxlength="500" placeholder="Every active admin" value="<?= escapeHtml($alert_to) ?>">
</div>
<div class="form-text">
Email addresses, with commas between. Left empty, they go to every active admin<?= $alert_admins ? ' - ' . escapeHtml(implode(', ', array_keys($alert_admins))) : '' ?>.
</div>
<div class="d-flex flex-wrap gap-2 mt-3">
<button type="submit" name="edit_cron_alerts" class="btn btn-sm btn-primary"><i class="fas fa-fw fa-check me-1"></i>Save</button>
<button type="submit" name="test_cron_alert" class="btn btn-sm btn-light border"><i class="fas fa-fw fa-paper-plane me-1"></i>Send a test</button>
</div>
</form>
<hr>
<h4 class="h6"><i class="fas fa-fw fa-satellite-dish me-2"></i>Uptime monitor</h4>
<p class="text-body-secondary">
An email can't say cron has stopped - it's cron that sends it. Point an uptime monitor (Uptime Kuma, UptimeRobot...) at this address every 5 minutes:
it answers <code>OK</code> while all is well, and an error (HTTP 503) when cron isn't running or a job is in trouble. Keep it private - it shows which jobs are failing.
</p>
<div class="input-group input-group-sm">
<span class="input-group-text"><i class="fas fa-fw fa-link"></i></span>
<input type="text" class="form-control font-monospace" value="<?= escapeHtml($health_url) ?>" readonly aria-label="Health address" data-cron-health-url>
<button type="button" class="btn btn-light border clipboardjs" title="Copy" data-clipboard-text="<?= escapeHtml($health_url) ?>"><i class="far fa-fw fa-copy"></i></button>
<a class="btn btn-light border text-danger confirm-link" href="post.php?renew_cron_health_key=1&amp;csrf_token=<?= $csrf ?>" title="Make a new address"
data-confirm-title="Make a new health address?" data-confirm-text="The old address stops working - give your uptime monitor the new one." data-confirm-button="Make a new one">
<i class="fas fa-fw fa-sync-alt"></i>
</a>
</div>
<div class="form-text">Or send the key in an <code>X-Health-Key</code> header to <code>/health.php</code>, which keeps it out of the web server's log.</div>
</div>
</div>
<?php } ?>
<?php if ($cron_heard) { // not running, the card at the top has the line ?>
<!-- The one crontab line everything on this page runs from -->
<div class="card mb-3">

View File

@@ -0,0 +1,40 @@
<?php
/*
* ITFlow - Database update to version 3.7.9 (from 3.7.8)
* Included by admin/database_updates.php - do not access directly
*
* Cron health (functions/cron_health.php): the alerts set on Maintenance >
* Cron, the key in the health address an uptime monitor checks, and what the
* Cron Watch job last emailed about.
* settings.config_cron_alert_email 1 = email the alerts (the default)
* settings.config_cron_alert_to addresses; empty = every active admin
* settings.config_cron_health_key the health address's key, made when first needed
* settings.config_cron_alert_state JSON - problems emailed, jobs waiting for a first run
*
* Re-runnable: each column is added only where it isn't there yet.
*/
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
$itflow_column = function ($table, $column) use ($mysqli) {
$result = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column' LIMIT 1");
return $result && mysqli_num_rows($result) > 0;
};
if (!$itflow_column('settings', 'config_cron_alert_email')) {
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_cron_alert_email` tinyint(1) NOT NULL DEFAULT 1 AFTER `config_cron_last_dispatch_at`");
}
if (!$itflow_column('settings', 'config_cron_alert_to')) {
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_cron_alert_to` varchar(500) DEFAULT NULL AFTER `config_cron_alert_email`");
}
if (!$itflow_column('settings', 'config_cron_health_key')) {
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_cron_health_key` varchar(200) DEFAULT NULL AFTER `config_cron_alert_to`");
}
if (!$itflow_column('settings', 'config_cron_alert_state')) {
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_cron_alert_state` text DEFAULT NULL AFTER `config_cron_health_key`");
}

View File

@@ -158,3 +158,104 @@ if (isset($_GET['clear_cron_error'])) {
redirect();
}
/*
* The Alerts card (functions/cron_health.php): whether the Cron Watch job emails, and who to,
* a test email to them, and a new key for the health address an uptime monitor checks.
*/
if (isset($_POST['edit_cron_alerts'])) {
validateCSRFToken();
if (!cronHealthReady()) {
flashAlert("Cron alerts need the database update - Maintenance > Update.", 'error');
redirect();
}
[$addresses, $not_addresses] = cronHealthAddresses($_POST['cron_alert_to'] ?? '');
if ($not_addresses) {
flashAlert("Not an email address: " . escapeHtml(implode(', ', $not_addresses)), 'error');
redirect();
}
$alert_email = isset($_POST['cron_alert_email']) ? 1 : 0;
$alert_to = escapeSql(substr(implode(', ', $addresses), 0, 500));
mysqli_query($mysqli, "UPDATE settings SET config_cron_alert_email = $alert_email, config_cron_alert_to = " . ($alert_to === '' ? 'NULL' : "'$alert_to'") . " WHERE company_id = 1");
logAudit("Cron", "Edit", "$session_name set cron alerts " . ($alert_email ? 'to email ' . ($alert_to === '' ? 'every active admin' : $alert_to) : 'not to email'));
flashAlert("Cron alerts saved.");
redirect();
}
if (isset($_POST['test_cron_alert'])) {
validateCSRFToken();
if (!cronHealthReady()) {
flashAlert("Cron alerts need the database update - Maintenance > Update.", 'error');
redirect();
}
if (!emailReady()) {
flashAlert("Set up outgoing mail first - Admin > Settings > Mail.", 'error');
redirect();
}
// To the addresses in the box, saved or not - so they can be tried before they're saved
[$addresses, $not_addresses] = cronHealthAddresses($_POST['cron_alert_to'] ?? '');
if ($not_addresses) {
flashAlert("Not an email address: " . escapeHtml(implode(', ', $not_addresses)), 'error');
redirect();
}
$recipients = cronHealthRecipients(implode(', ', $addresses));
if (!$recipients) {
flashAlert("Nobody to send to - add an address, or give an admin an email address.", 'error');
redirect();
}
cronHealthMail("ITFlow cron alerts: test", [
'tone' => 'primary', 'title' => 'Cron alerts: test',
'intro' => ['This is a test from Maintenance > Cron. When a cron job fails, gets stuck or falls behind, the alert comes to this address.'],
], implode(', ', $addresses));
logAudit("Cron", "Edit", "$session_name sent a test cron alert");
// Typed but not saved yet - say so, or the alerts still go where they did
$saved = array_map('strtolower', cronHealthAddresses(cronHealthSettings()['config_cron_alert_to'] ?? '')[0]);
$typed = array_map('strtolower', $addresses);
sort($saved);
sort($typed);
flashAlert("Test queued for " . escapeHtml(implode(', ', array_keys($recipients))) . " - it goes out with the mail queue, within a minute while cron runs." . ($saved !== $typed ? " Save for the alerts to go there too." : ''));
redirect();
}
if (isset($_GET['renew_cron_health_key'])) {
validateCSRFToken();
if (!cronHealthReady()) {
flashAlert("The health address needs the database update - Maintenance > Update.", 'error');
redirect();
}
cronHealthKey(true);
logAudit("Cron", "Edit", "$session_name made a new health address for cron");
flashAlert("New health address made - the old one no longer works, so give your uptime monitor the new one.");
redirect();
}