diff --git a/CHANGELOG.md b/CHANGELOG.md index ec1c6146e..18974b64a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,12 @@ Update from Maintenance > Update — Queue Update hands the job to cron and it a - Backups: the list of backups works like every other list - it sorts by its headings and has pages - and says more about each one: who made it or that the schedule did, when it was last downloaded, and why one failed. A backup's menu copies its path on the server, which is what restoring from the command line asks for, and its SHA-256 checksum, for checking a copy kept somewhere else. A backup still waiting to be made can be taken off the queue. - Backups: the nightly backup is on the page. It shows whether the schedule is on and at what time, with Turn on and Change the time beside it, so there is no trip to Maintenance > Cron. Beside the page, At a glance has the last Full Backup and the last Database Only, how many backups are kept and the room they take, the free space left where they are kept, and whether cron is running. - Backups: the encryption key is hidden until you click the eye, with a button to copy it; it used to sit on the page in full for anyone looking at the screen. Exporting the master key opens a window that asks for your password and says what will happen before it does. +- Cron: Maintenance > Cron is rebuilt so that its first line answers what you open it for - is cron running? The top of the page says when it last ran, how many jobs are on and which runs next. When something needs you, it says that instead: cron has never run on this server or has stopped, with the line to add to the server's crontab and a button that copies it; cron is switched off, with Turn cron on; or a job failed, by name. +- Cron: each job says what it does, its schedule and when it next runs, when it last ran and how long that took, and how it went. A job that failed says why under its name, and goes on saying so until you dismiss it - with a note once it has run since, so an old failure doesn't read as a new one. A job that skipped its run says why too: texting isn't set up, there is no Stripe provider, nothing was queued. +- Cron: the page keeps itself up to date. After Run now a job shows as queued, then running, then how it went, without a reload. And on a new server, once the cron line is in, the page says cron is running by itself. +- Cron: a job's menu has Run now, Change the schedule, Turn off - which asks first - and Copy the command to run it, which copies the command that runs that one job on the server, where it shows what it does as it goes. The list sorts by its headings, and starts in the order the day runs: the every-minute jobs, then the nightly ones by time. +- Cron: beside the page, At a glance has whether cron is running, when it last ran, how many jobs are on and off, any that failed, what runs next, and the time zone the schedules are in. Turn cron off, which stops every job at once for a restored backup or a test copy, asks first and says what stops. +- Update, Backup and Cron: a page that is keeping itself up to date no longer reloads under you. It waits while a window or a menu is open, while you are typing in a field, and while you have text selected to copy. ### Bug Fixes @@ -39,6 +45,14 @@ Update from Maintenance > Update — Queue Update hands the job to cron and it a - The master key window is an ajax modal, `admin/modals/backup/backup_master_key.php`, in place of markup at the foot of the page. The encryption key field uses `data-toggle="password"` and the copy buttons `clipboardjs`, both already in `js/app.js`. - `delete_backup` (`admin/post/backup.php`) reads the row's status so that the audit log and the message say what happened: a `Pending` row is "took a queued Full Backup off the queue", a `Complete` one "Backup deleted.", and any other "Backup removed from the list." - Hooks for tests and scripts: `data-backup-state` on the hero, `data-backup-list` with `data-backup-id` and `data-backup-status` on each row, `data-backup-schedule` (`on` or `off`), `data-backup-glance`, `data-backup-folder`, `data-backup-key`, `data-backup-now`, `data-backup-cron-warning` and `data-backup-master-key`. +- `admin/cron.php` is rewritten on the pattern of `admin/update.php` and `admin/backup.php`: one `$state` - `never`, `stopped`, `off`, `paused`, `failed` or `running`, tried in that order - picks the hero card's icon, tone and title from `$hero`. `never` and `stopped` are no heartbeat (`config_cron_last_dispatch_at`) at all, and none in five minutes; `off` is `config_enable_cron`; `paused` is a managed instance its manager has made read-only (`managedMode()`, `MANAGED_KEEP_JOBS`); `failed` is any job whose last run is `Failed`, or still `Running` six hours on. The card carries `data-itflow-reload-seconds`: 10 while a job is queued or started in the last half hour, 15 while cron hasn't been heard from. No CSS is added. +- The jobs are the registry's (`cronJobRegistry()`), each with its `cron_jobs` row, sorted in PHP on the standard `sort` and `order` - `job`, `schedule`, `last_run` or `status`, anything else falling back to `schedule`. There are no pages and no search on purpose: it is a fixed list of at most fifteen, and a job that failed mustn't be on page two. Each job is a `` of two rows, so its description, why it was skipped and its error run the width of the table. A job the dispatcher hasn't met has no row and no menu. +- How the page reads `cron_job_last_status`: `Completed` and `Failed` as they are; `Stopped: ` as Skipped with the reason under the job, less a trailing `-- Quitting..`, and "Cron: is not enabled" as "Cron is switched off"; `Stopped: Paused - ` as Paused; `Stopped` alone as Skipped, pointing at App Logs; `cron_job_run_now` as Queued. `cron_job_last_error` is shown until dismissed (`clear_cron_error`), marked as having run since when the last status isn't a failure. +- `includes/cron_jobs.php`: each job gains an `icon` (a Font Awesome class, a cog without one), and `cronJobTimeAgo()` is gone - the page uses `timeAgo()`, as the Update and Backup pages do. `CONTRIBUTING.md` says to give a new job its icon. +- `admin/modals/cron/cron_edit.php` opens with what the job does, uses the select picker for Schedule (its inline script still follows the picker's `change`), and names the time zone a daily time is in. It is the same window Maintenance > Backup opens for Change the time. +- `admin/post/cron.php`: the messages follow the buttons - "Cron turned off.", "Mail Queue turned on." - where they said "Cron disabled." and "Cron job enabled."; `enable_cron_job` and `disable_cron_job` load the registry for the job's label. The audit log's wording is unchanged. +- `js/auto_reload.js` holds its reload while `.modal.show`, `.dropdown-menu.show` or `.swal2-container` is on the page, while an `input`, `textarea` or `select` has focus, or while there is a selection, and looks again every two seconds. Before, the Backup page could reload away the Change the time window, or what was being typed into its retention boxes, while a backup was being made. +- Hooks for tests and scripts: `data-cron-state` on the hero, `data-cron-jobs` with `data-cron-job`, `data-cron-status` and `data-cron-enabled` on each job's ``, `data-cron-run`, `data-cron-edit`, `data-cron-off` and `data-cron-on` in its menu, `data-cron-reason`, `data-cron-error` and `data-cron-dismiss` under it, `data-cron-glance`, `data-cron-command`, `data-cron-switch` (`on` or `off`), `data-cron-busy` and `data-cron-recovered`. ## [26.11.7] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3d208bf07..a6ab886f7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -89,7 +89,7 @@ One crontab entry runs everything: * * * * * php /path/to/itflow/cron/cron.php >/dev/null ``` -`cron/cron.php` is a dispatcher. It wakes every minute, works out which scripts in `cron/` are due, and requires them into its own process. Adding a job is a new script in `cron/` plus an entry in `includes/cron_jobs.php`. The crontab never changes again. +`cron/cron.php` is a dispatcher. It wakes every minute, works out which scripts in `cron/` are due, and requires them into its own process. Adding a job is a new script in `cron/` plus an entry in `includes/cron_jobs.php` - with a `label`, a `description` written for the admin reading Maintenance > Cron, and an `icon` for its row there. The crontab never changes again. That registry is the only thing that decides **which** scripts can run, and the schedule in it is only a default: it seeds the job's `cron_jobs` row the first time the dispatcher meets the job, and from then on the row is what runs, because Maintenance > Cron writes to it. The database therefore holds **when and whether**, never **what** — a row naming a script that is not in the registry is ignored, so nothing that reaches the database can point the dispatcher at an arbitrary file. Keep it that way. diff --git a/admin/cron.php b/admin/cron.php index 815696215..fe575a31a 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -1,208 +1,548 @@ Cron. One answer at the top - cron is running, it has stopped or never + * started, it is switched off, a job failed - then every job: what it does, its schedule, + * how its last run went and when the next is. Beside them: cron at a glance, the one + * crontab line all of it runs from, and the switch that stops every job at once. + * + * Nothing runs here. Run now sets a flag the dispatcher (cron/cron.php) takes on its next + * pass, so while a job is queued or running the page reloads itself (js/auto_reload.js), as + * Maintenance > Update and Backup do - and while cron hasn't been heard from, so the page + * says so by itself once the crontab line is in. The handlers are admin/post/cron.php. + * + * The jobs are the registry's (includes/cron_jobs.php), each with its row from cron_jobs. A + * job the dispatcher has not met yet has no row: it shows the schedule it ships with, and + * there is nothing to change about it until cron has run once. + */ + +// The list: by schedule until a heading is clicked - the every-minute jobs, then the day's in the order they run +$sort = "schedule"; +$order = "ASC"; + require_once "includes/inc_all_admin.php"; require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/cron_jobs.php'; +$csrf = $_SESSION['csrf_token']; + $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT config_enable_cron, config_cron_last_dispatch_at FROM settings WHERE company_id = 1")); -$config_enable_cron = intval($row['config_enable_cron']); +$cron_on = intval($row['config_enable_cron']) === 1; $cron_last_dispatch_at = $row['config_cron_last_dispatch_at']; -// The dispatcher writes its heartbeat before it runs anything, so anything older than a few +// The dispatcher writes its heartbeat before it runs anything, so one older than a few // minutes means the crontab entry itself is missing or failing - a different problem from a -// job that is disabled or erroring, and the one people spend the longest not finding. -$cron_is_running = $cron_last_dispatch_at !== null && (time() - strtotime($cron_last_dispatch_at)) < 300; +// job that is off or erroring, and the one people spend the longest not finding. +$cron_heard = !empty($cron_last_dispatch_at) && (time() - strtotime($cron_last_dispatch_at)) < 300; -$cron_command = "* * * * * php " . dirname(__DIR__) . "/cron/cron.php >/dev/null"; +// A managed instance its manager has made read-only or suspended runs only the jobs that keep it looked after +$managed_mode = managedMode(); +$managed_kept = $managed_mode === 'active' ? [] : (MANAGED_KEEP_JOBS[$managed_mode] ?? []); -// Registry order is dispatch order, so the table reads the way the cycle runs -$cron_jobs = []; -foreach (cronJobRegistry() as $job) { - $cron_jobs[$job['name']] = $job; - $cron_jobs[$job['name']]['row'] = null; +// The crontab line, and a job run by hand, as this server runs them - in this folder, as the user that owns the files +$install_root = dirname(__DIR__); +$file_owner = ''; +if (function_exists('posix_getpwuid')) { + $file_owner = (string) (posix_getpwuid((int) @fileowner($install_root . '/cron/cron.php'))['name'] ?? ''); } +$shell_word = function ($word) { + return preg_match('/^[A-Za-z0-9_\/.@%+=:,-]+$/', $word) ? $word : escapeshellarg($word); +}; +$cron_command = "* * * * * php $install_root/cron/cron.php >/dev/null"; +$by_hand = function ($script) use ($install_root, $file_owner, $shell_word) { + return "cd " . $shell_word($install_root) . " && " . ($file_owner !== '' ? "sudo -u " . $shell_word($file_owner) . " " : '') . "php cron/" . $shell_word($script); +}; +// What a job said as it stopped early, as it reads on the page +$tidy_reason = function ($reason) { + $reason = preg_replace('/\s*--\s*Quitting\.*\s*$/i', '', trim(ltrim((string) $reason, ': '))); + if (stripos($reason, 'Cron: is not enabled') === 0) { + return 'Cron is switched off'; + } + return rtrim($reason, ". \n\r\t"); +}; + +$job_rows = []; $sql = mysqli_query($mysqli, "SELECT cron_job_daily_at, cron_job_enabled, cron_job_id, cron_job_interval_minutes, cron_job_last_duration, cron_job_last_error, cron_job_last_error_at, cron_job_last_run_at, cron_job_last_status, cron_job_name, cron_job_run_now, cron_job_schedule FROM cron_jobs"); while ($job_row = mysqli_fetch_assoc($sql)) { - if (isset($cron_jobs[$job_row['cron_job_name']])) { - $cron_jobs[$job_row['cron_job_name']]['row'] = $job_row; + $job_rows[$job_row['cron_job_name']] = $job_row; +} + +/* + * 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. + */ +$jobs = []; +foreach (cronJobRegistry() as $position => $entry) { + + $job_row = $job_rows[$entry['name']] ?? null; + + $job = [ + 'name' => $entry['name'], + 'label' => $entry['label'], + 'description' => $entry['description'], + 'script' => $entry['script'], + 'icon' => $entry['icon'] ?? 'fa-cog', + 'position' => $position, + 'id' => intval($job_row['cron_job_id'] ?? 0), + 'enabled' => $job_row ? intval($job_row['cron_job_enabled']) === 1 : intval($entry['enabled'] ?? 1) === 1, + 'schedule' => $job_row ? (string) $job_row['cron_job_schedule'] : (string) $entry['schedule'], + 'interval_minutes' => max(1, intval($job_row ? $job_row['cron_job_interval_minutes'] : ($entry['interval_minutes'] ?? 1))), + 'daily_at' => substr((string) ($job_row ? $job_row['cron_job_daily_at'] : ($entry['daily_at'] ?? '')), 0, 5), + 'last_run_at' => $job_row['cron_job_last_run_at'] ?? null, + 'last_duration' => $job_row['cron_job_last_duration'] ?? null, + 'last_error' => trim((string) ($job_row['cron_job_last_error'] ?? '')), + 'last_error_at' => $job_row['cron_job_last_error_at'] ?? null, + 'reason' => '', + ]; + $job['schedule_text'] = cronJobScheduleDescription($job['schedule'], $job['interval_minutes'], $job['daily_at']); + $job['every_minute'] = $job['schedule'] !== 'Daily' && $job['interval_minutes'] === 1; + + $status = (string) ($job_row['cron_job_last_status'] ?? ''); + if (!empty($job_row['cron_job_run_now'])) { + $job['state'] = 'queued'; + } elseif ($status === 'Running') { + $job['state'] = strtotime((string) $job['last_run_at']) > strtotime('-6 hours') ? 'running' : 'lost'; + } elseif ($status === 'Completed') { + $job['state'] = 'completed'; + } elseif ($status === 'Failed') { + $job['state'] = 'failed'; + } elseif ($status === '') { + $job['state'] = 'never'; + } else { + $job['reason'] = $tidy_reason(str_starts_with($status, 'Stopped') ? substr($status, 7) : $status); + $job['state'] = 'skipped'; + if (str_starts_with($job['reason'], 'Paused')) { + $job['state'] = 'paused'; + $job['reason'] = ucfirst(ltrim(substr($job['reason'], 6), ' -')); + } + } + + // 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)); + $job['next_run'] = $job_row && $job['enabled'] && $dispatched ? cronJobNextRun($job_row) : null; + + $jobs[$job['name']] = $job; +} + +$jobs_on = array_filter($jobs, function ($job) { return $job['enabled']; }); +$jobs_off = count($jobs) - count($jobs_on); +// 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 +$jobs_recovered = array_filter($jobs, function ($job) { return $job['last_error'] !== '' && !in_array($job['state'], ['failed', 'lost'], true); }); +// On their way: asked for, or started in the last half hour +$jobs_busy = array_filter($jobs, function ($job) { + return $job['state'] === 'queued' || ($job['state'] === 'running' && strtotime((string) $job['last_run_at']) > strtotime('-30 minutes')); +}); + +// The next job that isn't one of the every-minute ones +$next_job = null; +foreach ($jobs as $job) { + if ($job['next_run'] !== null && !$job['every_minute'] && (!$next_job || $job['next_run'] < $next_job['next_run'])) { + $next_job = $job; } } +/* + * The one thing to say at the top. In this order, because each makes the ones after it moot: + * cron not running at all, running but switched off, most of it held back by a manager, a + * job that failed, then all well. + */ +if (!$cron_heard) { + $state = empty($cron_last_dispatch_at) ? 'never' : 'stopped'; +} elseif (!$cron_on) { + $state = 'off'; +} elseif ($managed_mode !== 'active') { + $state = 'paused'; +} elseif ($jobs_failed) { + $state = 'failed'; +} else { + $state = 'running'; +} + +$failed_count = count($jobs_failed); +$hero = [ + 'never' => ['icon' => 'fa-plug', 'tone' => 'warning', 'title' => "Cron hasn't run yet"], + 'stopped' => ['icon' => 'fa-exclamation-triangle', 'tone' => 'danger', 'title' => 'Cron has stopped'], + 'off' => ['icon' => 'fa-power-off', 'tone' => 'warning', 'title' => 'Cron is switched off'], + 'paused' => ['icon' => 'fa-pause', 'tone' => 'warning', 'title' => 'Most jobs are paused'], + 'failed' => ['icon' => 'fa-exclamation-triangle', 'tone' => 'danger', 'title' => $failed_count === 1 ? reset($jobs_failed)['label'] . ' failed' : "$failed_count jobs failed"], + 'running' => ['icon' => 'fa-check', 'tone' => 'success', 'title' => 'Cron is running'], +][$state]; + +// Reload while something is on its way, and while waiting to hear from cron at all +$reload_seconds = !$cron_heard ? 15 : ($jobs_busy ? 10 : 0); + +// A time as "3 hours ago", with the date and time behind it +$ago = function ($datetime) { + return '' . escapeHtml(timeAgo($datetime)) . ''; +}; +// How long a run took +$took = function ($seconds) { + $seconds = floatval($seconds); + if ($seconds < 0.1) { + return 'under 0.1 s'; + } + if ($seconds < 10) { + return rtrim(rtrim(number_format($seconds, 1), '0'), '.') . ' s'; + } + if ($seconds < 60) { + return round($seconds) . ' s'; + } + $minutes = intdiv((int) round($seconds), 60); + if ($minutes < 60) { + return $minutes . ' min ' . ((int) round($seconds) % 60) . ' s'; + } + return intdiv($minutes, 60) . ' h ' . ($minutes % 60) . ' min'; +}; +// Job names in a sentence - "Mail Queue", "Mail Queue and Backup", "Mail Queue, Backup and Nightly Tasks" - each a link to its row +$job_names = function ($list) { + $links = array_map(function ($job) { + return '' . escapeHtml($job['label']) . ''; + }, array_values($list)); + $last = array_pop($links); + return $links ? implode(', ', $links) . ' and ' . $last : (string) $last; +}; +// The crontab line, with a button that copies it +$command_block = function () use ($cron_command) { + return '
' + . '
' . escapeHtml($cron_command) . '
' + . '' + . '
'; +}; +$owner_note = $file_owner !== '' ? ' (' . escapeHtml($file_owner) . ' here)' : ''; + +// The list - only these sort; anything else asked for falls back to the schedule +$sort_keys = [ + 'job' => function ($job) { return strtolower($job['label']); }, + '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); }, +]; +if (!isset($sort_keys[$sort])) { + $sort = 'schedule'; +} +uasort($jobs, function ($a, $b) use ($sort_keys, $sort, $order) { + $compared = $sort_keys[$sort]($a) <=> $sort_keys[$sort]($b); + return ($order === 'DESC' ? -$compared : $compared) ?: $a['position'] <=> $b['position']; +}); +// A heading sorts its own way round first - names A to Z, the latest run and what needs a look at the top - then the other +$sort_first = ['job' => 'ASC', 'schedule' => 'ASC', 'last_run' => 'DESC', 'status' => 'ASC']; +$sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon, $sort_first) { + return '' + . $label . ($sort === $column ? " $order_icon" : '') . ''; +}; + +$status_looks = [ + 'queued' => ['label' => 'Queued', 'tone' => 'warning'], + 'running' => ['label' => 'Running', 'tone' => 'primary'], + 'lost' => ['label' => "Didn't finish", 'tone' => 'danger'], + 'completed' => ['label' => 'Completed', 'tone' => 'success'], + 'failed' => ['label' => 'Failed', 'tone' => 'danger'], + 'skipped' => ['label' => 'Skipped', 'tone' => 'secondary'], + 'paused' => ['label' => 'Paused', 'tone' => 'warning'], + 'never' => ['label' => 'Not run yet', 'tone' => 'secondary'], +]; + ?> -
-
-

