Skip to content
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
wants to merge 5 commits into
base: 3.8
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,11 @@ $task
->preventOverlapping($store);

```
Optionally, you can specify a TTL (Time To Live) for the lock

```php
$task->preventOverlapping($store, 60); // Lock expires after 60 seconds
```

## Keeping the Output

Expand Down
27 changes: 19 additions & 8 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why null is allowed?

{
if (null !== $store && !($store instanceof PersistingStoreInterface)) {
$expectedClass = PersistingStoreInterface::class;
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about some tests?

$lock->acquire();

return !$lock->isAcquired();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
Expand Down