pantono / scheduler
Scheduler Module for pantono
Requires
- php: >=8.4
- pantono/config: ^1.0
- pantono/contracts: ^1.0
- pantono/core: ^1.0
- pantono/database: ^1.0
- pantono/phinx: ^0.16
- pantono/utilities: ^1.0
- symfony/console: ^8.0
- symfony/event-dispatcher: ^8.0
- symfony/yaml: ^8.0
Requires (Dev)
- phpstan/phpstan: ^2
- phpunit/phpunit: ^13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Cron-style scheduled jobs for Pantono applications. Every module (and the application) lists its recurring
console commands in a conf/schedule.yml; one schedule:run call a minute runs whatever is due.
Install
composer require pantono/scheduler- Run the migrations (adds
scheduled_job_run). - Trigger
schedule:runonce a minute (see Triggering).
Defining jobs
Put a schedule.yml in a module's or the application's conf/ folder:
update_product_counts: expression: '0 * * * *' # five-field cron, or @hourly, @daily, @weekly, @monthly, @yearly command: 'products:update-counts' # any console command arguments: ['--quiet'] # optional timeout: 600 # optional, seconds (default 3600); the run is stopped after this without_overlapping: true # optional (default true): skip while the last run is still going enabled: true # optional (default true) description: Refresh category product counts # optional, shown by schedule:list
Files are read in the same order as the rest of Pantono's config, so the application's file wins. Unlike other config types, entries are merged key by key, so the application can change one part of a module's job without repeating it:
# conf/schedule.yml in the application schedule_prune_runs: enabled: false # switch a module's job off subscriptions_process: expression: '0 6 * * *' # or just move it
A schedule.<env>.yml next to schedule.yml is applied after it, for per-environment changes.
${VAR} and ${VAR:default} in values are read from the environment.
Cron fields accept *, numbers, ranges (1-5), steps (*/15, 0-30/10), lists (5,35) and names
(jan-dec, sun-sat). Day of week is 0-7, where 0 and 7 are Sunday. As in cron, if both day of month
and day of week are restricted, a day matching either runs.
Commands
| Command | What it does |
|---|---|
schedule:run [--dry-run] |
Runs the jobs due this minute. Exits non-zero if any failed. |
schedule:list |
Every job with its expression, next run, last run and last status. |
schedule:prune [--days=30] |
Deletes finished runs older than N days. The module schedules this daily at 03:15. |
How runs are kept safe
- Each job's command runs in its own PHP process (
php bin/console <command> <arguments>), so a fatal error, a memory leak or leftover state in one job can't affect another. Setscheduler.consoleinconfig.ymlif the console script isn'tbin/console. - Before a job runs, a row is inserted into
scheduled_job_runfor the job and the minute. A unique index on(job_name, scheduled_for)means a secondschedule:runin the same minute, an overlapping cron run or a second server finds the row and skips the job. Each due job runs once per minute, however many ticks. - With
without_overlapping(the default), a job whose previous run is stillrunningis not started again. A run older than the job's timeout counts as dead (the process was killed), so it stops blocking. - Every run records its status (
succeeded,failed,timed_out), exit code, the last 64 KB of output and error output, and the host.
Events
| Event | When |
|---|---|
PreScheduledJobRunEvent |
Before a due job runs. Call skip() to leave it out this minute (nothing is recorded). |
ScheduledJobFailedEvent |
After a run that exited non-zero or timed out. Listen to this to alert someone. |
PostScheduledJobRunEvent |
After every run, with the JobResult and run id. |
Triggering
Something has to call schedule:run every minute. No separate service is needed.
Cron (recommended). One line on the host or container that already runs the queue workers:
* * * * * cd /path/to/app && php bin/console schedule:run >> cache/schedule.log 2>&1
or a systemd timer with OnCalendar=*-*-* *:*:00 running the same command.
From a long-running process. A queue worker (or any loop) can call Scheduler::tick() on each
iteration. It runs the due jobs at most once per minute per process and returns null otherwise, and the
run claims stop several workers running a job twice. This needs no cron at all, but scheduling then stops
whenever the workers are down, and a busy worker delays the tick until it next loops:
$scheduler = $locator->loadDependency('@Scheduler'); while (true) { $scheduler->tick(); // ... handle the next message, with a receive timeout well under a minute }
Tests
vendor/bin/phpunit runs against SQLite in memory; vendor/bin/phpstan analyse is clean at level 8.