Skip to content
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
Original file line number Diff line number Diff line change
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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
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