Skip to content

Commit 21c5fac

Browse files
committed
Add $stopAllOnAnyFailure flag to \Robo\Task\Base\ParallelExec
1 parent d872e13 commit 21c5fac

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/Task/Base/ParallelExec.php

+22
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class ParallelExec extends BaseTask implements CommandInterface, PrintedInterfac
5151
*/
5252
protected $isPrinted = false;
5353

54+
/**
55+
* @var bool
56+
*/
57+
protected $stopAllOnAnyFailure = false;
58+
5459
/**
5560
* {@inheritdoc}
5661
*/
@@ -134,6 +139,17 @@ public function waitInterval($waitInterval)
134139
return $this;
135140
}
136141

142+
/**
143+
* @param bool $stopAllOnAnyFailure
144+
*
145+
* @return $this
146+
*/
147+
public function stopAllOnAnyFailure($stopAllOnAnyFailure)
148+
{
149+
$this->stopAllOnAnyFailure = $stopAllOnAnyFailure;
150+
return $this;
151+
}
152+
137153
/**
138154
* {@inheritdoc}
139155
*/
@@ -185,6 +201,12 @@ public function run()
185201
}
186202
}
187203
unset($running[$k]);
204+
if ($this->stopAllOnAnyFailure && $process->getExitCode() !== 0) {
205+
foreach ($running as $otherRunningProcess) {
206+
$otherRunningProcess->stop();
207+
}
208+
$queue = [];
209+
}
188210
}
189211
}
190212
if (empty($running) && empty($queue)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace Robo;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Robo\Task\Base\Exec;
6+
use Robo\Task\Base\ParallelExec;
7+
use Robo\Traits\TestTasksTrait;
8+
9+
class StopAllOnAnyFailureTest extends TestCase
10+
{
11+
use TestTasksTrait;
12+
use Task\File\loadTasks;
13+
14+
protected $fixtures;
15+
16+
public function setUp()
17+
{
18+
$this->fixtures = new Fixtures();
19+
$this->initTestTasksTrait();
20+
}
21+
22+
public function tearDown()
23+
{
24+
$this->fixtures->cleanup();
25+
}
26+
27+
public function testParallelProcessesGetStoppedIfStopAllOnAnyFailureIsSet()
28+
{
29+
// Some tests may left this to true
30+
Result::$stopOnFail = false;
31+
$this->fixtures->createAndCdToSandbox();
32+
33+
$filenameOk = uniqid('ok_');
34+
$filenameKo = uniqid('ko_');
35+
36+
/** @var ParallelExec $parallel */
37+
$parallel = $this->task(ParallelExec::class);
38+
$parallel->stopAllOnAnyFailure(true);
39+
$parallel->process($this->task(Exec::class, sprintf('touch %s && false', escapeshellarg($filenameOk))));
40+
$parallel->process($this->task(Exec::class, sprintf('sleep 3 && touch %s', escapeshellarg($filenameKo))));
41+
$parallel->run();
42+
43+
$this->assertFileExists($filenameOk);
44+
$this->assertFileNotExists($filenameKo);
45+
}
46+
}

0 commit comments

Comments
 (0)