Cron

Register per-minute crons by module and name, run them one by one or by name, and log each run's start, end, output and any error. Also runs a callback within a bounded time window, and simulates second-resolution crons across a run.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes
  • Cron - run registered crons, and run a callback within a time window.
  • CronCommand - the command routing for the cron command.
  • CronConfig - register the per-minute crons a module provides.
  • CronLogData - immutable record of a single cron run.
  • CronLogDatabase - write and read cron run log rows.
  • CronSimulate - simulate second-resolution crons over a fixed run.

Cron

Runs the crons registered through CronConfig, and provides helpers to run a callback for up to, or for at least, a given number of seconds.

Run

Cron::Run(): void

Register the cron command with its minute and named subcommands, then start the command server. The minute subcommand launches each registered cron as a separate named run; the named subcommand runs a single cron by its name.

Returns void

RunCallback

Cron::RunCallback(string $moduleName, string $cronName, callable $callback): void

Run a single cron callback, logging its start and end. Output is captured and any thrown error is recorded as the run's error message.

NameTypeDescription
$moduleNamestringThe module the cron belongs to.
$cronNamestringThe name of the cron.
$callbackcallableThe work to run.

Returns void

RunForAtLeast

Cron::RunForAtLeast(float $secondsToRun, callable $callback): void

Run the callback once, then sleep for any remaining time so the call takes at least the given number of seconds.

NameTypeDescription
$secondsToRunfloatThe least number of seconds the call should take.
$callbackcallableThe work to run once.

Returns void

RunForUpTo

Cron::RunForUpTo(float $secondsToRun, callable $callback): void

Call the callback repeatedly until it returns a falsey value or the given number of seconds has elapsed, whichever comes first.

NameTypeDescription
$secondsToRunfloatThe most number of seconds to keep calling for.
$callbackcallableCalled each iteration; return a falsey value to stop early.

Returns void

CronCommand

The command routing for the cron command, with a minute subcommand that starts every registered cron and a named subcommand that runs one cron by name.

Functions

Route

CronCommand::Route(): void

Define the cron command's subcommands. The minute subcommand starts each registered cron as its own named process; the named subcommand takes a cron name argument and runs that cron's callback.

Returns void

CronConfig

Register the per-minute crons a module provides. Registered crons are held in the shared crons map, keyed by cron name.

Properties

PropertyTypeDescription
CronConfig::$cronsarrayThe registered crons, keyed by cron name; each holds the module name and callback.
Functions

Minute

CronConfig::Minute(string $moduleName, string $cronName, callable $callback): void

Register a cron to run each minute, storing its module name and callback under the cron name.

NameTypeDescription
$moduleNamestringThe module the cron belongs to.
$cronNamestringThe name of the cron, used as its key.
$callbackcallableThe work to run each minute.

Returns void

CronLogData

Immutable record of a single cron run. Fields and constructor only.

Properties

PropertyTypeDescription
$data->idintThe log row id (readonly).
$data->moduleNamestringThe module the cron belongs to (readonly).
$data->cronNamestringThe name of the cron (readonly).
$data->startTimeintThe start time as a Unix timestamp (readonly).
$data->endTimeint|nullThe end time as a Unix timestamp, or null while running (readonly).
$data->outputstring|nullThe captured output, or null (readonly).
$data->hasErrorboolWhether the run recorded an error (readonly).
$data->errorMsgstring|nullThe error message, or null (readonly).

__construct

new CronLogData(int $id = 0, string $moduleName = '', string $cronName = '', int $startTime = 0, int|null $endTime = null, string|null $output = null, bool $hasError = false, string|null $errorMsg = null)

Create a cron log record from its fields.

NameTypeDescription
$idintThe log row id.
$moduleNamestringThe module the cron belongs to.
$cronNamestringThe name of the cron.
$startTimeintThe start time as a Unix timestamp.
$endTimeint|nullThe end time as a Unix timestamp, or null.
$outputstring|nullThe captured output, or null.
$hasErrorboolWhether the run recorded an error.
$errorMsgstring|nullThe error message, or null.

Returns CronLogData

CronLogDatabase

Write and read the cron run log rows, recording a run's start and then its end with output and any error.

Functions

End

CronLogDatabase::End(int $cronLogId, string|null $output, string|null $optionalErrorMessage): void

Complete a log row, setting its end timestamp, captured output, and error state and message.

NameTypeDescription
$cronLogIdintThe id of the log row to complete.
$outputstring|nullThe captured output, or null.
$optionalErrorMessagestring|nullThe error message, or null when there was no error.

Returns void

fromRow

CronLogDatabase::fromRow(stdClass $row): CronLogData

Build a CronLogData from a database row.

NameTypeDescription
$rowstdClassThe database row to convert.

Returns CronLogData The cron run record.

Start

CronLogDatabase::Start(string $moduleName, string $cronName): int

Insert a new log row for a cron run, recording its module name, cron name, date and start timestamp.

NameTypeDescription
$moduleNamestringThe module the cron belongs to.
$cronNamestringThe name of the cron.

Returns int The id of the new log row.

CronSimulate

Run for a fixed number of seconds, calling a per-second callback at the correct times, so second-resolution crons can be simulated within a single process. Individual crons are defined with a regularity and an offset so they can be spread across the window.

Constants

ConstantValueDescription
CronSimulate::PROCESS_SKIP_NONE0Run every second regardless of how long callbacks take; a slow callback runs the next immediately.
CronSimulate::PROCESS_SKIP_SECONDS1When a callback runs long, move to the next second; seconds may be skipped entirely.
CronSimulate::PROCESS_SKIP_OVERRUN2Like skip none, but stop once the run time has passed and skip the remaining crons.
CronSimulate::PROCESS_SKIP__MAX2The highest valid skip mode value.

Properties

PropertyTypeDescription
CronSimulate::$currentTimestampintThe timestamp of the second currently being processed.

Cron

CronSimulate::Cron(int $regularitySeconds, int $offsetSeconds, callable $callback): void

Call the callback when the current timestamp matches the offset within the regularity, giving a cron that fires every regularity seconds at a chosen point in that cycle.

NameTypeDescription
$regularitySecondsintHow many seconds between calls.
$offsetSecondsintThe point within the regularity at which to fire, from 0 to regularity minus one.
$callbackcallableThe work to run when the cron fires.

Returns void

Process

CronSimulate::Process(int $startTimestamp, int $runForSeconds, int $skipMode, callable $callback): void

Run for the given number of seconds, calling the callback once per second with the current timestamp set, sleeping in real time to stay on schedule. The skip mode decides what happens when a callback runs longer than a second.

NameTypeDescription
$startTimestampintThe timestamp of the first second, for example the current time rounded to the minute.
$runForSecondsintHow many seconds to run for, for example 60.
$skipModeintOne of the CronSimulate PROCESS_SKIP constants.
$callbackcallableCalled each second; it calls CronSimulate::Cron for each simulated cron.

Returns void

processSeconds

CronSimulate::processSeconds(int $startTimestamp, int $runForSeconds, bool $stopOnTime, bool $skipSecondsIfLate, callable $callback): void

Loop over each second of the run, sleeping while ahead of schedule and calling the callback with the current timestamp set, according to the given behaviour flags.

NameTypeDescription
$startTimestampintThe timestamp of the first second.
$runForSecondsintHow many seconds to run for.
$stopOnTimeboolTrue to stop once the real elapsed time exceeds the run time.
$skipSecondsIfLateboolTrue to call the callback only while on or ahead of schedule, skipping late seconds.
$callbackcallableCalled each second with the current timestamp set.

Returns void