luiswagener / symfony-cron-bundle
Scheduled background jobs for Symfony applications, with persistent tracking of execution history.
Package info
github.com/luiswagener/symfony-cron
Type:symfony-bundle
pkg:composer/luiswagener/symfony-cron-bundle
Requires
- php: >=8.2
- doctrine/orm: ^3.0
- dragonmantank/cron-expression: ^3.3
- symfony/config: ^6.4 || ^7.0 || ^8.0
- symfony/console: ^6.4 || ^7.0 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.0 || ^8.0
- symfony/framework-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- doctrine/doctrine-bundle: ^2.13 || ^3.0
- phpunit/phpunit: ^10.5 || ^11.0 || ^12.0 || ^13.0
- symfony/phpunit-bridge: ^6.4 || ^7.0 || ^8.0
- symfony/yaml: ^6.4 || ^7.0 || ^8.0
README
Cron framework for Symfony applications. Register cron jobs with a single attribute, run them from a single minutely server cron, and persist last-run timestamps in the database.
Installation
composer require luiswagener/symfony-cron-bundle
Register the bundle in config/bundles.php:
Luiswagener\SymfonyCronBundle\SymfonyCronBundle::class => ['all' => true],
Create the database table:
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate
Doctrine must have auto_mapping: true (Symfony default) so the bundle entity is discovered automatically.
Server cron
Configure one minutely cron job on your server:
* * * * * /usr/bin/php /path/to/your-app/bin/console cron:run
Defining cron jobs
Place a class anywhere under src/ and annotate it with #[AsCronJob]. No service registration or tagging is required — standard Symfony autowiring picks it up automatically.
use Luiswagener\SymfonyCronBundle\Attribute\AsCronJob; #[AsCronJob('hourly')] final class CleanupExpiredTokensCron { public function __construct(private TokenRepository $tokens) {} public function __invoke(): void { $this->tokens->deleteExpired(); } }
Supported intervals
- Presets:
minutely,hourly,daily,weekly,monthly,yearly - Cron expressions:
0 * * * *,*/5 * * * * - Human-readable:
1 minute,every 5 minutes,1 hour
Command usage
php bin/console cron:run php bin/console cron:run "App\\Cron\\CleanupExpiredTokensCron" php bin/console cron:run --force php bin/console cron:run "App\\Cron\\CleanupExpiredTokensCron" --force
Skipping execution
Throw CronExecutionSkippedException to skip a job without updating last_run:
use Luiswagener\SymfonyCronBundle\Exception\CronExecutionSkippedException; public function __invoke(string $scope): void { if ($scope !== Cron::SCOPE_CLI) { throw new CronExecutionSkippedException(); } }
License
MIT