Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e0bdc0c

Browse filesBrowse files
committed
feature #26232 [Lock] Add a TTL to refresh lock (jderusse)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Lock] Add a TTL to refresh lock | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | see LockInterface's comment | Tests pass? | yes | Fixed tickets | NA | License | MIT | Doc PR | NA Using remote locks in long processes needs to defines a fined grain refresh TTL. For instance, when looping over a long list of jobs we can extends the live of the lock by few seconds before processing each item. But when the the jobs is splitted and each part to take the same time, we can not define the best TTL Exemple ``` $lock->acquire(); $this->2minutesJob(); $lock->refresh(); $this->5minutesJob(); $lock->refresh(); $this->1minutesJob(); ``` The purpose of this PR is to be able to override the default TTL ``` $lock->acquire(); $lock->refresh(120); $this->2minutesJob(); $lock->refresh(300); $this->5minutesJob(); $lock->refresh(60); $this->1minutesJob(); ``` Commits ------- 3b1f328 Add a TTL to refresh lock
2 parents 3cb5619 + 3b1f328 commit e0bdc0c
Copy full SHA for e0bdc0c

File tree

3 files changed

+24
-5
lines changed
Filter options

3 files changed

+24
-5
lines changed

‎src/Symfony/Component/Lock/Lock.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Lock.php
+7-4Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,25 @@ public function acquire($blocking = false)
105105
/**
106106
* {@inheritdoc}
107107
*/
108-
public function refresh()
108+
public function refresh($ttl = null)
109109
{
110-
if (!$this->ttl) {
110+
if (null === $ttl) {
111+
$ttl = $this->ttl;
112+
}
113+
if (!$ttl) {
111114
throw new InvalidArgumentException('You have to define an expiration duration.');
112115
}
113116

114117
try {
115118
$this->key->resetLifetime();
116-
$this->store->putOffExpiration($this->key, $this->ttl);
119+
$this->store->putOffExpiration($this->key, $ttl);
117120
$this->dirty = true;
118121

119122
if ($this->key->isExpired()) {
120123
throw new LockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.', $this->key));
121124
}
122125

123-
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $this->ttl));
126+
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $ttl));
124127
} catch (LockConflictedException $e) {
125128
$this->dirty = false;
126129
$this->logger->notice('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', array('resource' => $this->key));

‎src/Symfony/Component/Lock/LockInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/LockInterface.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ public function acquire($blocking = false);
3838
/**
3939
* Increase the duration of an acquired lock.
4040
*
41+
* @param float|null $ttl Maximum expected lock duration in seconds
42+
*
4143
* @throws LockConflictedException If the lock is acquired by someone else
4244
* @throws LockAcquiringException If the lock can not be refreshed
4345
*/
44-
public function refresh();
46+
public function refresh(/* $ttl = null */);
4547

4648
/**
4749
* Returns whether or not the lock is acquired.

‎src/Symfony/Component/Lock/Tests/LockTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Tests/LockTest.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ public function testRefresh()
9797
$lock->refresh();
9898
}
9999

100+
public function testRefreshCustom()
101+
{
102+
$key = new Key(uniqid(__METHOD__, true));
103+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
104+
$lock = new Lock($key, $store, 10);
105+
106+
$store
107+
->expects($this->once())
108+
->method('putOffExpiration')
109+
->with($key, 20);
110+
111+
$lock->refresh(20);
112+
}
113+
100114
public function testIsAquired()
101115
{
102116
$key = new Key(uniqid(__METHOD__, true));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.