Scheduler#

Note

Not part of core. Install it separately:

composer require kinetis/scheduler

Runs jobs and commands on a recurring schedule declared in code. The scheduler works out which slots are due, claims each slot once in a MySQL or Postgres table, and pushes a job for it onto the Queue. The existing queue:work worker runs every slot. Any number of scheduler replicas can run side by side, so a rolling deploy never leaves a slot without a scheduler.

The scheduler needs:

  • the default database connection (DB_CONNECTION and the other DB_* keys, see Database) for its claim table;

  • a bound queue (QUEUE_CONNECTION, see Queue) and a running queue:work that lists each entry’s queue.

Without a queue, see Without a queue worker.

Create the table#

The package ships one Migrations stub per dialect for the kinetis_schedule_claims table:

vendor/kinetis/scheduler/resources/migrations/create_kinetis_schedule_claims_table.mysql.php.stub
vendor/kinetis/scheduler/resources/migrations/create_kinetis_schedule_claims_table.pgsql.php.stub

Copy the one matching the default connection into migrations/ and run vendor/bin/kinetis migrate, as Tables packages need describes.

Declare a schedule#

Put #[Schedule] on a job class to schedule that job:

namespace App\Jobs;

use Kinetis\Queue\Job;
use Kinetis\Scheduler\Attributes\Schedule;

#[Schedule(cron: '0 3 * * *', arguments: ['format' => 'csv'], queue: 'reports', maxAttempts: 3)]
final readonly class ExportOrders implements Job
{
    public function __construct(
        public string $format,
    ) {}

    public function handle(): void
    {
        // ...
    }
}

On any other class, command names the command to run, with arguments as its argv. The command can be your own or a package’s:

namespace App\Console;

use Kinetis\Scheduler\Attributes\Schedule;

#[Schedule(cron: '0 3 * * *', command: 'session:gc')]
#[Schedule(cron: '30 4 * * *', command: 'queue:prune', arguments: ['--hours=168'])]
#[Schedule(everySeconds: 30, command: 'app:sync-prices')]
final class Schedules
{
}

The attribute repeats. Every class under the application’s PSR-4 roots and installed packages’ scan roots is read for it, as for CLI commands.

Parameter

Meaning

cron

A five-field cron expression: minute, hour, day of month, month, day of week.

everySeconds

An interval from 1 to 86400 seconds, aligned to the Unix epoch: everySeconds: 300 runs at :00, :05, :10 past each hour.

timezone

The zone cron is evaluated in, UTC by default. Not allowed with everySeconds.

command

The command to run. Required on a class that is not a job; not allowed on a job.

arguments

A command’s argv as a list of strings, or a job’s constructor arguments by name.

queue

The queue the slot’s job is pushed to, default by default.

maxAttempts

The job’s maxAttempts (Queue’s “Retries and failures”).

Give exactly one of cron and everySeconds. Development discovers declarations on every boot; kinetis build compiles them into .kinetis-cache/compiled.php with the rest of discovery (Caching & AOT Compilation). Discovery refuses a declaration, naming the class and the rule, when:

  • it gives both cron and everySeconds, or neither;

  • the cron expression does not have exactly five fields (@daily and a sixth, year field are refused), has a field out of range, or never matches a date (0 0 30 2 *);

  • the timezone is unknown, or is given with everySeconds;

  • everySeconds is outside 1 to 86400;

  • the named command does not exist, takes no arguments but is given some, or is given anything but a list of strings;

  • a job class names a command, or another class names none;

  • the job’s constructor does not accept the arguments, or the job built from them cannot be queued (Queue’s “What a constructor argument can hold”);

  • the queue name or maxAttempts is one push() would refuse;

  • the same declaration appears twice.

Run the scheduler#

vendor/bin/kinetis schedule:work

schedule:work ticks once a second. Its first tick covers the last 60 seconds, so a slot that came due while no scheduler ran, such as during a restart, is still enqueued; every later tick covers the seconds since the one before. It holds no request state between ticks.

SIGTERM and SIGINT stop it between ticks with exit code 0, which needs ext-pcntl as for queue:work (Queue’s “Deploys and restarts”). A claim or push that fails ends it with exit code 1, so the supervisor restarts it, and the restarted scheduler’s first tick picks up the last minute again. It refuses to start when the bound queue is a SyncQueue, which would run every job inside the scheduler.

vendor/bin/kinetis schedule:run
vendor/bin/kinetis schedule:run --at=2026-12-02T03:00:00Z

schedule:run makes one pass over the 60 seconds up to now, or up to --at, an RFC 3339 time, and exits. It is for an external timer that starts it every minute (see Deploy).

vendor/bin/kinetis schedule:list

schedule:list prints each entry’s id, target, schedule, timezone and next due time in that timezone. It reads only the compiled declarations, so it needs no database or queue.

Delivery#

A due slot is enqueued at least once. With Queue (SQL) it is enqueued exactly once. The job then runs at least once, which is the queue’s own contract (Queue’s “A job can run more than once”), so a scheduled job or command must tolerate a second run.

For each due slot the scheduler runs one transaction on the default connection: it inserts the (entry, slot) row into kinetis_schedule_claims, deletes that entry’s claims more than a day older than the slot, pushes the job, and commits. When the insert hits the primary key, another scheduler has claimed the slot and this one skips it. With kinetis/queue-sql the job row is written on the claim’s own transaction, so the claim and the job commit together; the kinetis_queue_jobs table must then be on the default connection, which it is while QUEUE_CONNECTION_NAME is unset or default (Queue (SQL)). With another backend the push happens before the commit: a push that fails rolls the claim back, and a commit that fails after a push leaves the slot unclaimed, so the restarted scheduler’s first tick enqueues it again.

