adachsoft / scheduler-symfony-bundle
Symfony bundle for discovering and running scheduled jobs with adachsoft/scheduler.
Package info
gitlab.com/a.adach/scheduler-symfony-bundle
Type:symfony-bundle
pkg:composer/adachsoft/scheduler-symfony-bundle
Requires
- php: ^8.3
- adachsoft/lease-lock: ^0.1
- adachsoft/scheduler: ^0.2
- adachsoft/scheduler-contracts: ^0.1
- psr/container: ^1.1 || ^2.0
- symfony/config: ^7.4 || ^8.0
- symfony/console: ^7.4 || ^8.0
- symfony/dependency-injection: ^7.4 || ^8.0
- symfony/http-kernel: ^7.4 || ^8.0
Requires (Dev)
- adachsoft/php-code-style: *
- friendsofphp/php-cs-fixer: *
- phpstan/phpstan: *
- phpunit/phpunit: ^12.5
- rector/rector: *
- symfony/framework-bundle: ^7.4 || ^8.0
- symplify/phpstan-rules: *
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The adachsoft/scheduler-symfony-bundle package integrates the scheduler with Symfony service discovery, dependency injection, and console commands.
Installation and registration
Install the bundle and its scheduler dependencies with Composer:
composer require adachsoft/scheduler-symfony-bundle
Register the bundle in config/bundles.php when Symfony Flex does not register it automatically:
return [
AdachSoft\SchedulerSymfonyBundle\SchedulerBundle::class => ['all' => true],
];
Scheduled jobs are discovered from services carrying the #[AsScheduledJob] attribute. Make sure the job's namespace is included in Symfony service discovery and that the service is autoconfigured.
Configuration
The bundle uses the scheduler configuration alias. All supported keys and defaults are shown below:
scheduler:
state_dir: '%kernel.project_dir%/var/scheduler'
executor: process # process or in_process
console_path: '%kernel.project_dir%/bin/console'
php_binary: null # null resolves PHP_BINARY when the worker container creates the executor
worker:
max_sleep_seconds: 60
process executes jobs in child processes and is the default. in_process runs jobs in the worker process. php_binary may be set to an explicit PHP CLI binary; when it is null, the running worker's PHP_BINARY is used at runtime. console_path must point to the Symfony console script used by child processes. The bundle stores job state under <state_dir>/state, lease files under <state_dir>/leases, and run history under <state_dir>/history.
Scheduler 0.2.0 lock storage
adachsoft/scheduler 0.2.0 replaces file-based job locks with owner-bound leases. This bundle stores lease files in <state_dir>/leases; files in the previous <state_dir>/locks directory are no longer used and can be removed manually after upgrading. A unique lock owner ID is generated at runtime for each PHP process.
Defining a job
A job implements ScheduledJobInterface and defines exactly one schedule with every or dailyAt:
<?php
declare(strict_types=1);
namespace App\Scheduler;
use AdachSoft\SchedulerContracts\Job\JobResultDto;
use AdachSoft\SchedulerContracts\Job\JobRunContextDto;
use AdachSoft\SchedulerContracts\Job\ScheduledJobInterface;
use AdachSoft\SchedulerSymfonyBundle\Attribute\AsScheduledJob;
#[AsScheduledJob(
id: 'app.pricing_sync',
dailyAt: '03:00',
timezone: 'UTC',
retryDelaySeconds: 3600,
overdueGraceSeconds: 7200,
failureThreshold: 3,
description: 'Synchronize model pricing.',
)]
final class PricingSyncJob implements ScheduledJobInterface
{
public function run(JobRunContextDto $context): JobResultDto
{
// Synchronize pricing and return a concise result for scheduler history.
return new JobResultDto('Pricing synchronized.');
}
}
Console commands
scheduler:workruns the long-lived worker.--max-iterations=Nlimits a worker invocation, and--max-sleep=Noverrides the maximum seconds between polls.scheduler:tickruns one scheduler tick and exits with code 1 when a run fails.scheduler:run-job <id>runs one job manually. An unknown job exits with code 2; a non-successful run exits with code 1.scheduler:status [--format=table|json]displays health and scheduling state. It exits with code 1 when any job is unhealthy.scheduler:history [--job=ID] [--outcome=VALUE] [--limit=N] [--format=table|json]lists recent job runs. The default limit is 20.scheduler:execute-job <id>is an internal child-process command and is hidden from the normal command list.
Running the worker in Docker
Run the worker as a background process in an application container or, preferably, as a dedicated service container:
services:
scheduler-worker:
image: your-application-image
command: ['php', 'bin/console', 'scheduler:work']
restart: unless-stopped
Keep the worker process supervised by the container runtime. Do not start a second worker in the same container unless the scheduler state and lock storage are shared safely.
Health checks
Use JobStatusProviderInterface in the application's /health endpoint to include scheduler status in the response. Return an unhealthy HTTP status when a job reports an unhealthy state:
$statuses = $jobStatusProvider->all();
$healthy = true;
foreach ($statuses as $status) {
$healthy = $healthy && $status->isHealthy();
}
return $healthy ? new Response('OK') : new Response('Scheduler unhealthy', 503);
A container health check can use the command's exit code directly:
healthcheck:
test: ['CMD', 'php', 'bin/console', 'scheduler:status', '--format=json']
interval: 30s
timeout: 5s
retries: 3