Skip to content

How it works

A monitor is a row in cron_monitors with a schedule, a UUID in hash, and a status. The cron job sends a ping to the hash. A scheduled command compares next_due_date with the clock and marks the monitor as down when the date has passed without a ping.

Monitor status transitions

The diagram shows the transitions that pings and the health check can cause. unknown is also the starting state for a new row.

Creating a monitor

The cron-monitor:create command and CronMonitor::create() both fill the row. The creating hook assigns Str::uuid() to hash only when it is empty and uses cron-monitor.timezone when no timezone is given. The migration defaults enabled to true, frequency_in_minutes to 60, grace_period_in_minutes to 5, timezone to UTC, and both status columns to unknown.

The saving hook fills status_last_change_date when it is empty, and calculates next_due_date when it is empty. On a new row last_check_date is empty, so the first due date is now plus frequency_in_minutes plus grace_period_in_minutes, even when a cron_expression is set. The cron expression takes over after the first ping, because the calculation only uses it when last_check_date exists. An explicitly supplied next_due_date is kept on create and on update.

Ping handling

A ping is a GET or POST request to /cron-monitor/ping/{hash}/{status?}. The status segment is optional. Without it the controller reads status from the request input and falls back to success. The value is lowercased and mapped to a monitor status:

Ping statusMonitor statusnext_due_datefailure_reason
successuprecalculated from the scheduleleft unchanged
errordownleft unchangedrequest body, reason, message, or application sent error ping
startedstartednow plus grace_period_in_minutesleft unchanged
unknownunknownleft unchangedrequest body, reason, message, or application sent error ping

The model writes last_check_date, status, and last_ping_status on every accepted ping. It also fires IncomingPingReceivedEvent before the update. A ping with an unknown status value throws InvalidPingStatus, which renders a 400 response.

A success ping recalculates the next due date and fires no failure or recovery event when the monitor was already up. When the monitor was down, it fires CronCheckRecoveredEvent if last_check_failed_date is set. A success during the started state returns the monitor to up without a recovery event.

Due dates and the grace period

calculateNextDueDate() has two branches:

  • With a valid cron_expression and a last_check_date, it returns the next run date from Cron\CronExpression.
  • Otherwise it returns now plus frequency_in_minutes.

The next due date is recalculated after a success or started ping, and when cron_expression, frequency_in_minutes, or grace_period_in_minutes changes on save. The recalculation is skipped when the save also supplies next_due_date explicitly. An error or unknown ping keeps the old due date.

The grace period is the window a job gets after it reports started. A started ping sets next_due_date to now plus grace_period_in_minutes. If no success ping arrives before that date, the health check marks the monitor as down. A success ping also adds the grace period to the recalculated due date.

The health check

php artisan cron-monitor:process-checks

Run the command every minute from the scheduler. It loads every enabled monitor and calls checkHealth on the collection. A monitor is checked when shouldCheckDown() is true:

  • enabled is true
  • status is not down
  • next_due_date is in the past

checkHealthStatus() then does one of three things:

  1. With no last_check_date, it returns unknown and sets the status to unknown if it was something else.
  2. With an expired next_due_date, it calls cronMonitorHasFailed(), sets status to down, writes last_check_failed_date, sets failure_reason to no ping received within required interval, and fires CronCheckFailedEvent.
  3. Otherwise it returns the current status unchanged.

The command prints the number of enabled monitors, the number of healthy monitors, and the number of unhealthy monitors. A monitor is healthy when it is disabled or its status is neither down nor unknown.

--force bypasses shouldCheckDown() and checks every enabled monitor. It does not mark a monitor down when the due date is still in the future; it only re-runs the check for monitors that are already down or otherwise skipped.

Reading the status

GET /cron-monitor/status/{hash} returns the status string in a JSON message. The ping route also returns the status after it applies the ping. Both routes use the prefix and middleware from cron-monitor.api.

The status endpoint returns HTTP 404 with {"message":"not found"} for an unknown hash, like the ping route.