Skip to content

API reference

The package namespace is JkOster\CronMonitor. The service provider is JkOster\CronMonitor\CronMonitorServiceProvider, registered through package discovery.

Classes

ClassPathPurpose
CronMonitorServiceProvidersrc/CronMonitorServiceProvider.phpPublishes config and migration, registers routes and commands.
Models\CronMonitorsrc/Models/CronMonitor.phpEloquent model with the health check trait and presenter.
CronMonitorRepositorysrc/CronMonitorRepository.phpStatic queries for monitors.
CronMonitorCollectionsrc/CronMonitorCollection.phpCollection with checkHealth() and sortByName().
Http\Controllers\CronMonitorControllersrc/Http/Controllers/CronMonitorController.phpping() and status() actions.
Facades\CronMonitorsrc/Facades/CronMonitor.phpFacade for CronMonitorRepository.
Notifications\Notifiablesrc/Notifications/Notifiable.phpDefault notification recipient.
Helpers\Periodsrc/Helpers/Period.phpDowntime period with duration() and toText().

Facade

The package registers the alias CronMonitor for JkOster\CronMonitor\Facades\CronMonitor. The facade resolves the CronMonitorRepository singleton, so every repository query is available through the facade:

use JkOster\CronMonitor\Facades\CronMonitor;

$monitor = CronMonitor::getByHash($hash);
$due = CronMonitor::getForUptimeCheck();

Model

JkOster\CronMonitor\Models\CronMonitor uses HasFactory, SupportsCronHealthCheck, and CronMonitorPresenter. It has no Eloquent relations.

Fillable attributes:

name, description, enabled, hash, status, frequency_in_minutes,
cron_expression, next_due_date, grace_period_in_minutes, last_check_date,
last_check_failed_date, failure_reason, status_last_change_date,
last_ping_status, timezone

Casts: enabled to boolean, last_check_date, last_check_failed_date, status_last_change_date, and next_due_date to datetime.

