From a95e0ace34f949e70908aff09fddf4c88ec3a00b Mon Sep 17 00:00:00 2001 From: G81BVfaN <131437174+G81BVfaN@users.noreply.github.com> Date: Wed, 16 Oct 2024 13:28:18 +0200 Subject: [PATCH 1/3] - Adding the possibility to specify a TTL (Time To Live) for the lock. --- src/Event.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Event.php b/src/Event.php index 30902b6..7d9581e 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 \Crunz\Exception\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); } From ffa32a4b00719db0560ffec15421fa190c4772b3 Mon Sep 17 00:00:00 2001 From: G81BVfaN <131437174+G81BVfaN@users.noreply.github.com> Date: Wed, 16 Oct 2024 13:30:44 +0200 Subject: [PATCH 2/3] - Adding the possibility to specify a TTL (Time To Live) for the lock. --- README.md | 5 +++++ 1 file changed, 5 insertions(+) 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 From ab7ec1dd6729ab74527fa2973037a935e270b5b8 Mon Sep 17 00:00:00 2001 From: akhateeb Date: Thu, 17 Oct 2024 11:32:34 +0200 Subject: [PATCH 3/3] - A little fix suggested from PHP CS Fixer --- src/Event.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event.php b/src/Event.php index 7d9581e..35c9fde 100644 --- a/src/Event.php +++ b/src/Event.php @@ -706,7 +706,7 @@ public function user($user) * @param PersistingStoreInterface|object $store * @param int|null $ttl * @return $this - * @throws \Crunz\Exception\CrunzException + * @throws CrunzException */ public function preventOverlapping(?object $store = null, ?int $ttl = 30) {