Skip to content

[Scheduler] Continue with stored Checkpoint::$time on lock #52039

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
Nov 17, 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
10 changes: 4 additions & 6 deletions src/Symfony/Component/Scheduler/Generator/Checkpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,18 @@ public function __construct(
public function acquire(\DateTimeImmutable $now): bool
{
if ($this->lock && !$this->lock->acquire()) {
// Reset local state if a Lock is acquired by another Worker.
// Reset local state if a Lock is acquired by another Worker and state is not shared through cache.
$this->reset = true;

return false;
}

if ($this->reset) {
$this->reset = false;
$this->save($now, -1);
}

if ($this->cache) {
[$this->time, $this->index, $this->from] = $this->cache->get($this->name, fn () => [$now, -1, $now]) + [2 => $now];
$this->save($this->time, $this->index);
} elseif ($this->reset) {
$this->reset = false;
$this->save($now, -1);
}

$this->time ??= $now;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ public function testWithLockResetStateAfterLockedAcquiring()
$this->assertFalse($concurrentLock->isAcquired());
}

public function testWithLockResetStateAfterLockedAcquiringCache()
{
$concurrentLock = new Lock(new Key('locked'), $store = new InMemoryStore(), autoRelease: false);
$concurrentLock->acquire();
$this->assertTrue($concurrentLock->isAcquired());

$lock = new Lock(new Key('locked'), $store, autoRelease: false);
$checkpoint = new Checkpoint('locked', $lock, $cache = new ArrayAdapter());
$now = new \DateTimeImmutable('2020-02-20 20:20:20Z');

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

$two = new Checkpoint('locked', $lock, $cache);

$concurrentLock->release();

$this->assertTrue($two->acquire($now));
$this->assertEquals($savedTime, $two->time());
$this->assertEquals($savedIndex, $two->index());
$this->assertTrue($lock->isAcquired());
$this->assertFalse($concurrentLock->isAcquired());
}

public function testWithLockKeepLock()
{
$lock = new Lock(new Key('lock'), new InMemoryStore());
Expand Down