Cron leaves alone the jobs with nothing to do on an install: a job can declare not_in_use in includes/cron_jobs.php, the same check its script stops on, and cron.php skips it before its lock and claim unless it is asked to Run Now, registering its row the first time (cronJobRegister()); checks for AI Triage, Text Messages, RMM Sync, RMM Alerts, the Ticket Email Parser, Mail Queue, Payment Provider Fees, Update Check, Manager Check-in and Managed Instances; AI Triage no longer runs with triage off or no AI model (aiTriageNotInUse()); Maintenance > Cron shows them as Not in use with the reason, Cron Watch doesn't expect them to run, and CONTRIBUTING.md has the rule; 26.12.5 changelog

This commit is contained in:
johnnyq
2026-12-02 18:02:56 -05:00
parent bd38983abb
commit 9e74c0240a
8 changed files with 182 additions and 13 deletions

View File

@@ -74,7 +74,8 @@ while ($job_row = mysqli_fetch_assoc($sql)) {
* Each job the registry lists, with what its row says. How its last run went is one of:
* queued (Run now, not picked up yet), running, lost (still Running hours later - it was cut
* off, and nothing recorded how), completed, failed, skipped (it stopped itself early, and
* says why), paused (by a manager), never.
* says why), paused (by a manager), never - or unused: it has nothing to do here, its feature
* off or not set up, so cron leaves it alone until that changes (cronJobNotInUse()).
*/
$jobs = [];
foreach (cronJobRegistry() as $position => $entry) {
@@ -98,6 +99,7 @@ foreach (cronJobRegistry() as $position => $entry) {
'last_error' => trim((string) ($job_row['cron_job_last_error'] ?? '')),
'last_error_at' => $job_row['cron_job_last_error_at'] ?? null,
'reason' => '',
'not_in_use' => cronJobNotInUse($entry),
];
$job['schedule_text'] = cronJobScheduleDescription($job['schedule'], $job['interval_minutes'], $job['daily_at']);
$job['every_minute'] = $job['schedule'] !== 'Daily' && $job['interval_minutes'] === 1;
@@ -122,8 +124,14 @@ foreach (cronJobRegistry() as $position => $entry) {
}
}
// Nothing to do here - whatever its last run said, unless it's asked for or going now
if ($job['not_in_use'] !== '' && !in_array($job['state'], ['queued', 'running'], true)) {
$job['state'] = 'unused';
$job['reason'] = $job['not_in_use'];
}
// When it runs next - nothing to say while cron isn't running, is switched off, or holds this job back
$dispatched = $cron_heard && $cron_on && ($managed_mode === 'active' || in_array($job['name'], $managed_kept, true));
$dispatched = $cron_heard && $cron_on && $job['state'] !== 'unused' && ($managed_mode === 'active' || in_array($job['name'], $managed_kept, true));
$job['next_run'] = $job_row && $job['enabled'] && $dispatched ? cronJobNextRun($job_row) : null;
$jobs[$job['name']] = $job;
@@ -131,6 +139,8 @@ foreach (cronJobRegistry() as $position => $entry) {
$jobs_on = array_filter($jobs, function ($job) { return $job['enabled']; });
$jobs_off = count($jobs) - count($jobs_on);
// On, with nothing to do here yet
$jobs_unused = count(array_filter($jobs_on, function ($job) { return $job['state'] === 'unused'; }));
// Failed on its last run - with an error, or cut off without a word
$jobs_failed = array_filter($jobs, function ($job) { return in_array($job['state'], ['failed', 'lost'], true); });
// Failed before and has run since - its error stays until somebody dismisses it
@@ -223,7 +233,7 @@ $sort_keys = [
'schedule' => function ($job) { return $job['schedule'] === 'Daily' ? 100000 + intval(substr($job['daily_at'], 0, 2)) * 60 + intval(substr($job['daily_at'], 3, 2)) : $job['interval_minutes']; },
'last_run' => function ($job) { return $job['last_run_at'] ? strtotime($job['last_run_at']) : 0; },
// What needs a look first
'status' => function ($job) { return array_search($job['state'], ['failed', 'lost', 'queued', 'running', 'paused', 'skipped', 'never', 'completed'], true); },
'status' => function ($job) { return array_search($job['state'], ['failed', 'lost', 'queued', 'running', 'paused', 'skipped', 'never', 'completed', 'unused'], true); },
];
if (!isset($sort_keys[$sort])) {
$sort = 'schedule';
@@ -247,6 +257,7 @@ $status_looks = [
'skipped' => ['label' => 'Skipped', 'tone' => 'secondary'],
'paused' => ['label' => 'Paused', 'tone' => 'warning'],
'never' => ['label' => 'Not run yet', 'tone' => 'secondary'],
'unused' => ['label' => 'Not in use', 'tone' => 'secondary', 'title' => "It has nothing to do here, so cron doesn't run it - it starts by itself once that changes. Run now still works."],
];
?>
@@ -299,7 +310,7 @@ $status_looks = [
<?php } else { ?>
<p class="mb-0 text-body-secondary">
It last ran <?= $ago($cron_last_dispatch_at) ?>. <?= count($jobs_on) ?> job<?= count($jobs_on) === 1 ? ' is' : 's are' ?> on<?= $jobs_off ? ", $jobs_off off" : '' ?>, and none failed on its last run.
It last ran <?= $ago($cron_last_dispatch_at) ?>. <?= count($jobs_on) ?> job<?= count($jobs_on) === 1 ? ' is' : 's are' ?> on<?= $jobs_unused ? " ($jobs_unused with nothing to do here yet)" : '' ?><?= $jobs_off ? ", $jobs_off off" : '' ?>, and none failed on its last run.
<?php if ($next_job) { ?>
Next is <?= $job_names([$next_job]) ?>, <?= $next_job['next_run'] <= date('Y-m-d H:i:s') ? 'within a minute' : $ago($next_job['next_run']) ?>.
<?php } ?>
@@ -352,7 +363,7 @@ $status_looks = [
<?php foreach ($jobs as $job) {
$look = $status_looks[$job['state']];
$failing = in_array($job['state'], ['failed', 'lost'], true);
$icon_tone = $failing ? 'bg-danger-subtle text-danger-emphasis' : ($job['enabled'] ? 'itflow-asset-tone-peripheral' : 'itflow-asset-tone-other opacity-50');
$icon_tone = $failing ? 'bg-danger-subtle text-danger-emphasis' : ($job['enabled'] && $job['state'] !== 'unused' ? 'itflow-asset-tone-peripheral' : 'itflow-asset-tone-other opacity-50');
// Why a skipped run was skipped - but not the switch, twelve times over, while the top of the page is saying it
$reason = $job['reason'] === 'Cron is switched off' && !$cron_on ? '' : $job['reason'];
?>
@@ -384,7 +395,7 @@ $status_looks = [
<?php } ?>
</td>
<td class="border-bottom-0 pt-3 pb-1">
<span class="badge rounded-pill bg-<?= $look['tone'] ?>-subtle text-<?= $look['tone'] ?>-emphasis"><?php if ($job['state'] === 'running') { ?><i class="fas fa-circle-notch fa-spin me-1"></i><?php } ?><?= escapeHtml($look['label']) ?></span>
<span class="badge rounded-pill bg-<?= $look['tone'] ?>-subtle text-<?= $look['tone'] ?>-emphasis"<?= isset($look['title']) ? ' title="' . escapeHtml($look['title']) . '"' : '' ?>><?php if ($job['state'] === 'running') { ?><i class="fas fa-circle-notch fa-spin me-1"></i><?php } ?><?= escapeHtml($look['label']) ?></span>
</td>
<td class="border-bottom-0 pt-3 pb-1 text-end pe-3">
<?php if ($job['id']) { ?>