8000 Don't lose checkpoint state when lock is acquired from another · symfony/symfony@fe6e951 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe6e951

Browse files
committed
Don't lose checkpoint state when lock is acquired from another
1 parent c9e7809 commit fe6e951

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,17 @@ public function __construct(
3131
public function acquire(\DateTimeImmutable $now): bool
3232
{
3333
if ($this->lock && !$this->lock->acquire()) {
34-
// Reset local state if a Lock is acquired by another Worker.
3534
$this->reset = true;
3635

3736
return false;
3837
}
3938

40-
if ($this->reset) {
41-
$this->reset = false;
42-
$this->save($now, -1);
43-
}
44-
4539
if ($this->cache) {
4640
[$this->time, $this->index, $this->from] = $this->cache->get($this->name, fn () => [$now, -1, $now]) + [2 => $now];
4741
$this->save($this->time, $this->index);
42+
} elseif ($this->reset) {
43+
$this->reset = false;
44+
$this->save($now, -1);
4845
}
4946

5047
$this->time ??= $now;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ public function testWithLockResetStateAfterLockedAcquiring()
190190
$this->assertFalse($concurrentLock->isAcquired());
191191
}
192192

193+
public function testWithLockResetStateAfterLockedAcquiringCache()
194+
{
195+
$concurrentLock = new Lock(new Key('locked'), $store = new InMemoryStore(), autoRelease: false);
196+
$concurrentLock->acquire();
197+
$this->assertTrue($concurrentLock->isAcquired());
198+
199+
$lock = new Lock(new Key('locked'), $store, autoRelease: false);
200+
$checkpoint = new Checkpoint('locked', $lock, $cache = new ArrayAdapter());
201+
$now = new \DateTimeImmutable('2020-02-20 20:20:20Z');
202+
203+
$checkpoint->save($savedTime = $now->modify('-2 min'), $savedIndex = 0);
204+
$checkpoint->acquire($now->modify('-1 min'));
205+
206+
$two = new Checkpoint('locked', $lock, $cache);
207+
208+
$concurrentLock->release();
209+
210+
$this->assertTrue($two->acquire($now));
211+
$this->assertEquals($savedTime, $two->time());
212+
$this->assertEquals($savedIndex, $two->index());
213+
$this->assertTrue($lock->isAcquired());
214+
$this->assertFalse($concurrentLock->isAcquired());
215+
}
216+
193217
public function testWithLockKeepLock()
194218
{
195219
$lock = new Lock(new Key('lock'), new InMemoryStore());

0 commit comments

Comments
 (0)
0