Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/WithoutOverlapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

trait WithoutOverlapping
{
/**
* Link to current mutex.
*
* @var Mutex
*/
private Mutex $mutex;

/**
* Overwrite the console command initialization.
*
Expand All @@ -28,14 +35,14 @@ protected function initialize(InputInterface $input, OutputInterface $output)
*/
protected function initializeMutex()
{
$mutex = new Mutex($this);
$this->mutex = new Mutex($this);

$timeout = $this->getMutexTimeout();
if (!$mutex->acquireLock($timeout)) {
if (!$this->mutex->acquireLock($timeout)) {
throw new MutexRuntimeException('Command is running now!');
}

register_shutdown_function([$this, 'releaseMutexLock'], $mutex);
register_shutdown_function([$this, 'releaseMutexLock'], $this->mutex);
}

/**
Expand Down Expand Up @@ -133,4 +140,16 @@ public function releaseMutexLock(Mutex $mutex)
{
$mutex->releaseLock();
}

/**
* Release the current mutex lock.
*
* For custom call
*
* @return void
*/
public function releaseCurrentMutexLock()
{
$this->mutex->releaseLock();
}
}