Skip to content

Commit

Permalink
bug symfony#50818 [Scheduler] Fix PeriodicalTrigger from argument f…
Browse files Browse the repository at this point in the history
…or stateful run dates (StanJansen)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

[Scheduler] Fix `PeriodicalTrigger` from argument for stateful run dates

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix symfony#50669
| License       | MIT
| Doc PR        | -

Summary: Unless you provide the `from` argument (with a date in the past), caching will not work for the `PeriodicalTrigger`.

If the `from` argument is not passed to the `RecurringMessage::every` method, it will, fallback to `new \DateTimeImmutable()`. If you use a stateful schedule and you restart the consumer (either manually or using a time limit), the cached last-executed date will always be overridden by the moment you restart the scheduler due to `if ($this->from > $run) {`.

Commits
-------

cb9be9f [Scheduler] Fix `PeriodicalTrigger` from argument for stateful run dates
  • Loading branch information
nicolas-grekas committed Jul 5, 2023
2 parents 16121cf + cb9be9f commit dd83b18
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,14 @@ public static function providerGetNextRunDateAgain(): iterable
];
yield [
$trigger,
new \DateTimeImmutable('2020-02-20T01:59:59.999999+02:00'),
new \DateTimeImmutable('2020-02-20T01:40:00+02:00'),
new \DateTimeImmutable('2020-02-20T02:00:00+02:00'),
];
yield [
$trigger,
new \DateTimeImmutable('2020-02-20T01:59:00+02:00'),
new \DateTimeImmutable('2020-02-20T02:09:00+02:00'),
];
yield [
$trigger,
new \DateTimeImmutable('2020-02-20T02:00:00+02:00'),
Expand Down
12 changes: 7 additions & 5 deletions src/Symfony/Component/Scheduler/Trigger/PeriodicalTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,20 @@ public function __toString(): string
public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
if ($this->intervalInSeconds) {
if ($this->from > $run) {
return $this->from;
}
if ($this->until <= $run) {
return null;
}

$from = $this->from->format('U.u');
$fromDate = min($this->from, $run);
$from = $fromDate->format('U.u');
$delta = $run->format('U.u') - $from;
$recurrencesPassed = floor($delta / $this->intervalInSeconds);
$nextRunTimestamp = sprintf('%.6F', ($recurrencesPassed + 1) * $this->intervalInSeconds + $from);
$nextRun = \DateTimeImmutable::createFromFormat('U.u', $nextRunTimestamp, $this->from->getTimezone());
$nextRun = \DateTimeImmutable::createFromFormat('U.u', $nextRunTimestamp, $fromDate->getTimezone());

if ($this->from > $nextRun) {
return $this->from;
}

return $this->until > $nextRun ? $nextRun : null;
}
Expand Down

0 comments on commit dd83b18

Please sign in to comment.