diff --git a/src/Illuminate/Console/CacheCommandMutex.php b/src/Illuminate/Console/CacheCommandMutex.php index 5eef85862f64..d80fcb2df75c 100644 --- a/src/Illuminate/Console/CacheCommandMutex.php +++ b/src/Illuminate/Console/CacheCommandMutex.php @@ -5,9 +5,12 @@ use Carbon\CarbonInterval; use Illuminate\Contracts\Cache\Factory as Cache; use Illuminate\Contracts\Cache\LockProvider; +use Illuminate\Support\InteractsWithTime; class CacheCommandMutex implements CommandMutex { + use InteractsWithTime; + /** * The cache factory implementation. * @@ -42,12 +45,13 @@ public function create($command) { $store = $this->cache->store($this->store); + /** @var \DateTimeInterface|\DateInterval $expiresAt */ $expiresAt = method_exists($command, 'isolationLockExpiresAt') ? $command->isolationLockExpiresAt() : CarbonInterval::hour(); - if ($store instanceof LockProvider) { - return $store->lock($this->commandMutexName($command), $expiresAt)->get(); + if ($store->getStore() instanceof LockProvider) { + return $store->getStore()->lock($this->commandMutexName($command), $this->secondsUntil($expiresAt))->get(); } return $store->add($this->commandMutexName($command), true, $expiresAt); @@ -66,8 +70,8 @@ public function exists($command) { $store = $this->cache->store($this->store); - if ($store instanceof LockProvider) { - $lock = $store->lock($this->commandMutexName($command)); + if ($store->getStore() instanceof LockProvider) { + $lock = $store->getStore()->lock($this->commandMutexName($command)); $acquired = $lock->get(); $lock->release(); @@ -87,8 +91,8 @@ public function forget($command) { $store = $this->cache->store($this->store); - if ($store instanceof LockProvider) { - return $store->lock($this->commandMutexName($command))->release(); + if ($store->getStore() instanceof LockProvider) { + return $store->getStore()->lock($this->commandMutexName($command))->forceRelease(); } return $this->cache->store($this->store)->forget($this->commandMutexName($command)); diff --git a/tests/Console/CacheCommandMutexTest.php b/tests/Console/CacheCommandMutexTest.php index 61326892188a..482f4e243d7d 100644 --- a/tests/Console/CacheCommandMutexTest.php +++ b/tests/Console/CacheCommandMutexTest.php @@ -37,6 +37,7 @@ class CacheCommandMutexTest extends TestCase protected function setUp(): void { $this->cacheFactory = m::mock(Factory::class); + $this->cacheRepository = m::mock(Repository::class); $this->mutex = new CacheCommandMutex($this->cacheFactory); $this->command = new class extends Command { @@ -116,7 +117,9 @@ public function testCannotCreateMutexIfAlreadyExistWithLockProvider() public function testCanCreateMutexWithCustomConnectionWithLockProvider() { $lock = m::mock(LockProvider::class); - $this->cacheFactory->expects('store')->once()->with('test')->andReturns($lock); + $this->cacheFactory->expects('store')->once()->with('test')->andReturn($this->cacheRepository); + $this->cacheRepository->expects('getStore')->andReturn($lock); + $this->acquireLockExpectations($lock, true); $this->mutex->useStore('test'); @@ -128,14 +131,15 @@ public function testCanCreateMutexWithCustomConnectionWithLockProvider() */ private function mockUsingCacheStore(): void { - $this->cacheRepository = m::mock(Repository::class); $this->cacheFactory->expects('store')->once()->andReturn($this->cacheRepository); + $this->cacheRepository->expects('getStore')->andReturn(null); } private function mockUsingLockProvider(): m\MockInterface { $lock = m::mock(LockProvider::class); - $this->cacheFactory->expects('store')->once()->andReturns($lock); + $this->cacheFactory->expects('store')->once()->andReturn($this->cacheRepository); + $this->cacheRepository->expects('getStore')->andReturn($lock); return $lock; } @@ -144,7 +148,7 @@ private function acquireLockExpectations(MockInterface $lock, bool $acquiresSucc { $lock->expects('lock') ->once() - ->with(m::type('string'), m::type(CarbonInterval::class)) + ->with(m::type('string'), m::type('int')) ->andReturns($lock); $lock->expects('get')