41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Concerns;
|
|
|
|
use Illuminate\Contracts\Cache\Lock;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
trait InteractsWithCacheLocks
|
|
{
|
|
/**
|
|
* Attempt to acquire a cache-backed lock for the given command.
|
|
*
|
|
* @return \Illuminate\Contracts\Cache\Lock|false|null Returns the lock when acquired, false if another process holds it, null if locks are unsupported.
|
|
*/
|
|
protected function acquireCommandLock(string $name, int $seconds, bool $force = false): Lock|false|null
|
|
{
|
|
try {
|
|
$lock = Cache::lock($name, $seconds);
|
|
|
|
if ($force) {
|
|
$lock->forceRelease();
|
|
}
|
|
|
|
if ($lock->get()) {
|
|
return $lock;
|
|
}
|
|
|
|
return false;
|
|
} catch (\BadMethodCallException $exception) {
|
|
Log::channel('storage-jobs')->debug('Cache store does not support locks for command', [
|
|
'lock' => $name,
|
|
'store' => config('cache.default'),
|
|
'exception' => $exception->getMessage(),
|
|
]);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|