commit21
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/** Local process exclusion; OS releases the lock if a worker is terminated. */
|
||||
class ReplenishmentProcessLock
|
||||
{
|
||||
/** @var resource|null */
|
||||
private $handle = null;
|
||||
|
||||
public function acquire(): bool
|
||||
{
|
||||
if ($this->handle !== null) {
|
||||
throw new RuntimeException('Verificação periódica já adquirida nesta instância.');
|
||||
}
|
||||
$handle = fopen(storage_path('framework/replenishment-command.lock'), 'c');
|
||||
if ($handle === false) {
|
||||
throw new RuntimeException('Não foi possível abrir o controlo da verificação periódica.');
|
||||
}
|
||||
if (! flock($handle, LOCK_EX | LOCK_NB)) {
|
||||
fclose($handle);
|
||||
|
||||
return false;
|
||||
}
|
||||
$this->handle = $handle;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function release(): void
|
||||
{
|
||||
if ($this->handle !== null) {
|
||||
flock($this->handle, LOCK_UN);
|
||||
fclose($this->handle);
|
||||
$this->handle = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->release();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user