The scheduler logs one info line for each slot it enqueues.

A command entry is pushed as a Kinetis\Scheduler\ScheduledCommand job. The worker runs the command on the job’s request scope, with its output on the worker’s stdout and stderr. A non-zero exit code throws ScheduledCommandFailedException, and an exception from the command propagates, so the worker’s retries and Failed Jobs apply as they do to any job. The command does not run through the console runner, so a failure is reported by the queue’s job events (Events), not by CommandFailed. A ScheduledCommand whose entry the running build no longer declares fails with UnknownScheduleEntryException.

An entry’s id is a hash of its job class or command, arguments, expression and timezone. Changing any of them makes a new entry, and during a rollout the old and the new declaration can each enqueue a slot they share. queue and maxAttempts are not part of the id.

Missed slots#

Slots are never caught up. A slot not claimed within 60 seconds of its time is dropped: when a schedule:work tick falls more than a minute behind, it logs a warning naming the dropped range and continues from the last minute. Each schedule:run covers only the 60 seconds before it, so consecutive runs must start at most 60 seconds apart, and the slots in a longer gap are dropped without a log line. An external timer firing every minute meets that for cron entries; an everySeconds entry under a minute belongs under schedule:work.

Slot times come from each scheduler’s own clock, so keep replicas on NTP. A slot is the same row whichever replica claims it, so clock skew changes when a slot is enqueued, never how often.

Time zones and daylight saving#

everySeconds and a cron entry without timezone follow UTC and are unaffected by daylight saving. A cron entry with a timezone follows that zone’s wall clock, as dragonmantank/cron-expression evaluates it. For 0 2 * * * in Europe/Berlin:

  • on 29 March 2026, when clocks skip from 02:00 to 03:00, there is one slot, at 03:00 CEST;

  • on 25 October 2026, when 02:00 to 03:00 happens twice, there are two slots, at 02:00 CEST and at 02:00 CET.

Schedule a job that must run once a day outside the hour its zone skips or repeats, 02:00 to 03:00 in Europe/Berlin, or schedule it in UTC.

Deploy#

Long-running schedulers#

Run schedule:work as a service next to queue:work, with two replicas so a deploy or a crash leaves one running. On an image that declares a HEALTHCHECK for a web server, such as dunglas/frankenphp, disable it as for queue:work (Runtime Adapters’s “Command-line services on the same image”):

docker-compose.yml#
  scheduler:
    build:
      context: .
      dockerfile: docker/Dockerfile
    command: ["php", "vendor/bin/kinetis", "schedule:work"]
    stop_signal: SIGTERM
    deploy:
      replicas: 2
    healthcheck:
      disable: true
Kubernetes#
apiVersion: apps/v1
kind: Deployment
metadata:
  name: scheduler
spec:
  replicas: 2
  selector:
    matchLabels:
      app: scheduler
  template:
    metadata:
      labels:
        app: scheduler
    spec:
      containers:
        - name: scheduler
          image: registry.example.com/app:1.4.0
          command: ["php", "vendor/bin/kinetis", "schedule:work"]

An external timer#

Start schedule:run every minute instead, from any of the timers in CLI’s “Running commands on a schedule”: a Kubernetes CronJob with schedule: "* * * * *", EventBridge Scheduler with an ECS RunTask target, or a host crontab. A timer that fires twice, or two runs that overlap, still claim each slot once, so no flock, concurrencyPolicy: Forbid or single-host rule is needed.

A Lambda function running kinetis/bref-adapter cannot run the scheduler: the adapter accepts only HTTP events (Runtime Adapters’s “Running on AWS Lambda”).

Without a queue worker#

With a SyncQueue bound (Queue’s “Run jobs inline in development: SyncQueue”), schedule:run runs each due job or command inline, in its own process. For each slot it commits the claim first and only then runs the job, outside the claim’s transaction, so a slot runs at most once: a job that throws is logged as an error, is not retried, and makes schedule:run exit 1 after the remaining slots have run. SyncQueue ignores queue and maxAttempts.

An application with no queue at all can also keep running its commands from the external timers in CLI’s “Running commands on a schedule”, without this package.

One run at a time#

A worker runs one job at a time. To keep an entry from overlapping itself, give it a queue of its own and run exactly one worker that lists that queue, as Application Recipes’s “At most N concurrent jobs of a type” shows:

#[Schedule(cron: '*/5 * * * *', command: 'app:rebuild-search-index', queue: 'search-index')]
vendor/bin/kinetis queue:work --queue=search-index

The claim table#

Each claim removes its entry’s claims more than a day older than the slot it claims, so the table holds about a day of slots per entry. An entry removed from the code leaves its last day of rows behind; delete them by entry_id once its jobs have drained. slot_at and claimed_at hold UTC wall-clock times, as Queue (SQL)’s “Stored times” describes for the queue’s own columns.

See also#

  • Queue — workers, retries and the at-least-once contract every scheduled job runs under.

  • Queue (SQL) — the backend that makes enqueueing exactly-once.

  • CLI — writing commands, and external schedulers.

  • Application Recipes — “Work that falls due”, for due times stored in data.