Skip to content
Closed
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 @@ -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

Expand Down
14 changes: 7 additions & 7 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down