diff --git a/docs/source/development/cronjob.rst b/docs/source/development/cronjob.rst index df72fa922..bda32b5c8 100644 --- a/docs/source/development/cronjob.rst +++ b/docs/source/development/cronjob.rst @@ -39,9 +39,12 @@ Implements a :code:`Chill\MainBundle\Cron\CronJobInterface`. Here is an example: use Chill\MainBundle\Entity\CronJobExecution; use DateInterval; use DateTimeImmutable; + use Symfony\Component\Clock\ClockInterface; class MyCronJob implements CronJobInterface { + function __construct(private ClockInterface $clock) {} + public function canRun(?CronJobExecution $cronJobExecution): bool { // the parameter $cronJobExecution contains data about the last execution of the cronjob @@ -56,7 +59,7 @@ Implements a :code:`Chill\MainBundle\Cron\CronJobInterface`. Here is an example: // this cron job should be executed if the last execution is greater than one day, but only during the night - $now = new DateTimeImmutable('now'); + $now = $clock->now(); return $cronJobExecution->getLastStart() < $now->sub(new DateInterval('P1D')) && in_array($now->format('H'), self::ACCEPTED_HOURS, true) @@ -69,10 +72,15 @@ Implements a :code:`Chill\MainBundle\Cron\CronJobInterface`. Here is an example: return 'arbitrary-and-unique-key'; } - public function run(): void + public function run(array $lastExecutionData): void { // here, we execute the command - } + + // we return execution data, which will be served for next execution + // this data should be easily serializable in a json column: it should contains + // only int, string, etc. Avoid storing object + return ['last-execution-id' => 0]; + } } How are cron job scheduled ? diff --git a/src/Bundle/ChillMainBundle/Cron/CronJobInterface.php b/src/Bundle/ChillMainBundle/Cron/CronJobInterface.php index 050b777e9..9194e6c53 100644 --- a/src/Bundle/ChillMainBundle/Cron/CronJobInterface.php +++ b/src/Bundle/ChillMainBundle/Cron/CronJobInterface.php @@ -24,9 +24,8 @@ interface CronJobInterface * * If data is returned, this data is passed as argument on the next execution * - * @param array $lastExecutionData the data which was returned from the previous execution - * - * @return array|null optionally return an array with the same data than the previous execution + * @param array> $lastExecutionData the data which was returned from the previous execution + * @return array>|null optionally return an array with the same data than the previous execution */ public function run(array $lastExecutionData): ?array; }