MethodReturnsBehavior
scopeEnabled($query)queryAdds where('enabled', true).
isHealthy()boolfalse when the monitor is enabled and its status is down or unknown.
status_as_emojistringAccessor with ✅, 🚨, 🕑, or ❓.
formattedReportTitle()stringEmoji plus name plus `
formattedLastCheck(string $format = '')stringFormats last_check_date. Empty format uses cron-monitor.notifications.date_format. forHumans uses diffForHumans().
formattedLastCheckFailed(string $format = '')stringSame for last_check_failed_date.

Health check methods

MethodReturnsBehavior
shouldCheckDown()boolfalse when disabled or already down. Otherwise true when next_due_date is past.
statusExpired()booltrue when next_due_date is set and past.
`calculateNextDueDate(?DateTime $currentTime = null, DateTimeZonestringnull $tz = null)`
calculateNextDueDateWithGracePeriod(...)DateTimeCalls calculateNextDueDate() and adds grace_period_in_minutes.
cronMonitorStatusReceived(string $incomingPingStatus, ?Request $request = null)stringMaps the ping status, updates the monitor, and fires the ping, failure, or recovery event. Throws InvalidPingStatus for an unknown value.
checkHealthStatus(?Carbon $now = null)stringReturns unknown without a last_check_date, marks an expired monitor down, otherwise returns the current status.
cronMonitorHasFailed(?Request $request = null)voidSets down, writes last_check_failed_date, sets the failure reason, saves, and fires CronCheckFailedEvent.
setFailureReason(?Request $request = null)voidUses the raw body, reason, message, or the default text.
cronMonitorHasRecovered(?Request $request = null)voidSets up and fires CronCheckRecoveredEvent when last_check_failed_date is set.
cronMonitorGracePeriodStarted()voidSets started and sets next_due_date to now plus grace_period_in_minutes.
cronMonitorStatusUnknown()voidSets unknown.

The saving hook fills status_last_change_date when it is empty and calculates next_due_date from the schedule plus grace period when it is empty. It recalculates the due date when cron_expression, frequency_in_minutes, or grace_period_in_minutes changes, unless the save also sets next_due_date explicitly. The creating hook assigns a UUID to hash when it is empty and falls back to cron-monitor.timezone when timezone is empty.

Repository

MethodReturnsBehavior
getByHash(string $hash)?CronMonitorFinds one monitor by hash.
getUnchecked()CollectionEnabled monitors with status unknown, sorted by name.
getEnabled()CollectionAll enabled monitors, sorted by name.
getDisabled()CollectionAll disabled monitors, sorted by name.
getForUptimeCheck()CronMonitorCollectionEnabled monitors where shouldCheckDown() is true, sorted by name.
getHealthy()CollectionEnabled monitors where isHealthy() is true, sorted by name.
getWithFailingUptimeCheck()CollectionEnabled monitors with status down, sorted by name.
getUnhealthy()CollectionEnabled monitors where isHealthy() is false, sorted by name.

Every query uses monitor_model from the config. An invalid class throws InvalidConfiguration.

Collection

JkOster\CronMonitor\CronMonitorCollection extends Illuminate\Support\Collection.

MethodBehavior
checkHealth(bool $force = false)Calls checkHealthStatus() on each monitor when $force is true or shouldCheckDown() is true.
sortByName()Sorts by name and returns the collection.

Enums

JkOster\CronMonitor\Models\Enums\CronMonitorStatus is a class with string constants, not a PHP enum.

ConstantValue
CronMonitorStatus::UPup
CronMonitorStatus::DOWNdown
CronMonitorStatus::STARTEDstarted
CronMonitorStatus::UNKNOWNunknown

JkOster\CronMonitor\Models\Enums\IncomingPingStatus:

ConstantValue
IncomingPingStatus::SUCCESSsuccess
IncomingPingStatus::ERRORerror
IncomingPingStatus::STARTEDstarted
IncomingPingStatus::UNKNOWNunknown

Events

ClassConstructorBroadcast channel
Events\IncomingPingReceivedEventCronMonitor $monitor, string $status, array $requestData, ?string $ipcroncheck.pinged.{monitor.id}
Events\CronCheckFailedEventCronMonitor $monitor, ?array $requestDatacroncheck.failed.{monitor.id}
Events\CronCheckRecoveredEventCronMonitor $monitor, Period $downtimePeriod, ?array $requestDatacroncheck.recovered.{monitor.id}

All three implement ShouldBroadcast and ShouldQueue. The channels are private.

Notifications

ClassConstructorChannels
Notifications\Notifications\CronCheckFailedCronMonitor $monitortoMail(), toSlack(), toArray()
Notifications\Notifications\CronCheckRecoveredCronMonitor $monitor, Period $downtimePeriodtoMail(), toSlack(), toArray()

BaseNotification gets its channels from cron-monitor.notifications.notifications[static::class] and reads notifications.location for a location description. Notifiable routes mail to notifications.mail.to and Slack to notifications.slack.webhook_url.

NotificationsEventHandler subscribes to CronCheckFailedEvent and CronCheckRecoveredEvent. It maps each event to its shipped notification, checks isStillRelevant(), resolves the configured notifiable, and sends the notification. A configured notification class may extend the shipped class for the event, which lets you replace the message while keeping the event data flow.

Commands

SignatureDescription
cron-monitor:create {name} {--d|description=} {--f|frequency=60} {--g|grace=5} {--c|cron=} {--t|timezone=} {--disabled}Creates a monitor and prints its hash, ping URL, and next due date. Validates the cron expression, frequency, grace period, and timezone.
cron-monitor:process-checks {--f|force}Checks enabled monitors and prints healthy and unhealthy counts. --force checks monitors that are not due or already down.
cron-monitor:sync-file {path} {--delete-missing}Creates or updates monitors from a JSON file. Keeps the hash from the file and generates one when it is missing. --delete-missing removes rows whose hash is not in the file. Aborts without changing anything when the file is not valid JSON.

All commands extend Commands\BaseCommand.

Exceptions

ClassFactoryRendered response
Exceptions\InvalidPingStatusreceivedPingStatusIsInvalid(string $status)HTTP 400 with the message.
Exceptions\InvalidConfigurationmodelIsNotValid(string $className)None.
Exceptions\InvalidPeriodstartDateMustComeBeforeEndDate(CarbonInterface $start, CarbonInterface $end)None.
Exceptions\CannotSaveMonitoralreadyExists(CronMonitor $monitor)None. SyncFile throws it directly for an invalid cron expression.

Source map

AreaPath
Service provider and routessrc/CronMonitorServiceProvider.php, routes/api.php
Model and health checksrc/Models/CronMonitor.php, src/Models/Traits/SupportsCronHealthCheck.php
Presentersrc/Models/Presenters/CronMonitorPresenter.php
Repository and collectionsrc/CronMonitorRepository.php, src/CronMonitorCollection.php
Controllersrc/Http/Controllers/CronMonitorController.php
Commandssrc/Commands/ProcessCronCheck.php, src/Commands/SyncFile.php
Eventssrc/Events/
Notificationssrc/Notifications/
Config and migrationconfig/cron-monitor.php, database/migrations/create_cron_monitors_table.php.stub