Skip to main content

Scheduler Plugin

Scheduler for executing tasks using second-based intervals, date-times, ISO 8601 durations, relative intervals, or cron expressions.

Installation

$ composer require phpstreamserver/scheduler

Usage Example

server.php
<?php

require __DIR__ . '/vendor/autoload.php';

use PHPStreamServer\Core\Server;
use PHPStreamServer\Plugin\Scheduler\SchedulerPlugin;
use PHPStreamServer\Plugin\Scheduler\Worker\ScheduledWorker;

$server = new Server();

$server->addPlugin(
new SchedulerPlugin(),
);

$server->addWorker(
new ScheduledWorker(
name: 'Scheduled worker',
schedule: '*/1 * * * *',
onStart: function (ScheduledWorker $worker): void {
// process
},
),
);

exit($server->run());

Plugin Configuration

🧩 SchedulerPlugin

Plugin class: SchedulerPlugin
This plugin does not have any specific configurable parameter.

Worker Configuration

⚙️ ScheduledWorker

Worker class: ScheduledWorker
This worker type is designed to execute tasks periodically.
The schedule parameter accepts:

  • A numeric string representing seconds (for example, '60')
  • An ISO8601 datetime format (for example, 2026-01-01T00:00:00Z)
  • An ISO8601 duration format (for example, PT1M)
  • A relative date format (for example, 1 minute)
  • A cron expression (for example, */1 * * * *)
ParameterTypeDefaultDescription
namestring|nullnullOptional. Worker name. Defaults to scheduled worker <id>.
schedulestring1 minuteOptional. Schedule in one of the formats described above.
jitterint0Optional. Maximum random delay, in seconds, added to the scheduled time. Set to 0 to disable.
userstring|nullnullOptional. Unix user name or user ID (UID).
groupstring|nullnullOptional. Unix group name or group ID (GID).
onStartClosure|nullnullOptional. Closure(ScheduledWorker): void callback executed when the scheduled process starts.

Each run executes in a separate child process. Runs never overlap. If a task is still running at its next scheduled time, that run is skipped and the following run is scheduled.

Commands

▶️ GetWorkersCommand

Command class: GetWorkersCommand
Retrieves metadata for workers registered with the scheduler.