Scheduling
Most applications need recurring work: pruning expired sessions, sending a daily report, syncing a feed every five minutes. PAM’s approach is to run that work inside the cluster you already have. When you start a cluster with task workers, the PAM master sends them one tick per second. A scheduler listens for those ticks and runs whatever is due, so you need no system cron entry and no separate long-running process.
PAM itself provides the tick, not the cron syntax. The scheduler with frequencies, overlap protection and distributed locks lives in Nimbus, PAM’s HTTP framework. Laravel applications keep using Laravel’s scheduler.
| You use | Recurring work runs through |
|---|---|
| Nimbus | Nimbus\Scheduling on task-worker ticks (this page) |
| Laravel | pam artisan schedule:work or schedule:run from cron (Artisan & Workers) |
| The raw runtime | Your own handler for the tick task (see Background Tasks) |
Defining a Schedule
Section titled “Defining a Schedule”The scheduler needs the cron expression parser:
pam composer require dragonmantank/cron-expressionBuild the schedule where you build your application, and hand it to the application’s background runner:
<?php
declare(strict_types=1);
use Nimbus\App;use Nimbus\Scheduling\Schedule;use Nimbus\Scheduling\Scheduler;
$root = dirname(__DIR__);
$app = new App();$app->routes($root . '/routes.php');
$schedule = new Schedule(timezone: 'UTC', container: $app->container());
$schedule->call(static function (): void { error_log('heartbeat ' . gmdate('H:i:s'));})->everyMinute()->name('heartbeat');
$app->background()->scheduler(new Scheduler($schedule));
return $app;Then start the cluster with at least one task worker:
pam start index.php --workers 4 --task-workers 1The task worker logs the heartbeat on the first tick of every minute:
heartbeat 18:51:40Kinds of Tasks
Section titled “Kinds of Tasks”Schedule has three ways to add a task. Each returns a ScheduledTask you configure with a frequency.
// A callback$schedule->call(fn () => $sessions->pruneExpired())->everyFiveMinutes()->name('prune-sessions');
// A queued job (requires a JobDispatcher passed to the Schedule)$schedule->job(SendDailyReport::class)->dailyAt('07:00');
// A console command of the application (requires the container)$schedule->command('reports:send', ['--force'])->hourly()->withoutOverlapping();call()runs the callback on the task worker. Callbacks are named after their file and line unless you callname(), so name them whenever that is not stable across deploys.job()dispatches a job to the queue you passed as theSchedule’s first argument instead of running it inline. Pass a class name (built through the container on each run) or an instance.command()runs a Nimbus console command. A non-zero exit code fails the task with the command’s output.
Frequencies
Section titled “Frequencies”| Method | Runs |
|---|---|
cron('*/10 * * * *') |
Any five-field expression or macro such as @hourly |
everyMinute(), everyTwoMinutes(), everyFiveMinutes(), everyTenMinutes(), everyFifteenMinutes(), everyThirtyMinutes() |
At those intervals |
hourly(), hourlyAt(17) |
Every hour, at minute 0 or the given minute |
daily(), dailyAt('03:00') |
Every day |
weekly(), weeklyOn(1, '08:00') |
Every week (day 0 is Sunday) |
monthly(), monthlyOn(1, '00:00') |
Every month |
timezone('America/Sao_Paulo') |
Evaluate this task in another time zone |
The schedule-wide time zone comes from the Schedule constructor. Without one (and without a per-task timezone()), expressions are evaluated in UTC, not in PHP’s date.timezone.
Running Once Across Workers and Servers
Section titled “Running Once Across Workers and Servers”Before running a due task, the scheduler claims a lock for that task and that minute. The lock implementation decides how far “once” reaches:
| Lock | Scope |
|---|---|
MemorySchedulerLock (default) |
One process |
CacheSchedulerLock |
Whatever the cache repository shares |
RedisSchedulerLock |
Every process using the same Redis |
DatabaseSchedulerLock |
Every process using the same database (scheduler_locks table) |
Use Redis or the database whenever more than one task worker, or more than one server, runs the scheduler:
use Nimbus\Redis\RedisPool;use Nimbus\Scheduling\RedisSchedulerLock;use Nimbus\Scheduling\Scheduler;
$app->background()->scheduler( new Scheduler($schedule, new RedisSchedulerLock(RedisPool::fromEnvironment())),);withoutOverlapping() also skips a run while the previous run of the same task still holds its lock. The lock expires after 1,440 minutes by default (pass a smaller number for short tasks) so that a crashed worker cannot block a task forever.
Failed tasks are logged through Pam\Observability\Telemetry with the task name and exception class, and do not stop the other tasks of the same tick.
Queues on the Same Ticks
Section titled “Queues on the Same Ticks”The background runner can also drain a job queue on every tick. Each tick runs the scheduler first, then processes at most 100 jobs or 0.8 seconds of work so the next tick is not delayed:
$app->background() ->scheduler(new Scheduler($schedule, $lock)) ->queue($worker); // a Nimbus\Jobs\JobWorker over RedisJobQueue or DatabaseJobQueueQueue setup (codecs, retries, failed jobs) is covered in the Nimbus README.
Without Task Workers
Section titled “Without Task Workers”On a platform where you cannot run task workers, run the same background runner as its own process. It ticks every second, sleeps with Pam\Async\delay() under PAM, and stops on SIGTERM or SIGINT:
<?php
declare(strict_types=1);
/** @var \Nimbus\App $app */$app = require __DIR__ . '/../bootstrap/app.php';
$app->background()->run(intervalSeconds: 1.0);pam bin/background.phpKeep that process alive with your supervisor or pam up. Use a distributed lock if it runs next to task workers that also tick.
What PAM Does Not Provide
Section titled “What PAM Does Not Provide”- There is no cron syntax, task registry or
pam schedulecommand in the runtime itself. - Ticks are at-most-once per second per task pool and are skipped while the previous tick is still running. A tick that falls inside a deploy or restart is lost; the scheduler catches up only for the current minute, not for missed ones.
- Scheduled work is not durable. If a task must happen even when the cluster is down, dispatch a job to a durable queue and let the queue retry it.
Next Steps
Section titled “Next Steps”- Background Tasks: task workers and process pools.
- Clustering:
--task-workersand otherpam startoptions. - Observability & Diagnostics: watch the
taskspool inpam top.