diff --git a/README.md b/README.md index 03e02d7..56f8953 100644 --- a/README.md +++ b/README.md @@ -517,6 +517,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 diff --git a/src/Event.php b/src/Event.php index 30902b6..35c9fde 100644 --- a/src/Event.php +++ b/src/Event.php @@ -704,10 +704,11 @@ public function user($user) * that will be responsible for the locking. * * @param PersistingStoreInterface|object $store - * + * @param int|null $ttl * @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; @@ -723,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); $lock->acquire(); return !$lock->isAcquired(); @@ -1100,15 +1101,14 @@ public function everySixHours(): self /** * Get the symfony lock object for the task. * + * @param int|null $ttl * @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); }