Skip to content

[Scheduler] Add capability to skip missed periodic tasks, only the last schedule will be called #58001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add capability to skip missed periodic tasks, only the last schedule will be called

6.4
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ public function getMessages(): \Generator
$yield = false;
}

if ($nextTime = $trigger->getNextRunDate($time)) {
$nextTime = $trigger->getNextRunDate($time);

if ($this->schedule->shouldProcessOnlyLastMissedRun()) {
while ($nextTime < $this->clock->now()) {
$nextTime = $trigger->getNextRunDate($nextTime);
}
}

if ($nextTime) {
$heap->insert([$nextTime, $index, $recurringMessage]);
}

Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Scheduler/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
private ?LockInterface $lock = null;
private ?CacheInterface $state = null;
private bool $shouldRestart = false;
private bool $onlyLastMissed = false;

public function with(RecurringMessage $message, RecurringMessage ...$messages): static
{
Expand Down Expand Up @@ -123,6 +124,21 @@ public function getState(): ?CacheInterface
return $this->state;
}

/**
* @return $this
*/
public function processOnlyLastMissedRun(bool $onlyLastMissed): static
{
$this->onlyLastMissed = $onlyLastMissed;

return $this;
}

public function shouldProcessOnlyLastMissedRun(): bool
{
return $this->onlyLastMissed;
}

/**
* @return array<RecurringMessage>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,67 @@ public function testCheckpointSavedInBrokenLoop()
$this->assertEquals(self::makeDateTime('22:13:00'), $checkpoint->time());
}

public function testCheckpointSavedInBigBrokenLoop()
{
$clock = new MockClock(self::makeDateTime('22:15:00'));

$message = RecurringMessage::every('1 minute', (object) ['id' => 'message']);
$schedule = (new Schedule())->add($message);

$cache = new ArrayAdapter();
$schedule->stateful($cache);
$checkpoint = new Checkpoint('dummy', cache: $cache);

$scheduler = new MessageGenerator($schedule, 'dummy', clock: $clock, checkpoint: $checkpoint);

// Warmup. The first run is always returns nothing.
$this->assertSame([], iterator_to_array($scheduler->getMessages(), false));
$this->assertEquals(self::makeDateTime('22:15:00'), $checkpoint->time());

$clock->sleep(60 + 10); // 22:16:10

$this->assertCount(1, iterator_to_array($scheduler->getMessages(), false));

$clock->sleep(2 * 60); // 22:18:10

$this->assertCount(2, iterator_to_array($scheduler->getMessages(), false));

$clock->sleep(5 * 60); // 22:23:10

$this->assertCount(5, iterator_to_array($scheduler->getMessages(), false));

$this->assertEquals(self::makeDateTime('22:23:00'), $checkpoint->time());
}

public function testCheckpointSavedInBigBrokenLoopWithOnlyLastMissed()
{
$clock = new MockClock(self::makeDateTime('22:15:00'));

$message = RecurringMessage::every('1 minute', (object) ['id' => 'message']);
$schedule = (new Schedule())->add($message);

$cache = new ArrayAdapter();
$schedule->stateful($cache)->processOnlyLastMissedRun(true);
$checkpoint = new Checkpoint('dummy', cache: $cache);

$scheduler = new MessageGenerator($schedule, 'dummy', clock: $clock, checkpoint: $checkpoint);

// Warmup. The first run is always returns nothing.
$this->assertSame([], iterator_to_array($scheduler->getMessages(), false));
$this->assertEquals(self::makeDateTime('22:15:00'), $clock->now());

$clock->sleep(60 + 10); // 22:16:10
$this->assertCount(1, iterator_to_array($scheduler->getMessages(), false));

$clock->sleep(2 * 60); // 22:18:10
$this->assertCount(1, iterator_to_array($scheduler->getMessages(), false));

$clock->sleep(5 * 60); // 22:23:10
$this->assertCount(1, iterator_to_array($scheduler->getMessages(), false));

$this->assertEquals(self::makeDateTime('22:23:10'), $clock->now());
}

public static function messagesProvider(): \Generator
{
$first = (object) ['id' => 'first'];
Expand Down
Loading