Cron

-
-
+
- -
-
Cron is not running
- ITFlow last heard from cron . - Nothing below will run - no mail is being sent, no email is being turned into tickets, and invoices are not being generated. - Add this line to the crontab of the user that owns the ITFlow files: -
-
- -
- Cron last checked in . - -
- +
- -
- -
Cron is switched off
- The dispatcher is running, but every job below stops itself immediately while this is off - - no mail is sent, no email becomes a ticket, and nothing is invoiced. -
- -

- - The master switch is on. Turning it off - stops every job at once without touching their schedules, which is what you want on a restored - backup or a staging clone - those come up with every job enabled and will otherwise email clients - and charge cards. Switching back on returns you to exactly this configuration. - Turn cron off. - -

- + +
+
> +
+ +
+

-
- - - - - - - - - - - - - - +

+ ITFlow does its background work from one cron job on the server - sending email, turning email into tickets, recurring invoices, reminders, backups - and it hasn't heard from it yet. + Add this line to the crontab of the user that owns ITFlow's files, or as a cron job that runs every minute in your hosting control panel: +

+ +

This page says so by itself once it has run - give it a minute.

- $job_row = $job['row']; + +

+ ITFlow last heard from it . Until it runs again no email is sent, no email becomes a ticket, and nothing is invoiced or backed up. + Check that the server's cron service is running, and that this line is still in the crontab of the user that owns ITFlow's files: +

