Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Allow custom mutex names for isolated commands #47814

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Illuminate/Console/CacheCommandMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions tests/Console/CacheCommandMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}