diff --git a/CHANGELOG.md b/CHANGELOG.md index 034fefc6d..dc3aeb7cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,10 @@ This file documents all notable changes made to ITFlow. ### Upgrading to 26.12.5 -Update from Maintenance > Update as usual. There are no database changes. +Update from Maintenance > Update as usual. This release has one database change, 4.4.2, which keeps when a cron job was last started by something other than cron.php. + +- If ITFlow was installed with the install script before 26.08, `/etc/cron.d/itflow` may still have a line for each job besides the `cron.php` one - the 26.08 upgrade asked for them to come out. Maintenance > Cron now lists any it finds, with the file and line. Take them out: `cron.php` runs every job. Until then they step aside while `cron.php` is running, so nothing runs twice. +- Cron started as root - a crontab line for root, or `php cron/cron.php` typed in a root shell - now runs as the user that owns ITFlow's files, so what it makes is that user's. ### New Features & Updates @@ -15,12 +18,15 @@ Update from Maintenance > Update as usual. There are no database changes. ### Bug Fixes +- A cron job that an old cron line also ran - the line for the Ticket Email Parser or the Mail Queue left in `/etc/cron.d/itflow` by the install script before 26.08 - no longer looks stopped. The old line's run took the job's lock, so `cron.php` passed it by: its last run on Maintenance > Cron stayed days old and Cron Watch emailed that it had fallen behind, while its work got done - and turning it off didn't stop it. Now the old line's run steps aside while `cron.php` is running, Maintenance > Cron lists the lines to take out, with the file and line, and so do Debug and Cron Watch's email. A line ITFlow can't see - in a user's own crontab - is still found from its runs, and said with where to look. +- A cron job whose lock file was made by a run as root no longer stops cron. The web server's user can't open such a file, and every job after it in the list stopped, every minute, without a word - Cron Watch with them - until the server restarted. Now that job fails, saying which file to delete, and the rest run. Cron started as root now becomes the user that owns ITFlow's files first, so it doesn't leave such files - or uploads, backups and git files the web server can't use - behind. - AI Triage no longer runs every minute when AI triage is off in Settings > Tickets or there's no AI model to ask - before, it ran anyway and showed **Completed** every minute with nothing done. ### Developer Updates - `getActiveAgentUserCount()` (`functions/app.php`) leaves out `users.user_service_account = 1` with `serviceAccountsOutSql()`, so `getAgentSeatsTaken()`, `agentUserLimitReached()` and the managed report's `agents.active` do too (the report's list still carries them, with `service` set). `admin/post/users.php` skips `agentUserLimitReached()` for an API / service account in `add_user`, `activate_user`, `restore_user` and `bulk_activate_users`, and `edit_user` refuses turning an active one into an agent at the limit before changing anything (`serviceAccountLeavingNeedsSeat()`, `functions/service_account.php`). Admin > Users keeps New User open at the limit when service accounts are available, and the New User window says one can still be added. - Cron jobs can declare `'not_in_use' => function () { ... }` in `includes/cron_jobs.php`, returning why the job has nothing to do on this install or `''` (`cronJobNotInUse()`, which counts a check that throws as in use; `cronJobSettings()` reads the settings the checks need once a request). `cron/cron.php` reads every job's row and Run Now flag once a cycle and skips a job not in use before its lock and claim - registering its row the first time (`cronJobRegister()`, split out of `cronJobClaim()`) - unless it is asked to Run Now. `cronHealthJobs()` leaves such jobs out, so Cron Watch neither waits on nor flags them, and `admin/cron.php` shows them in a new `unused` state. Checks added for `manager_checkin`, `mail_queue`, `sms_queue`, `rmm_sync`, `rmm_alerts`, `ticket_email_parser`, `ai_triage`, `managed_instances`, `payment_provider_fees` and `update_check`, each the same condition its script stops on. `aiTriageNotInUse()` (`functions/ai_ticket.php`) is new, and `cron/ai_triage.php` now stops on it too. CONTRIBUTING.md has the rule. +- Cron and runs outside it (database 4.4.2, `cron_jobs.cron_job_outside_at`): `cron/includes/cron_lock.php` gains `cronLockFile()`, `cronLockHolder()`, `cronLockFileProblem()`, `cronLockDispatchBeat()`, `cronLockOutsideFile()`, `cronLockAttended()` and `cronLockRunAsOwner()`, run first: started as root on an install whose `cron/cron.php` is another user's, it calls `posix_initgroups()`, `posix_setgid()` and `posix_setuid()` to become that user, or exits saying how to run it. `cronLockAcquire($path, $stamp)` returns null for a lock file it can't open (it used to `die()`, ending the dispatch at that job), and writes `direct` or `terminal` into the file while a run on its own holds it. The dispatcher touches the heartbeat file each cycle; a run on its own without a terminal on STDIN (`stream_isatty()`) while it is under 180 seconds old exits 0, quietly, and touches its `.outside` file. The dispatcher records that file's time, or a lock found held by a `direct` run, as `cron_job_outside_at` (`cronJobNoteOutside()`, at most every ten minutes), and fails a job whose lock can't be opened with `cronLockFileProblem()` as its error (`cronJobLockFailed()`, logged to App Logs once). `functions/cron_health.php`: `cronStrayLines()` reads `/etc/crontab` and the `/etc/cron.d` files cron would read for lines that run this install's job scripts on their own (anything in `cron/` but `cron.php`, and the old `scripts/cron*.php` and `cron_*.php`), `cronStrayText()`, `cronStrayWhat()`, `cronOutsideJobs()` (the last `CRON_HEALTH_OUTSIDE_HOURS`, 26), `cronOutsideReady()` and `cronFilesOwner()`; `cronHealthProblems()` adds a `stray` problem and an `outside` one per job the lines don't cover, and doesn't call a job overdue while it is run outside. `health.php` leaves both kinds out, so an uptime monitor doesn't go red for them. Maintenance > Cron has a card for the lines (`data-cron-stray`, `data-cron-stray-line`) and a note on each job run outside (`data-cron-outside`); Debug's Background jobs adds Old cron lines. CONTRIBUTING.md has the rule. ## [26.12.4] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 503015d98..92036aa27 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -110,6 +110,8 @@ A job can also ship switched off with `'enabled' => 0` in the registry. The row A job whose work hangs on a feature that can be off or not set up - texting, an RMM provider, AI triage, a mailbox - declares `'not_in_use' => function () { ... }` in `includes/cron_jobs.php`, returning why it has nothing to do or `''` when it has. The dispatcher then doesn't lock, claim or load it at all, Maintenance > Cron shows it as **Not in use** with the reason, and Cron Watch doesn't expect it to run. Make it the same check the script stops on - cheap, and the database only, never the network - so leaving the job out never leaves its work undone; the script keeps its own check, for Run Now and for running it by hand. +A job's script can still be run on its own - `php cron/mail_queue.php`, from **Copy the Command to Run It** - and before 26.08 each one had a crontab line of its own, which some installs keep. Such a run without a terminal while `cron.php` has run in the last three minutes steps aside: the dispatcher runs the job, on its schedule and under its switch, and records that something else tried (`cron_job_outside_at`), and Maintenance > Cron, Debug and Cron Watch name the lines in `/etc/crontab` and `/etc/cron.d` to take out. Run from a terminal, a script always runs. Started as root, cron becomes the user that owns ITFlow's files before it makes anything (`cronLockRunAsOwner()`), and a lock file the dispatcher can't open fails only that job. Don't take any of that away: a lock file left by another user used to stop every job after it, silently, until the server restarted. + `config_enable_cron` is a second, coarser switch that sits above the per-job ones. It is **not** enforced by the dispatcher — every job checks it in its own header and stops itself with `cronJobStop()`. A new job has to make that check too; one that skips it keeps running on an install that believes cron is off, which is exactly the trap `ticket_email_parser` and `ticket_sla` sat in until 26.08. Be precise about what it does, because it is easy to oversell. It is **not** a guard on restored data: a full backup dumps the `settings` table, so a restored copy comes back with `config_enable_cron = 1` alongside every enabled `cron_jobs` row, exactly as production had them. What it gives you is one reversible bit — the fastest way to stop an install acting on live data once you have noticed, and the only way to stop everything without editing seven rows. diff --git a/admin/cron.php b/admin/cron.php index 9b8d0554a..5b37ca537 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -65,7 +65,7 @@ $tidy_reason = function ($reason) { $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"); + cron_job_name, cron_job_run_now, cron_job_schedule" . (cronOutsideReady() ? ', cron_job_outside_at' : '') . " FROM cron_jobs"); while ($job_row = mysqli_fetch_assoc($sql)) { $job_rows[$job_row['cron_job_name']] = $job_row; } @@ -100,6 +100,8 @@ foreach (cronJobRegistry() as $position => $entry) { 'last_error_at' => $job_row['cron_job_last_error_at'] ?? null, 'reason' => '', 'not_in_use' => cronJobNotInUse($entry), + // Run by something other than cron.php lately - an old crontab line (functions/cron_health.php) + 'outside_at' => !empty($job_row['cron_job_outside_at']) && strtotime($job_row['cron_job_outside_at']) > time() - CRON_HEALTH_OUTSIDE_HOURS * 3600 ? $job_row['cron_job_outside_at'] : null, ]; $job['schedule_text'] = cronJobScheduleDescription($job['schedule'], $job['interval_minutes'], $job['daily_at']); $job['every_minute'] = $job['schedule'] !== 'Daily' && $job['interval_minutes'] === 1; @@ -137,6 +139,10 @@ foreach (cronJobRegistry() as $position => $entry) { $jobs[$job['name']] = $job; } +// Old crontab lines that run job scripts on their own, besides cron.php +$stray_lines = cronStrayLines(); +$stray_jobs = array_filter(array_column($stray_lines, 'job')); + $jobs_on = array_filter($jobs, function ($job) { return $job['enabled']; }); $jobs_off = count($jobs) - count($jobs_on); // On, with nothing to do here yet @@ -344,6 +350,33 @@ $status_looks = [ + + +
+ Before 26.08 each job had a cron line of its own. These are still in the server's cron files and start their jobs on their own, besides
+ cron.php. While cron.php is running they step aside, so the schedules and switches here hold - but whenever it isn't, they run their jobs outside them. Take them out - cron.php runs every job now:
+
= escapeHtml($stray_line['text']) ?>
+
+ Put # in front of each, or delete the file if every line in it is one of these - with sudo nano, say. Then this goes by itself.
+
crontab -l, as root and as ' . escapeHtml(cronFilesOwner()) . '.' ?> Take it out - while cron.php runs, it steps aside.