Skip to content
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

[Scheduler] Save checkpoint in a finally block #52307

Merged
merged 1 commit into from Oct 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -72,8 +72,11 @@ public function getMessages(): \Generator
}

if ($yield) {
yield (new MessageContext($this->name, $id, $trigger, $time, $nextTime)) => $message;
$checkpoint->save($time, $index);
try {
yield (new MessageContext($this->name, $id, $trigger, $time, $nextTime)) => $message;
} finally {
$checkpoint->save($time, $index);
fabpot marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down
Expand Up @@ -14,6 +14,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Scheduler\Generator\Checkpoint;
use Symfony\Component\Scheduler\Generator\MessageContext;
use Symfony\Component\Scheduler\Generator\MessageGenerator;
use Symfony\Component\Scheduler\RecurringMessage;
Expand Down Expand Up @@ -128,6 +130,32 @@ public function testYieldedContext()
$this->assertEquals(self::makeDateTime('22:16:00'), $context->nextTriggerAt);
}

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

$message = $this->createMessage((object) ['id' => 'message'], '22:13:00', '22:14:00', '22:16:00');
$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));

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

foreach ($scheduler->getMessages() as $message) {
// Message is handled but loop is broken just after
break;
}

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

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