More documentation for cronjob

This commit is contained in:
Julien Fastré 2024-07-18 10:09:12 +02:00
parent 643156f822
commit 97239ada84
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 13 additions and 6 deletions

View File

@ -39,9 +39,12 @@ Implements a :code:`Chill\MainBundle\Cron\CronJobInterface`. Here is an example:
use Chill\MainBundle\Entity\CronJobExecution; use Chill\MainBundle\Entity\CronJobExecution;
use DateInterval; use DateInterval;
use DateTimeImmutable; use DateTimeImmutable;
use Symfony\Component\Clock\ClockInterface;
class MyCronJob implements CronJobInterface class MyCronJob implements CronJobInterface
{ {
function __construct(private ClockInterface $clock) {}
public function canRun(?CronJobExecution $cronJobExecution): bool public function canRun(?CronJobExecution $cronJobExecution): bool
{ {
// the parameter $cronJobExecution contains data about the last execution of the cronjob // 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 // 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')) return $cronJobExecution->getLastStart() < $now->sub(new DateInterval('P1D'))
&& in_array($now->format('H'), self::ACCEPTED_HOURS, true) && 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'; return 'arbitrary-and-unique-key';
} }
public function run(): void public function run(array $lastExecutionData): void
{ {
// here, we execute the command // 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 ? How are cron job scheduled ?

View File

@ -24,9 +24,8 @@ interface CronJobInterface
* *
* If data is returned, this data is passed as argument on the next execution * 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 * @param array<string|int, int|float|string|bool|array<int|string, int|float|string|bool>> $lastExecutionData the data which was returned from the previous execution
* * @return array<string|int, int|float|string|bool|array<int|string, int|float|string|bool>>|null optionally return an array with the same data than the previous execution
* @return array|null optionally return an array with the same data than the previous execution
*/ */
public function run(array $lastExecutionData): ?array; public function run(array $lastExecutionData): ?array;
} }