API reference
The package namespace is JkOster\CronMonitor. The service provider is JkOster\CronMonitor\CronMonitorServiceProvider, registered through package discovery.
Classes
| Class | Path | Purpose |
|---|---|---|
CronMonitorServiceProvider | src/CronMonitorServiceProvider.php | Publishes config and migration, registers routes and commands. |
Models\CronMonitor | src/Models/CronMonitor.php | Eloquent model with the health check trait and presenter. |
CronMonitorRepository | src/CronMonitorRepository.php | Static queries for monitors. |
CronMonitorCollection | src/CronMonitorCollection.php | Collection with checkHealth() and sortByName(). |
Http\Controllers\CronMonitorController | src/Http/Controllers/CronMonitorController.php | ping() and status() actions. |
Facades\CronMonitor | src/Facades/CronMonitor.php | Facade for CronMonitorRepository. |
Notifications\Notifiable | src/Notifications/Notifiable.php | Default notification recipient. |
Helpers\Period | src/Helpers/Period.php | Downtime 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, timezoneCasts: enabled to boolean, last_check_date, last_check_failed_date, status_last_change_date, and next_due_date to datetime.
| Method | Returns | Behavior |
|---|---|---|
scopeEnabled($query) | query | Adds where('enabled', true). |
isHealthy() | bool | false when the monitor is enabled and its status is down or unknown. |
status_as_emoji | string | Accessor with ✅, 🚨, 🕑, or ❓. |
formattedReportTitle() | string | Emoji plus name plus ` |
formattedLastCheck(string $format = '') | string | Formats last_check_date. Empty format uses cron-monitor.notifications.date_format. forHumans uses diffForHumans(). |
formattedLastCheckFailed(string $format = '') | string | Same for last_check_failed_date. |
Health check methods
| Method | Returns | Behavior |
|---|---|---|
shouldCheckDown() | bool | false when disabled or already down. Otherwise true when next_due_date is past. |
statusExpired() | bool | true when next_due_date is set and past. |
| `calculateNextDueDate(?DateTime $currentTime = null, DateTimeZone | string | null $tz = null)` |
calculateNextDueDateWithGracePeriod(...) | DateTime | Calls calculateNextDueDate() and adds grace_period_in_minutes. |
cronMonitorStatusReceived(string $incomingPingStatus, ?Request $request = null) | string | Maps the ping status, updates the monitor, and fires the ping, failure, or recovery event. Throws InvalidPingStatus for an unknown value. |
checkHealthStatus(?Carbon $now = null) | string | Returns unknown without a last_check_date, marks an expired monitor down, otherwise returns the current status. |
cronMonitorHasFailed(?Request $request = null) | void | Sets down, writes last_check_failed_date, sets the failure reason, saves, and fires CronCheckFailedEvent. |
setFailureReason(?Request $request = null) | void | Uses the raw body, reason, message, or the default text. |
cronMonitorHasRecovered(?Request $request = null) | void | Sets up and fires CronCheckRecoveredEvent when last_check_failed_date is set. |
cronMonitorGracePeriodStarted() | void | Sets started and sets next_due_date to now plus grace_period_in_minutes. |
cronMonitorStatusUnknown() | void | Sets 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
| Method | Returns | Behavior |
|---|---|---|
getByHash(string $hash) | ?CronMonitor | Finds one monitor by hash. |
getUnchecked() | Collection | Enabled monitors with status unknown, sorted by name. |
getEnabled() | Collection | All enabled monitors, sorted by name. |
getDisabled() | Collection | All disabled monitors, sorted by name. |
getForUptimeCheck() | CronMonitorCollection | Enabled monitors where shouldCheckDown() is true, sorted by name. |
getHealthy() | Collection | Enabled monitors where isHealthy() is true, sorted by name. |
getWithFailingUptimeCheck() | Collection | Enabled monitors with status down, sorted by name. |
getUnhealthy() | Collection | Enabled 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.
| Method | Behavior |
|---|---|
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.
| Constant | Value |
|---|---|
CronMonitorStatus::UP | up |
CronMonitorStatus::DOWN | down |
CronMonitorStatus::STARTED | started |
CronMonitorStatus::UNKNOWN | unknown |
JkOster\CronMonitor\Models\Enums\IncomingPingStatus:
| Constant | Value |
|---|---|
IncomingPingStatus::SUCCESS | success |
IncomingPingStatus::ERROR | error |
IncomingPingStatus::STARTED | started |
IncomingPingStatus::UNKNOWN | unknown |
Events
| Class | Constructor | Broadcast channel |
|---|---|---|
Events\IncomingPingReceivedEvent | CronMonitor $monitor, string $status, array $requestData, ?string $ip | croncheck.pinged.{monitor.id} |
Events\CronCheckFailedEvent | CronMonitor $monitor, ?array $requestData | croncheck.failed.{monitor.id} |
Events\CronCheckRecoveredEvent | CronMonitor $monitor, Period $downtimePeriod, ?array $requestData | croncheck.recovered.{monitor.id} |
All three implement ShouldBroadcast and ShouldQueue. The channels are private.
Notifications
| Class | Constructor | Channels |
|---|---|---|
Notifications\Notifications\CronCheckFailed | CronMonitor $monitor | toMail(), toSlack(), toArray() |
Notifications\Notifications\CronCheckRecovered | CronMonitor $monitor, Period $downtimePeriod | toMail(), 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
| Signature | Description |
|---|---|
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
| Class | Factory | Rendered response |
|---|---|---|
Exceptions\InvalidPingStatus | receivedPingStatusIsInvalid(string $status) | HTTP 400 with the message. |
Exceptions\InvalidConfiguration | modelIsNotValid(string $className) | None. |
Exceptions\InvalidPeriod | startDateMustComeBeforeEndDate(CarbonInterface $start, CarbonInterface $end) | None. |
Exceptions\CannotSaveMonitor | alreadyExists(CronMonitor $monitor) | None. SyncFile throws it directly for an invalid cron expression. |
Source map
| Area | Path |
|---|---|
| Service provider and routes | src/CronMonitorServiceProvider.php, routes/api.php |
| Model and health check | src/Models/CronMonitor.php, src/Models/Traits/SupportsCronHealthCheck.php |
| Presenter | src/Models/Presenters/CronMonitorPresenter.php |
| Repository and collection | src/CronMonitorRepository.php, src/CronMonitorCollection.php |
| Controller | src/Http/Controllers/CronMonitorController.php |
| Commands | src/Commands/ProcessCronCheck.php, src/Commands/SyncFile.php |
| Events | src/Events/ |
| Notifications | src/Notifications/ |
| Config and migration | config/cron-monitor.php, database/migrations/create_cron_monitors_table.php.stub |