Skip to content
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 @@ -508,6 +508,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 $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;
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?

Copy link
Author

@G81BVfaN G81BVfaN Jun 30, 2025

Choose a reason for hiding this comment

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

I've added a very basic test:
test_expiring_store_can_be_passed_to_prevent_overlapping_with_ttl()

$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 $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
15 changes: 15 additions & 0 deletions tests/Unit/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,21 @@ public function test_non_blocking_store_can_be_passed_to_prevent_overlapping():
$event->preventOverlapping($store);
}

public function test_expiring_store_can_be_passed_to_prevent_overlapping_with_ttl(): void
{
// Arrange
$store = new PdoStore('');
$ttl = 3;

$event = $this->createEvent();

// Expect
$this->expectNotToPerformAssertions();

// Act
$event->preventOverlapping($store, $ttl);
}

/**
* @param \Closure(): array{
* now: \DateTimeImmutable,
Expand Down