Skip to content

Commit b649406

Browse files
committed
Don't lose checkpoint state when lock is acquired from another
1 parent 3e9cabf commit b649406

File tree

3 files changed

+4
-12
lines changed

3 files changed

+4
-12
lines changed

src/Symfony/Component/Scheduler/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Add `ScheduledStamp` to `RedispatchMessage`
1414
* Allow modifying Schedule instances at runtime
1515
* Add `MessageProviderInterface` to trigger unique messages at runtime
16+
* Continue from stored `Checkpoint` time when a lock is acquired
1617

1718
6.3
1819
---

src/Symfony/Component/Scheduler/Generator/Checkpoint.php

-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ final class Checkpoint implements CheckpointInterface
1919
private \DateTimeImmutable $from;
2020
private \DateTimeImmutable $time;
2121
private int $index = -1;
22-
private bool $reset = false;
2322

2423
public function __construct(
2524
private readonly string $name,
@@ -31,17 +30,9 @@ public function __construct(
3130
public function acquire(\DateTimeImmutable $now): bool
3231
{
3332
if ($this->lock && !$this->lock->acquire()) {
34-
// Reset local state if a Lock is acquired by another Worker.
35-
$this->reset = true;
36-
3733
return false;
3834
}
3935

40-
if ($this->reset) {
41-
$this->reset = false;
42-
$this->save($now, -1);
43-
}
44-
4536
if ($this->cache) {
4637
[$this->time, $this->index, $this->from] = $this->cache->get($this->name, fn () => [$now, -1, $now]) + [2 => $now];
4738
$this->save($this->time, $this->index);

src/Symfony/Component/Scheduler/Tests/Generator/CheckpointTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ public function testWithLockResetStateAfterLockedAcquiring()
178178
$checkpoint = new Checkpoint('locked', $lock);
179179
$now = new \DateTimeImmutable('2020-02-20 20:20:20Z');
180180

181-
$checkpoint->save($now->modify('-2 min'), 0);
181+
$checkpoint->save($savedTime = $now->modify('-2 min'), $savedIndex = 0);
182182
$checkpoint->acquire($now->modify('-1 min'));
183183

184184
$concurrentLock->release();
185185

186186
$this->assertTrue($checkpoint->acquire($now));
187-
$this->assertEquals($now, $checkpoint->time());
188-
$this->assertEquals(-1, $checkpoint->index());
187+
$this->assertEquals($savedTime, $checkpoint->time());
188+
$this->assertEquals($savedIndex, $checkpoint->index());
189189
$this->assertTrue($lock->isAcquired());
190190
$this->assertFalse($concurrentLock->isAcquired());
191191
}

0 commit comments

Comments
 (0)