Skip to content

[Scheduler] Allow modifying the schedule at runtime and recalculate heap #51553

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
Sep 26, 2023
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Add `AbstractTriggerDecorator`
* Make `ScheduledStamp` "send-able"
* Add `ScheduledStamp` to `RedispatchMessage`
* Allow modifying the Schedule at runtime
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Allow modifying the Schedule at runtime
* Allow modifying Schedule instances at runtime


6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

final class MessageGenerator implements MessageGeneratorInterface
{
private Schedule $schedule;
private ?Schedule $schedule = null;
private TriggerHeap $triggerHeap;
private ?\DateTimeImmutable $waitUntil;

Expand All @@ -36,6 +36,12 @@ public function getMessages(): \Generator
{
$checkpoint = $this->checkpoint();

if ($this->schedule?->shouldRestart()) {
unset($this->triggerHeap);
$this->waitUntil = new \DateTimeImmutable('@0');
$this->schedule->setRestart(false);
}

if (!$this->waitUntil
|| $this->waitUntil > ($now = $this->clock->now())
|| !$checkpoint->acquire($now)
Expand Down
49 changes: 47 additions & 2 deletions src/Symfony/Component/Scheduler/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,55 @@ final class Schedule implements ScheduleProviderInterface
private array $messages = [];
private ?LockInterface $lock = null;
private ?CacheInterface $state = null;
private bool $shouldRestart = false;

public static function with(RecurringMessage $message, RecurringMessage ...$messages): static
{
return static::doAdd(new self(), $message, ...$messages);
}

/**
* @return $this
*/
public function add(RecurringMessage $message, RecurringMessage ...$messages): static
{
$this->setRestart(true);

return static::doAdd($this, $message, ...$messages);
}

private static function doAdd(self $schedule, RecurringMessage $message, RecurringMessage ...$messages): static
{
foreach ([$message, ...$messages] as $m) {
if (isset($this->messages[$m->getId()])) {
if (isset($schedule->messages[$m->getId()])) {
throw new LogicException('Duplicated schedule message.');
}

$this->messages[$m->getId()] = $m;
$schedule->messages[$m->getId()] = $m;
}

return $schedule;
}

/**
* @return $this
*/
public function remove(RecurringMessage $message): static
{
unset($this->messages[$message->getId()]);
$this->setRestart(true);

return $this;
}

/**
* @return $this
*/
public function clear(): static
{
$this->messages = [];
$this->setRestart(true);

return $this;
}

Expand Down Expand Up @@ -83,4 +118,14 @@ public function getSchedule(): static
{
return $this;
}

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

public function setRestart(bool $shouldRestart): bool
{
return $this->shouldRestart = $shouldRestart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,72 @@ public function getSchedule(): Schedule
}
}

public function testGetMessagesFromScheduleProviderWithRestart()
{
$first = (object) ['id' => 'first'];
$startTime = '22:12:00';
$runs = [
'22:12:00' => [],
'22:12:01' => [],
'22:13:00' => [$first],
'22:13:01' => [],
];
$schedule = [[$first, '22:13:00', '22:14:00']];

// for referencing
$now = self::makeDateTime($startTime);

$clock = $this->createMock(ClockInterface::class);
$clock->method('now')->willReturnReference($now);

foreach ($schedule as $i => $s) {
if (\is_array($s)) {
$schedule[$i] = $this->createMessage(...$s);
}
}

$scheduleProvider = new class($schedule) implements ScheduleProviderInterface {
private Schedule $schedule;

public function __construct(array $schedule)
{
$this->schedule = Schedule::with(...$schedule);
$this->schedule->stateful(new ArrayAdapter());
}

public function getSchedule(): Schedule
{
return $this->schedule;
}

public function add(RecurringMessage $message): self
{
$this->schedule->add($message);

return $this;
}
};

$scheduler = new MessageGenerator($scheduleProvider, 'dummy', $clock);

// Warmup. The first run always returns nothing.
$this->assertSame([], iterator_to_array($scheduler->getMessages(), false));

$toAdd = (object) ['id' => 'added-after-start'];

foreach ($runs as $time => $expected) {
$now = self::makeDateTime($time);
$this->assertSame($expected, iterator_to_array($scheduler->getMessages(), false));
}

$scheduleProvider->add($this->createMessage($toAdd, '22:13:10', '22:13:11'));

$this->assertSame([], iterator_to_array($scheduler->getMessages(), false));

$now = self::makeDateTime('22:13:10');
$this->assertSame([$toAdd], iterator_to_array($scheduler->getMessages(), false));
}

public function testYieldedContext()
{
// for referencing
Expand Down