+ +

This page says so by itself when it's back.

- // A job the dispatcher has not met yet has no row. It gets one on the next - // pass, seeded from the schedule in the registry, so show that meanwhile. - $cron_job_id = $job_row ? intval($job_row['cron_job_id']) : 0; - $enabled = $job_row ? intval($job_row['cron_job_enabled']) : 1; - $schedule = $job_row ? $job_row['cron_job_schedule'] : $job['schedule']; - $interval_minutes = $job_row ? intval($job_row['cron_job_interval_minutes']) : intval($job['interval_minutes'] ?? 1); - $daily_at = $job_row ? $job_row['cron_job_daily_at'] : ($job['daily_at'] ?? null); - $run_now = $job_row ? intval($job_row['cron_job_run_now']) : 0; - $last_run_at = $job_row ? $job_row['cron_job_last_run_at'] : null; - $last_duration = $job_row ? $job_row['cron_job_last_duration'] : null; - $last_status = $job_row ? $job_row['cron_job_last_status'] : null; - $last_error = $job_row ? $job_row['cron_job_last_error'] : null; - $last_error_at = $job_row ? $job_row['cron_job_last_error_at'] : null; + +

+ The server still calls it every minute, but each job stops itself straight away - no email is sent, no email becomes a ticket, and nothing is invoiced or backed up. + That's right for a restored backup or a test copy, which mustn't email your clients or charge their cards. Anywhere else, turn it back on - every job's schedule is as it was left. +

