Skip to content

Usage

The package has three artisan commands and two HTTP endpoints. Monitors themselves are Eloquent models, so the command and the model can both create them, and Eloquent changes or removes them.

Managing monitors

Create a monitor with the artisan command:

php artisan cron-monitor:create "Nightly invoice export" \
  --description="Exports paid invoices to the accounting system" \
  --frequency=1440 \
  --grace=10 \
  --timezone="Europe/Vienna"

The command prints the hash, the ping URL, and the next due date. --cron="0 2 * * *" uses a calendar schedule instead of the frequency, and --disabled creates the monitor paused. Names are not unique; the hash identifies the monitor.

The model is also available when you need to set attributes the command does not expose:

use JkOster\CronMonitor\Models\CronMonitor;

$monitor = CronMonitor::create([
    'name' => 'Nightly invoice export',
    'description' => 'Exports paid invoices to the accounting system',
    'enabled' => true,
    'frequency_in_minutes' => 1440,
    'grace_period_in_minutes' => 10,
    'timezone' => 'Europe/Vienna',
]);

Use a cron expression instead of a frequency when the job runs on a calendar schedule:

$monitor = CronMonitor::create([
    'name' => 'Nightly invoice export',
    'cron_expression' => '0 2 * * *',
    'grace_period_in_minutes' => 10,
    'timezone' => 'Europe/Vienna',
]);

The expression is validated when it is used, not when the row is saved. An invalid expression falls back to frequency_in_minutes. The sync-file command validates expressions before saving.

Pause, resume, list, and delete with normal Eloquent calls:

$monitor->update(['enabled' => false]);
$monitor->update(['enabled' => true]);

$monitors = CronMonitor::orderBy('name')->get();

$monitor->delete();

The migration and every column are listed in the table schema.

Ping endpoints

The routes are registered under cron-monitor.api.prefix, which defaults to cron-monitor. They are not inside the web or api middleware group. Only cron-monitor.api.middleware applies.

MethodPathMeaning
GET, POST/cron-monitor/ping/{hash}Reads status from the request input. Defaults to success.
GET, POST/cron-monitor/ping/{hash}/{status}Uses the status from the path.
GET/cron-monitor/status/{hash}Returns the current monitor status.

The status segment is case-insensitive. These values are accepted:

Ping statusEffect
successSets the monitor to up and recalculates the next due date.
errorSets the monitor to down and records a failure reason.
startedSets the monitor to started and sets the next due date to now plus the grace period.
unknownSets the monitor to unknown and records a failure reason. The health check that runs after the ping can mark it down when the old due date has passed.

A successful ping:

curl -fsS "https://example.com/cron-monitor/ping/6f1c2f7a-0000-0000-0000-000000000000"

The response is the status after the health check:

{"message":"pong","status":"up"}

A failure ping with a plain text reason:

curl -fsS -X POST "https://example.com/cron-monitor/ping/6f1c2f7a-0000-0000-0000-000000000000/error" \
  -H "Content-Type: text/plain" \
  --data "Invoice export failed: upstream API returned 503"

The failure reason is chosen in this order:

  1. The raw request body, when it is not empty.
  2. The reason input.
  3. The message input.
  4. application sent error ping.

The raw body wins over reason and message. Send the reason as plain text, or leave the body empty and pass reason as a query or form field:

curl -fsS -X POST "https://example.com/cron-monitor/ping/6f1c2f7a-0000-0000-0000-000000000000/error?reason=Import+timed+out"

A long job can report its start and finish:

curl -fsS "https://example.com/cron-monitor/ping/6f1c2f7a-0000-0000-0000-000000000000/started"
curl -fsS "https://example.com/cron-monitor/ping/6f1c2f7a-0000-0000-0000-000000000000"

Read the status without changing it:

curl -fsS "https://example.com/cron-monitor/status/6f1c2f7a-0000-0000-0000-000000000000"
{"message":"up"}

An unknown hash returns HTTP 404 with {"message":"not found"} on both the ping route and the status route. An invalid ping status returns HTTP 400 with {"message":"Invalid ping status received: <status>"}.

Monitor statuses

JkOster\CronMonitor\Models\Enums\CronMonitorStatus holds the stored status values:

ConstantValueMeaning
UPupThe last ping was a success.
DOWNdownAn error ping arrived, or the due date passed without a ping.
STARTEDstartedThe job reported its start and is inside the grace period.
UNKNOWNunknownNo ping has arrived, or the job explicitly reported unknown.

JkOster\CronMonitor\Models\Enums\IncomingPingStatus holds the values a ping can send:

ConstantValue
SUCCESSsuccess
ERRORerror
STARTEDstarted
UNKNOWNunknown

The stored status and the ping status are separate columns: status and last_ping_status.

Events

All three events implement ShouldBroadcast and ShouldQueue. They broadcast on private channels. Listeners run on the queue, so a queue worker has to be running for them to execute.

EventWhen it firesPayloadChannel
IncomingPingReceivedEventOn every accepted ping, before the status updateCronMonitor $monitor, string $status, array $requestData, ?string $ipcroncheck.pinged.{monitor.id}
CronCheckFailedEventWhen a monitor changes to down through an error ping, or when the health check finds an expired monitorCronMonitor $monitor, ?array $requestDatacroncheck.failed.{monitor.id}
CronCheckRecoveredEventWhen a success ping moves a monitor from down to up and last_check_failed_date is setCronMonitor $monitor, Period $downtimePeriod, ?array $requestDatacroncheck.recovered.{monitor.id}

Period exposes duration() and toText(). The recovered event uses it for the downtime in the notification text.

Notifications

The config maps a notification class to its channels:

'notifications' => [
    \JkOster\CronMonitor\Notifications\Notifications\CronCheckFailed::class => ['slack', 'mail'],
    \JkOster\CronMonitor\Notifications\Notifications\CronCheckRecovered::class => ['slack', 'mail'],
],

Set the array to [] to disable a notification. mail reads notifications.mail.to. slack reads notifications.slack.webhook_url, which defaults to CRON_MONITOR_SLACK_WEBHOOK_URL.

CronCheckFailed::isStillRelevant() returns true only while the monitor is down. CronCheckRecovered::isStillRelevant() returns true only while it is up. notifications.date_format controls dates in the message text.

NotificationsEventHandler maps CronCheckFailedEvent to CronCheckFailed and CronCheckRecoveredEvent to CronCheckRecovered, then sends the matching notification to notifications.notifiable. To customize the message, configure a class that extends the shipped notification for the event. An empty channel array still disables that notification.

Custom notifications are covered in Advanced usage.