|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Paraunit\Runner; |
|
5
|
|
|
|
|
6
|
|
|
use Paraunit\Filter\Filter; |
|
7
|
|
|
use Paraunit\Lifecycle\EngineEvent; |
|
8
|
|
|
use Paraunit\Lifecycle\ProcessEvent; |
|
9
|
|
|
use Paraunit\Process\ProcessBuilderFactory; |
|
10
|
|
|
use Paraunit\Process\SymfonyProcessWrapper; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
12
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Runner |
|
16
|
|
|
* @package Paraunit\Runner |
|
17
|
|
|
*/ |
|
18
|
|
|
class Runner implements EventSubscriberInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var ProcessBuilderFactory */ |
|
21
|
|
|
private $processBuilderFactory; |
|
22
|
|
|
|
|
23
|
|
|
/** @var EventDispatcherInterface */ |
|
24
|
|
|
private $eventDispatcher; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Filter */ |
|
27
|
|
|
private $filter; |
|
28
|
|
|
|
|
29
|
|
|
/** @var PipelineCollection */ |
|
30
|
|
|
private $pipelineCollection; |
|
31
|
|
|
|
|
32
|
|
|
/** @var \SplQueue */ |
|
33
|
|
|
private $queuedProcesses; |
|
34
|
|
|
|
|
35
|
|
|
/** @var int */ |
|
36
|
|
|
private $exitCode; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
40
|
|
|
* @param ProcessBuilderFactory $processFactory |
|
41
|
|
|
* @param Filter $filter |
|
42
|
|
|
* @param PipelineCollection $pipelineCollection |
|
43
|
|
|
*/ |
|
44
|
33 |
|
public function __construct( |
|
45
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
46
|
|
|
ProcessBuilderFactory $processFactory, |
|
47
|
|
|
Filter $filter, |
|
48
|
|
|
PipelineCollection $pipelineCollection |
|
49
|
|
|
) { |
|
50
|
33 |
|
$this->eventDispatcher = $eventDispatcher; |
|
51
|
33 |
|
$this->processBuilderFactory = $processFactory; |
|
52
|
33 |
|
$this->filter = $filter; |
|
53
|
33 |
|
$this->pipelineCollection = $pipelineCollection; |
|
54
|
33 |
|
$this->queuedProcesses = new \SplQueue(); |
|
55
|
33 |
|
$this->exitCode = 0; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
56 |
|
public static function getSubscribedEvents(): array |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
56 |
|
ProcessEvent::PROCESS_PARSING_COMPLETED => 'onProcessParsingCompleted', |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return int The final exit code: 0 if no failures, 10 otherwise |
|
67
|
|
|
*/ |
|
68
|
17 |
|
public function run(): int |
|
69
|
|
|
{ |
|
70
|
17 |
|
$this->eventDispatcher->dispatch(EngineEvent::BEFORE_START); |
|
71
|
|
|
|
|
72
|
17 |
|
$this->createProcessQueue(); |
|
73
|
|
|
|
|
74
|
17 |
|
$this->eventDispatcher->dispatch(EngineEvent::START); |
|
75
|
|
|
|
|
76
|
17 |
|
while ($this->pipelineCollection->checkRunningState() || $this->pushToPipeline()) { |
|
77
|
16 |
|
usleep(100); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
17 |
|
$this->eventDispatcher->dispatch(EngineEvent::END); |
|
81
|
|
|
|
|
82
|
17 |
|
return $this->exitCode; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param ProcessEvent $processEvent |
|
87
|
|
|
*/ |
|
88
|
29 |
|
public function onProcessParsingCompleted(ProcessEvent $processEvent) |
|
89
|
|
|
{ |
|
90
|
29 |
|
$process = $processEvent->getProcess(); |
|
91
|
|
|
|
|
92
|
29 |
|
if ($process->isToBeRetried()) { |
|
93
|
5 |
|
$process->reset(); |
|
94
|
5 |
|
$process->increaseRetryCount(); |
|
95
|
|
|
|
|
96
|
5 |
|
$this->queuedProcesses->enqueue($process); |
|
97
|
|
|
|
|
98
|
5 |
|
$this->eventDispatcher->dispatch(ProcessEvent::PROCESS_TO_BE_RETRIED, new ProcessEvent($process)); |
|
99
|
28 |
|
} elseif ($process->getExitCode() !== 0) { |
|
100
|
11 |
|
$this->exitCode = 10; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
17 |
|
private function createProcessQueue() |
|
105
|
|
|
{ |
|
106
|
17 |
|
foreach ($this->filter->filterTestFiles() as $file) { |
|
107
|
16 |
|
$processBuilder = $this->processBuilderFactory->create($file); |
|
108
|
16 |
|
$this->queuedProcesses->enqueue(new SymfonyProcessWrapper($processBuilder, $file)); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
17 |
|
private function pushToPipeline(): bool |
|
113
|
|
|
{ |
|
114
|
17 |
|
$somethingHasBeenPushed = false; |
|
115
|
17 |
|
while (! $this->queuedProcesses->isEmpty() && $this->pipelineCollection->hasEmptySlots()) { |
|
116
|
16 |
|
$this->pipelineCollection->push($this->queuedProcesses->dequeue()); |
|
117
|
16 |
|
$somethingHasBeenPushed = true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
17 |
|
return $somethingHasBeenPushed; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|