- $next_run = $job_row ? cronJobNextRun($job_row) : null; + +

+ has made this ITFlow . + keep running; the rest wait, and run when it's active again. +

- if ($run_now) { - $status_badge = 'Queued'; - } elseif ($last_status === 'Running') { - $status_badge = 'Running'; - } elseif ($last_status === 'Completed') { - $status_badge = 'Completed'; - } elseif ($last_status === 'Failed') { - $status_badge = 'Failed'; - } elseif ($last_status !== null) { - $status_badge = 'Stopped'; - } else { - $status_badge = 'Never run'; - } + +

+ Cron itself is running - it last ran - but didn't finish last run. + What went wrong is under below. +

- ?> - - - - - - - - - - - - - - - - -
JobScheduleLast RunDurationStatusNext RunAction
- - Disabled -
-
cron/ -
- -
-
- - 9) { ?> -
+ +

+ It last ran . job on, and none failed on its last run. + + Next is , . + +

+ + + +

+ failed earlier and run since - what went wrong is kept below until you dismiss it. +

+ + + +

+ on way - this page keeps itself up to date. +

+ + + + +

Cron is switched off here as well, so its jobs do nothing when it runs until it's turned on.

-
-
-
-
- - - -
- Last error - -
-
-
+ +
+
+
-

- - Run Now does not start the job in your browser - it asks the - dispatcher to pick it up on its next pass, so a job starts within a minute and still runs on the command - line with the same locking as a scheduled run. Detailed per-job output is in - App Logs. - -

+ +
+
+

Jobs

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + Off + + + + +
Next
+ +
+ + + +
Took
+ + + — + +
+ + + + + +
+
+ +
+ +
+ + +
Asked to run now - .
+ +
It started and never said how it ended - most likely cut off by a restart or a time limit. It runs again when it's next due, or sooner with Run now.
+ +
It stopped early without saying why - App Logs may have it.
+ +
+ + + +
+ +
+
+ Failed - it has run since' ?> +
+
+
+ Dismiss +
+ +
+
+ +
+ +
+ + +
+
+

At a glance

+
+
    +
  • + Cron + + + +
  • +
  • + Last ran + +
  • +
  • + Jobs + on +
  • +
  • + Failed + +
  • + +
  • + Next + + + + +
  • + +
  • + Times are in + + + now + +
  • +
+
+ + + +
+
+

The cron job

+
+
+

Everything on this page runs from one line in the server's crontab - that of the user that owns ITFlow's files:

+ +

It runs every minute and works out which jobs are due, so changing a job's schedule here never means changing it. On a hosting control panel, it's a cron job that runs every minute.

+
+
+ + + + +
+
+

Turning cron off

+
+
+

Stops every job at once, without touching their schedules. It's for a restored backup or a test copy, which would otherwise email your clients and charge their cards. Turning it back on picks up exactly where this leaves off.

+ + Turn cron off + +
+
+ + +
+
+ + diff --git a/admin/modals/cron/cron_edit.php b/admin/modals/cron/cron_edit.php index 0aeb13b2e..ce38c1288 100644 --- a/admin/modals/cron/cron_edit.php +++ b/admin/modals/cron/cron_edit.php @@ -35,19 +35,23 @@ ob_start(); - Your ITFlow timezone. A run missed because the server was off happens at the next opportunity instead of waiting a day. + In ITFlow's time zone. A run missed because the server was off happens when it's back, not a day later.
diff --git a/admin/post/cron.php b/admin/post/cron.php index 535b9384a..84a4e4922 100644 --- a/admin/post/cron.php +++ b/admin/post/cron.php @@ -23,7 +23,7 @@ if (isset($_GET['enable_cron']) || isset($_GET['disable_cron'])) { logAudit("Cron", "Edit", "$session_name " . ($enabled ? 'enabled' : 'disabled') . " the master cron switch"); - flashAlert("Cron " . ($enabled ? 'enabled' : 'disabled') . ".", $enabled ? 'success' : 'error'); + flashAlert("Cron turned " . ($enabled ? 'on' : 'off') . ".", $enabled ? 'success' : 'error'); redirect(); @@ -130,13 +130,16 @@ if (isset($_GET['enable_cron_job']) || isset($_GET['disable_cron_job'])) { redirect(); } + require_once "../includes/cron_jobs.php"; + $cron_job_name = escapeSql($row['cron_job_name']); + $cron_job_label = cronJobRegistryByName()[$row['cron_job_name']]['label'] ?? 'Cron job'; mysqli_query($mysqli, "UPDATE cron_jobs SET cron_job_enabled = $enabled WHERE cron_job_id = $cron_job_id"); logAudit("Cron", "Edit", "$session_name " . ($enabled ? 'enabled' : 'disabled') . " cron job $cron_job_name", 0, $cron_job_id); - flashAlert("Cron job " . ($enabled ? 'enabled' : 'disabled') . ".", $enabled ? 'success' : 'error'); + flashAlert("$cron_job_label turned " . ($enabled ? 'on' : 'off') . ".", $enabled ? 'success' : 'error'); redirect(); diff --git a/includes/cron_jobs.php b/includes/cron_jobs.php index eb9a4fee9..689a6c9db 100644 --- a/includes/cron_jobs.php +++ b/includes/cron_jobs.php @@ -23,6 +23,8 @@ * That shared use is why this sits here rather than in cron/includes/ with the lock, which * only cron loads: the admin pages would otherwise be reaching into the cron directory. * + * 'icon' is the job's Font Awesome icon on Maintenance > Cron, and nothing to the dispatcher. + * * 'enabled' => 0 ships a job switched off - the row is seeded disabled and stays that way * until somebody turns it on in Maintenance > Cron. Used for work an install should opt into * rather than inherit from an upgrade, like the backup job filling a disk overnight. @@ -56,6 +58,7 @@ function cronJobRegistry(): array [ 'name' => 'demo_reset', 'label' => 'Demo Reset', + 'icon' => 'fa-undo', 'script' => 'demo_reset.php', 'description' => 'For the demo (config.php\'s $config_demo_mode): puts it back to fresh demo data - the demo accounts, Starter Content and the demo clients - and signs everyone out. Locked in the demo; php cron/demo_reset.php runs it now.', 'schedule' => 'Daily', @@ -65,6 +68,7 @@ function cronJobRegistry(): array [ 'name' => 'manager_checkin', 'label' => 'Manager Check-in', + 'icon' => 'fa-network-wired', 'script' => 'manager_checkin.php', 'description' => 'For an ITFlow managed by another (Admin > Managed By): reports how it is doing and takes what its manager sets. Stops straight away when it isn\'t managed.', 'schedule' => 'Interval', @@ -73,6 +77,7 @@ function cronJobRegistry(): array [ 'name' => 'mail_queue', 'label' => 'Mail Queue', + 'icon' => 'fa-envelope', 'script' => 'mail_queue.php', 'description' => 'Sends everything ITFlow has queued - invoices, quotes, ticket replies, notifications.', 'schedule' => 'Interval', @@ -81,6 +86,7 @@ function cronJobRegistry(): array [ 'name' => 'sms_queue', 'label' => 'Text Messages', + 'icon' => 'fa-comment-dots', 'script' => 'sms_queue.php', 'description' => 'Sends the texts waiting to go, a second apart, texts reminders of visits and calls coming up, and tidies the text log - once texting is set up in Admin > Text Messaging.', 'schedule' => 'Interval', @@ -89,6 +95,7 @@ function cronJobRegistry(): array [ 'name' => 'ticket_email_parser', 'label' => 'Ticket Email Parser', + 'icon' => 'fa-inbox', 'script' => 'ticket_email_parser.php', 'description' => 'Reads the support mailbox and turns incoming mail into tickets and replies.', 'schedule' => 'Interval', @@ -97,6 +104,7 @@ function cronJobRegistry(): array [ 'name' => 'ticket_sla', 'label' => 'Ticket SLA Monitor', + 'icon' => 'fa-stopwatch', 'script' => 'ticket_sla.php', 'description' => 'Moves tickets through their SLA warning and breach stages and sends the alerts.', 'schedule' => 'Interval', @@ -105,6 +113,7 @@ function cronJobRegistry(): array [ 'name' => 'ai_triage', 'label' => 'AI Triage', + 'icon' => 'fa-robot', 'script' => 'ai_triage.php', 'description' => 'Gives new tickets from email, the portal and the API a category and priority by AI, when Settings > Tickets says to. Up to 5 a run.', 'schedule' => 'Interval', @@ -113,6 +122,7 @@ function cronJobRegistry(): array [ 'name' => 'managed_instances', 'label' => 'Managed Instances', + 'icon' => 'fa-server', 'script' => 'managed_instances.php', 'description' => 'For an ITFlow that manages others (Admin > Managed Instances): notifies when one stops checking in, and closes requests that were never answered.', 'schedule' => 'Interval', @@ -121,6 +131,7 @@ function cronJobRegistry(): array [ 'name' => 'payment_provider_fees', 'label' => 'Payment Provider Fees', + 'icon' => 'fa-credit-card', 'script' => 'payment_provider_fees.php', 'description' => 'Records what Stripe charged on each card payment as an expense, once Stripe has settled it - while Admin > Payment Providers records its fees.', 'schedule' => 'Interval', @@ -129,6 +140,7 @@ function cronJobRegistry(): array [ 'name' => 'domain_refresher', 'label' => 'Domain Refresher', + 'icon' => 'fa-globe', 'script' => 'domain_refresher.php', 'description' => 'Looks up DNS, registration and expiry for domains that are due - never looked up, not in a week, or expiring within 60 days. Up to 50 a run.', 'schedule' => 'Daily', @@ -137,6 +149,7 @@ function cronJobRegistry(): array [ 'name' => 'nightly_tasks', 'label' => 'Nightly Tasks', + 'icon' => 'fa-moon', 'script' => 'nightly_tasks.php', 'description' => 'The daily run: recurring invoices and tickets, overdue reminders, autopay, late fees, clean-up, update check.', 'schedule' => 'Daily', @@ -146,6 +159,7 @@ function cronJobRegistry(): array [ 'name' => 'backup', 'label' => 'Backup', + 'icon' => 'fa-cloud-upload-alt', 'script' => 'backup.php', 'description' => 'Builds the scheduled backup and anything queued from Maintenance > Backup. Off by default.', 'schedule' => 'Daily', @@ -156,6 +170,7 @@ function cronJobRegistry(): array [ 'name' => 'certificate_refresher', 'label' => 'Certificate Refresher', + 'icon' => 'fa-lock', 'script' => 'certificate_refresher.php', 'description' => 'Checks the host of every certificate once a day - what it serves now, its expiry and issuer, and whether it is OK, for another name, untrusted, expired or unreachable.', 'schedule' => 'Daily', @@ -164,6 +179,7 @@ function cronJobRegistry(): array [ 'name' => 'update_check', 'label' => 'Update Check', + 'icon' => 'fa-sync-alt', 'script' => 'update_check.php', 'description' => 'Asks the git remote whether a newer release exists and stores the answer for Maintenance > Update.', 'schedule' => 'Daily', @@ -172,6 +188,7 @@ function cronJobRegistry(): array [ 'name' => 'app_update', 'label' => 'Application Update', + 'icon' => 'fa-download', 'script' => 'app_update.php', 'description' => 'Runs an update queued from Maintenance > Update. Does nothing unless one is queued.', 'schedule' => 'Daily', @@ -266,32 +283,3 @@ function cronJobNextRun(array $row): ?string return date('Y-m-d H:i:s', max($next, time())); } - -/* - * "4 minutes ago" / "in 2 minutes" for the admin page. Anything older than a day reads as a - * date, because "27,000 minutes ago" tells nobody anything. - */ -function cronJobTimeAgo(?string $datetime): string -{ - if (empty($datetime)) { - return 'Never'; - } - - $seconds = time() - strtotime($datetime); - $ahead = $seconds < 0; - $seconds = abs($seconds); - - if ($seconds < 60) { - $text = 'less than a minute'; - } elseif ($seconds < 3600) { - $minutes = floor($seconds / 60); - $text = $minutes . ' minute' . ($minutes == 1 ? '' : 's'); - } elseif ($seconds < 86400) { - $hours = floor($seconds / 3600); - $text = $hours . ' hour' . ($hours == 1 ? '' : 's'); - } else { - return date('M j, g:i A', strtotime($datetime)); - } - - return $ahead ? "in $text" : "$text ago"; -} diff --git a/js/auto_reload.js b/js/auto_reload.js index 277e80c2d..b6514e2b3 100644 --- a/js/auto_reload.js +++ b/js/auto_reload.js @@ -8,6 +8,10 @@ * * Deliberately an attribute rather than an inline