lingoda / cron-bundle
Lingoda cron bundle
Installs: 41 533
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 14
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- ext-pcntl: *
- ext-zip: *
- ext-zlib: *
- doctrine/orm: ^2.10
- dragonmantank/cron-expression: ^3.3
- lorisleiva/cron-translator: ^0.4.5
- nesbot/carbon: ^2.71 || ^3.0
- psr/log: ^1.1 || ^3.0
- symfony/cache: ^5.4 || ^6.0 || ^7.0
- symfony/config: ^5.4 || ^6.0 || ^7.0
- symfony/console: ^5.4 || ^6.0 || ^7.0
- symfony/dependency-injection: ^5.4 || ^6.0 || ^7.0
- symfony/http-kernel: ^5.4 || ^6.0 || ^7.0
- symfony/lock: ^5.4 || ^6.0 || ^7.0
- symfony/messenger: ^5.4 || ^6.0 || ^7.0
- webmozart/assert: ^1.11
Requires (Dev)
- nyholm/symfony-bundle-test: ^2.0
- phpspec/phpspec: ^7.5
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-doctrine: ^1.3
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-symfony: ^1.3
- phpstan/phpstan-webmozart-assert: ^1.2
- phpunit/phpunit: ^9.5
- rector/rector: ^1.0
- symfony/phpunit-bridge: ^5.4
Conflicts
- doctrine/persistence: <1.3
- symfony/symfony: *
This package is auto-updated.
Last update: 2025-03-14 09:33:02 UTC
README
This bundle allows scheduling of jobs that will execute in a different process than the process that triggered them
How to use
Either extend the ScheduleBaseCronJob
abstract class or implement the CronJobInterface
interface. These will automatically be recognized by your Symfony application. Then there's nothing left to do than execute the command bin/console lg:cron:trigger-due-jobs
. This will send a message on the application's messenger bus for each recognized cron job that is due. Usually, this means that a message will be put on some sort of a queue which will then be picked up by a handler which will actually run the job.
When implementing a cron job, you shall not expect the cron to actually run at the scheduled times.
- The queue might be unavailable or overloaded, and you can't predict the delay
- The actual previous run time will be passed to your run method. Use it!
- The job might be run manually. Or it might have been run manually previously. Even multiple times.
Triggering individual jobs
bin/console lg:cron:run-job App\\Cron\\SomeCronJobImplementation
Examples
Extending ScheduleBaseCronJob
class MyCronJob extends ScheduleBaseCronJob { protected function getSchedule(): Schedule { // this job will execute every hour at the 30th minute return Schedule::everyHour(30); } public function run(): void { // do the job here } }
Implementing CronJobInterface
class MyCronJob implements CronJobInterface { public function run(): void { // do the job } public function shouldRun(DateTimeInterface $lastFinishTime = null): bool { // implement your own logic for determining if a job should // at a particular time or not } public function getLockTTL(): float { // before running, an expiring lock is acquired for each job to prevent // a job of the same type to run at the same time; with this method // the job can specify a custom expiry period in seconds for the lock return 30.0; } public function __toString(): string { // Here comes the cron expression as string, e.g. 30 1 * * * return '30 1 * * *'; } }