-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Expiring lock stores & lock's TTL fixes #96
Open
G81BVfaN
wants to merge
5
commits into
crunzphp:3.8
Choose a base branch
from
RE-INvent-Retail:expiring_lock_stores_fix
base: 3.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6713d49
- Adding the possibility to specify a TTL (Time To Live) for the lock.
G81BVfaN 23ae3a9
- Adding the possibility to specify a TTL (Time To Live) for the lock.
G81BVfaN 35f387c
- A little fix suggested from PHP CS Fixer
G81BVfaN 0ea5ad9
- Stop refreshing the lock's TTL, if the lock store supports expiring.
G81BVfaN 4004aba
- Some PHP CS Fixer's fixes & workarounds.
G81BVfaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -701,11 +701,14 @@ public function user($user) | |
* By default, the lock is acquired through file system locks. Alternatively, you can pass a symfony lock store | ||
* that will be responsible for the locking. | ||
* | ||
* @param PersistingStoreInterface|object $store | ||
* @param PersistingStoreInterface|object|null $store A symfony lock store | ||
* @param int|null $ttl Time To Live of the lock in seconds | ||
* | ||
* @return $this | ||
* | ||
* @throws CrunzException | ||
*/ | ||
public function preventOverlapping(?object $store = null) | ||
public function preventOverlapping(?object $store = null, ?int $ttl = 30) | ||
{ | ||
if (null !== $store && !($store instanceof PersistingStoreInterface)) { | ||
$expectedClass = PersistingStoreInterface::class; | ||
|
@@ -721,8 +724,8 @@ public function preventOverlapping(?object $store = null) | |
$this->lockFactory = new LockFactory($lockStore); | ||
|
||
// Skip the event if it's locked (processing) | ||
$this->skip(function () { | ||
$lock = $this->createLockObject(); | ||
$this->skip(function () use ($ttl) { | ||
$lock = $this->createLockObject($ttl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about some tests? |
||
$lock->acquire(); | ||
|
||
return !$lock->isAcquired(); | ||
|
@@ -1008,14 +1011,22 @@ public function errorCallbacks() | |
} | ||
|
||
/** | ||
* If this event is prevented from overlapping, this method should be called regularly to refresh the lock. | ||
* If this event is prevented from overlapping, this method should be called regularly to refresh the lock, | ||
* UNLESS the lock store supports expiring (TTL), which means no refresh is needed. | ||
*/ | ||
public function refreshLock(): void | ||
{ | ||
if (!$this->preventOverlapping) { | ||
return; | ||
} | ||
|
||
// If the lock has remaining lifetime (i.e. the method returns a float and not NULL), that means the LockStore does support TTL [ 'MemcachedStore', 'MongoDbStore' , 'PdoStore', 'DoctrineDbalStore', 'RedisStore' ] | ||
// @see https://symfony.com/doc/6.4/components/lock.html#available-stores for detailed information | ||
$remainingLifetime = $this->lock->getRemainingLifetime(); | ||
if (null !== $remainingLifetime) { | ||
return; | ||
} | ||
|
||
$lock = $this->createLockObject(); | ||
$remainingLifetime = $lock->getRemainingLifetime(); | ||
|
||
|
@@ -1094,15 +1105,15 @@ public function everySixHours(): self | |
/** | ||
* Get the symfony lock object for the task. | ||
* | ||
* @param int|null $ttl Time To Live of the lock in seconds | ||
* | ||
* @return Lock | ||
*/ | ||
protected function createLockObject() | ||
protected function createLockObject(?int $ttl = 30) | ||
{ | ||
$this->checkLockFactory(); | ||
|
||
if (null === $this->lock && null !== $this->lockFactory) { | ||
$ttl = 30; | ||
|
||
$this->lock = $this->lockFactory | ||
->createLock($this->lockKey(), $ttl); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
null
is allowed?