How Monitoring Works
This page explains what happens between the scheduler running a task and a row appearing in your database: which Laravel events the package listens to, how a task is matched to its database row, what each log item contains, how background tasks are handled, and the exact rules used to decide that a run failed or finished late.
On this page:
- The Two Halves: Sync and Events
- Scheduler Events and What They Record
- Log Item Meta
- How Failures Are Detected
- Background Tasks
- Skipped Tasks
- When Is a Task Late?
- When Is the Last Run Shown as Failed?
- Environments
The Two Halves: Sync and Events
Monitoring has two independent parts:
- The sync (
schedule-monitor:sync) decides which tasks are monitored. It writes one row per task tomonitored_scheduled_tasks, keyed by the task's name. - The event listeners record what happens. They are always active, but they only write log items for tasks that have a row.
When an event arrives, the package rebuilds the task's name from the scheduler event (using monitorName() if set, otherwise the default name) and looks up the row with that name. If there is no row (the task was never synced, was renamed since the last sync, or has no name), the event is ignored without any warning.
Note: The lookup at run time only uses the name. It does not re-check
doNotMonitor(), and it does not know about duplicates. Two tasks with the same name both write to the same row (see Duplicate Names).
Scheduler Events and What They Record
| Laravel event | Log item type | Task column updated | Oh Dear ping |
|---|---|---|---|
ScheduledTaskStarting |
starting |
last_started_at |
Only when send_starting_ping is true |
ScheduledTaskFinished, exit code 0 or null |
finished |
last_finished_at |
Yes (/finished) |
ScheduledTaskFinished, any other exit code |
failed |
last_failed_at |
Yes (/failed) |
ScheduledTaskFailed (an exception was thrown) |
failed |
last_failed_at |
Yes (/failed) |
ScheduledTaskSkipped |
skipped |
last_skipped_at |
No |
Pings are only dispatched when the task row has a ping_url, which the sync sets when Oh Dear is configured. See Oh Dear Pings.
The log item types are available as constants on the log item model:
| Constant | Value |
|---|---|
MonitoredScheduledTaskLogItem::TYPE_STARTING |
starting |
MonitoredScheduledTaskLogItem::TYPE_FINISHED |
finished |
MonitoredScheduledTaskLogItem::TYPE_FAILED |
failed |
MonitoredScheduledTaskLogItem::TYPE_SKIPPED |
skipped |
A normal successful run therefore produces two log items, starting then finished. A run that throws produces starting then failed.
Log Item Meta
Each log item has a JSON meta column (cast to an array). Its contents depend on the type:
| Type | Meta keys |
|---|---|
starting |
memory |
finished |
runtime, exit_code, memory, output |
failed from a non-zero exit code |
runtime, exit_code, memory, output |
failed from an exception |
failure_message |
skipped |
none (meta is null) |
| Key | Meaning |
|---|---|
memory |
memory_get_usage(true) in bytes, measured in the PHP process that runs the scheduler when the event is handled. For Artisan commands and shell commands, which Laravel runs as separate processes, this is the scheduler's memory, not the child process's. For background tasks it is 0 in finished items. |
runtime |
The run time in seconds reported by Laravel's ScheduledTaskFinished event. 0 for background tasks. |
exit_code |
The task's exit code as Laravel recorded it. For closures and jobs, current Laravel versions report 0 on success; a scheduled closure that returns false gets exit code 1 and is therefore recorded as a failure. |
output |
The captured output, only for tasks using storeOutputInDb(); otherwise null. |
failure_message |
The exception message, truncated to 255 characters. |
Note: Meta is written with a full replace, not a merge. A
faileditem created from an exception contains onlyfailure_message.
How Failures Are Detected
There are two routes to a failed log item:
- A non-zero exit code. When
ScheduledTaskFinishedarrives with an exit code that is neither0nornull, the package records a failure instead of a finish. This is how failing Artisan and shell commands are caught: Laravel runs them in a separate process and only sees the exit code. - An exception. When Laravel fires
ScheduledTaskFailed, for example because a scheduled closure threw, the package records a failure with the exception message.
In current Laravel versions the scheduler also throws an exception of its own after a foreground command exits with a non-zero code ("Scheduled command [...] failed with exit code [...]"), which fires ScheduledTaskFailed as well. In that case you can see two failed log items for one run: one with exit_code and runtime, and one with failure_message, and Oh Dear receives two failure pings. Both describe the same failure.
Background Tasks
Tasks scheduled with ->runInBackground() are started by the scheduler, which then moves on without waiting. Laravel fires ScheduledTaskFinished right away, with an exit code of null. The package recognizes this case (a background task with a null exit code) and ignores that event, because the task has not actually finished.
When the background process ends, Laravel calls php artisan schedule:finish with the task's id and exit code. The package listens for that command starting, attaches an "after" callback to every background task in the schedule, and when Laravel runs the callback for the finished task it records:
- a
finisheditem withruntime0andmemory0when the exit code is0, or - a
faileditem when the exit code is non-zero.
So for background tasks the starting and finished times are accurate, but runtime and memory are not measured. Compute the duration from the created_at of the starting and finished items if you need it.
Skipped Tasks
Laravel fires ScheduledTaskSkipped when a due task does not run because one of its filters rejected it. This includes when() and skip() conditions, withoutOverlapping() when the previous run still holds the lock, and, depending on your Laravel version, onOneServer() on the servers that did not get the lock. The package writes a skipped log item and sets last_skipped_at, but does not ping Oh Dear.
That is the reason the README lists between, unlessBetween, when, and skip as unsupported: Oh Dear only knows the cron expression, so it expects a ping on every cron tick, and a skipped run looks like a missed run there. See Troubleshooting.
When Is a Task Late?
The list command marks a task's Finished at time in red when its last run "finished too late". The rule, from the package's Task::lastRunFinishedTooLate():
- Take the time of the last finish. If the task never finished, take the moment its row was created (minus one second).
- Compute the next time the cron expression is due after that moment, in the task's timezone.
- Add the grace time.
- If that deadline is in the past, the task is late.
Example: an hourly task with the default grace time of 5 minutes last finished at 10:00:03. The next run after that is 11:00, so the deadline is 11:05. At 11:05:01, if no newer finish has been recorded, the task is late. Once the 11:00 run finishes, the clock resets and the next deadline is 12:05.
Things to know about this rule:
- Only
finishedevents reset the clock. A task that keeps failing becomes "late" too, because failures do not updatelast_finished_at. - The grace time is the time a run may take after its scheduled start. A daily task that usually needs 30 minutes should have a grace time above 30, or it will look late every day until it finishes.
- The list only colors the time red when there is a last finished time. A task that has never finished is not highlighted, even if it is overdue: its
Finished atsimply shows--.
When Is the Last Run Shown as Failed?
The Failed at time is shown in red when the last failure happened after the last start (with one second of tolerance). If the task started again after the failure, the failure is considered old and is no longer highlighted. If there is a failure but no recorded start at all, it is highlighted.
Environments
The sync and the list only consider tasks that run in the current environment. A task restricted with ->environments('production') is invisible to schedule-monitor:sync and schedule-monitor:list when APP_ENV is staging. Because the sync deletes rows for tasks it does not see, run it in each environment with that environment's own database. See schedule-monitor:sync.