Skip to content

Commit

Permalink
Add --isolated flag to command
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver committed Oct 28, 2022
1 parent 04a9a4b commit b074952
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/Illuminate/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Traits\Macroable;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Command extends SymfonyCommand
Expand Down Expand Up @@ -87,6 +88,15 @@ public function __construct()
if (! isset($this->signature)) {
$this->specifyParameters();
}

if ($this instanceof Isolated) {
$this->getDefinition()->addOption(new InputOption(
'isolated',
null,
InputOption::VALUE_OPTIONAL,
'Limits the command to only have one instance running at once'
));
}
}

/**
Expand Down Expand Up @@ -140,7 +150,7 @@ public function run(InputInterface $input, OutputInterface $output): int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($this instanceof Isolated && ! $this->mutex()->create($this)) {
if ($this instanceof Isolated && $this->option('isolated') && ! $this->mutex()->create($this)) {
$this->info(sprintf(
'Skipping [%s], as command already running.', $this->getName()
));
Expand All @@ -153,7 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
try {
return (int) $this->laravel->call([$this, $method]);
} finally {
if ($this instanceof Isolated) {
if ($this instanceof Isolated && $this->option('isolated')) {
$this->mutex()->release($this);
}
}
Expand Down
13 changes: 11 additions & 2 deletions tests/Console/CommandMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ public function testCanRunCommandAgainAfterOtherCommandFinished()
$this->assertEquals(2, $this->command->ran);
}

protected function runCommand()
public function testCanRunCommandAgainNonAutomated()
{
$input = new ArrayInput([]);
$this->commandMutex->shouldNotHaveBeenCalled();

$this->runCommand(false);

$this->assertEquals(1, $this->command->ran);
}

protected function runCommand($withIsolated = true)
{
$input = new ArrayInput(['--isolated' => $withIsolated]);
$output = new NullOutput;
$this->command->run($input, $output);
}
Expand Down

0 comments on commit b074952

Please sign in to comment.