Skip to content

Commit

Permalink
Add $stopAllOnAnyFailure flag to \Robo\Task\Base\ParallelExec
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Sep 10, 2020
1 parent d872e13 commit 21c5fac
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Task/Base/ParallelExec.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class ParallelExec extends BaseTask implements CommandInterface, PrintedInterfac
*/
protected $isPrinted = false;

/**
* @var bool
*/
protected $stopAllOnAnyFailure = false;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -134,6 +139,17 @@ public function waitInterval($waitInterval)
return $this;
}

/**
* @param bool $stopAllOnAnyFailure
*
* @return $this
*/
public function stopAllOnAnyFailure($stopAllOnAnyFailure)
{
$this->stopAllOnAnyFailure = $stopAllOnAnyFailure;
return $this;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -185,6 +201,12 @@ public function run()
}
}
unset($running[$k]);
if ($this->stopAllOnAnyFailure && $process->getExitCode() !== 0) {
foreach ($running as $otherRunningProcess) {
$otherRunningProcess->stop();
}
$queue = [];
}
}
}
if (empty($running) && empty($queue)) {
Expand Down
46 changes: 46 additions & 0 deletions tests/integration/StopAllOnAnyFailureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Robo;

use PHPUnit\Framework\TestCase;
use Robo\Task\Base\Exec;
use Robo\Task\Base\ParallelExec;
use Robo\Traits\TestTasksTrait;

class StopAllOnAnyFailureTest extends TestCase
{
use TestTasksTrait;
use Task\File\loadTasks;

protected $fixtures;

public function setUp()
{
$this->fixtures = new Fixtures();
$this->initTestTasksTrait();
}

public function tearDown()
{
$this->fixtures->cleanup();
}

public function testParallelProcessesGetStoppedIfStopAllOnAnyFailureIsSet()
{
// Some tests may left this to true
Result::$stopOnFail = false;
$this->fixtures->createAndCdToSandbox();

$filenameOk = uniqid('ok_');
$filenameKo = uniqid('ko_');

/** @var ParallelExec $parallel */
$parallel = $this->task(ParallelExec::class);
$parallel->stopAllOnAnyFailure(true);
$parallel->process($this->task(Exec::class, sprintf('touch %s && false', escapeshellarg($filenameOk))));
$parallel->process($this->task(Exec::class, sprintf('sleep 3 && touch %s', escapeshellarg($filenameKo))));
$parallel->run();

$this->assertFileExists($filenameOk);
$this->assertFileNotExists($filenameKo);
}
}

0 comments on commit 21c5fac

Please sign in to comment.