diff --git a/src/Illuminate/Console/CacheCommandMutex.php b/src/Illuminate/Console/CacheCommandMutex.php index af9819a334dd..0a896c6bc9ce 100644 --- a/src/Illuminate/Console/CacheCommandMutex.php +++ b/src/Illuminate/Console/CacheCommandMutex.php @@ -101,12 +101,18 @@ public function forget($command) } /** + * Get the isolatable command mutex name. + * * @param \Illuminate\Console\Command $command * @return string */ protected function commandMutexName($command) { - return 'framework'.DIRECTORY_SEPARATOR.'command-'.$command->getName(); + $baseName = 'framework'.DIRECTORY_SEPARATOR.'command-'.$command->getName(); + + return method_exists($command, 'isolatableId') + ? $baseName.'-'.$command->isolatableId() + : $baseName; } /** diff --git a/tests/Console/CacheCommandMutexTest.php b/tests/Console/CacheCommandMutexTest.php index fa8ce086436a..4794ec7df812 100644 --- a/tests/Console/CacheCommandMutexTest.php +++ b/tests/Console/CacheCommandMutexTest.php @@ -160,4 +160,54 @@ private function acquireLockExpectations(MockInterface $lock, bool $acquiresSucc ->once() ->andReturns($acquiresSuccessfully); } + + public function testCommandMutexNameWithoutIsolatedMutexNameMethod() + { + $this->mockUsingCacheStore(); + + $this->cacheRepository->shouldReceive('getStore') + ->with('test') + ->andReturn($this->cacheRepository); + + $this->cacheRepository->shouldReceive('add') + ->once() + ->withArgs(function ($key) { + $this->assertEquals('framework'.DIRECTORY_SEPARATOR.'command-command-name', $key); + + return true; + }) + ->andReturn(true); + + $this->mutex->create($this->command); + } + + public function testCommandMutexNameWithIsolatedMutexNameMethod() + { + $command = new class extends Command + { + protected $name = 'command-name'; + + public function isolatableId() + { + return 'isolated'; + } + }; + + $this->mockUsingCacheStore(); + + $this->cacheRepository->shouldReceive('getStore') + ->with('test') + ->andReturn($this->cacheRepository); + + $this->cacheRepository->shouldReceive('add') + ->once() + ->withArgs(function ($key) { + $this->assertEquals('framework'.DIRECTORY_SEPARATOR.'command-command-name-isolated', $key); + + return true; + }) + ->andReturn(true); + + $this->mutex->create($command); + } }