|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Paraunit\Runner; |
|
6
|
|
|
|
|
7
|
|
|
use Paraunit\Filter\Filter; |
|
8
|
|
|
use Paraunit\Lifecycle\EngineEvent; |
|
9
|
|
|
use Paraunit\Lifecycle\ProcessEvent; |
|
10
|
|
|
use Paraunit\Process\ProcessFactoryInterface; |
|
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 ProcessFactoryInterface */ |
|
21
|
|
|
private $processFactory; |
|
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 ProcessFactoryInterface $processFactory |
|
41
|
|
|
* @param Filter $filter |
|
42
|
|
|
* @param PipelineCollection $pipelineCollection |
|
43
|
|
|
*/ |
|
44
|
44 |
|
public function __construct( |
|
45
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
46
|
|
|
ProcessFactoryInterface $processFactory, |
|
47
|
|
|
Filter $filter, |
|
48
|
|
|
PipelineCollection $pipelineCollection |
|
49
|
|
|
) { |
|
50
|
44 |
|
$this->eventDispatcher = $eventDispatcher; |
|
51
|
44 |
|
$this->processFactory = $processFactory; |
|
52
|
44 |
|
$this->filter = $filter; |
|
53
|
44 |
|
$this->pipelineCollection = $pipelineCollection; |
|
54
|
44 |
|
$this->queuedProcesses = new \SplQueue(); |
|
55
|
44 |
|
$this->exitCode = 0; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
70 |
|
public static function getSubscribedEvents(): array |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
70 |
|
ProcessEvent::PROCESS_TERMINATED => 'pushToPipeline', |
|
62
|
70 |
|
ProcessEvent::PROCESS_TO_BE_RETRIED => 'onProcessToBeRetried', |
|
63
|
70 |
|
ProcessEvent::PROCESS_PARSING_COMPLETED => 'onProcessParsingCompleted', |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return int The final exit code: 0 if no failures, 10 otherwise |
|
69
|
|
|
*/ |
|
70
|
26 |
|
public function run(): int |
|
71
|
|
|
{ |
|
72
|
26 |
|
$this->eventDispatcher->dispatch(EngineEvent::BEFORE_START); |
|
73
|
|
|
|
|
74
|
26 |
|
$this->createProcessQueue(); |
|
75
|
|
|
|
|
76
|
26 |
|
$this->eventDispatcher->dispatch(EngineEvent::START); |
|
77
|
|
|
|
|
78
|
|
|
do { |
|
79
|
26 |
|
$this->pushToPipeline(); |
|
80
|
26 |
|
usleep(100); |
|
81
|
26 |
|
$this->pipelineCollection->triggerProcessTermination(); |
|
82
|
26 |
|
} while (! $this->pipelineCollection->isEmpty() || ! $this->queuedProcesses->isEmpty()); |
|
83
|
|
|
|
|
84
|
26 |
|
$this->eventDispatcher->dispatch(EngineEvent::END); |
|
85
|
|
|
|
|
86
|
26 |
|
return $this->exitCode; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param ProcessEvent $processEvent |
|
91
|
|
|
*/ |
|
92
|
36 |
|
public function onProcessParsingCompleted(ProcessEvent $processEvent) |
|
93
|
|
|
{ |
|
94
|
36 |
|
if ($processEvent->getProcess()->getExitCode() !== 0) { |
|
95
|
14 |
|
$this->exitCode = 10; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param ProcessEvent $processEvent |
|
101
|
|
|
*/ |
|
102
|
7 |
|
public function onProcessToBeRetried(ProcessEvent $processEvent) |
|
103
|
|
|
{ |
|
104
|
7 |
|
$this->queuedProcesses->enqueue($processEvent->getProcess()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
26 |
|
private function createProcessQueue() |
|
108
|
|
|
{ |
|
109
|
26 |
|
foreach ($this->filter->filterTestFiles() as $file) { |
|
110
|
25 |
|
$this->queuedProcesses->enqueue( |
|
111
|
25 |
|
$this->processFactory->create($file) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
26 |
|
public function pushToPipeline() |
|
117
|
|
|
{ |
|
118
|
26 |
|
while (! $this->queuedProcesses->isEmpty() && $this->pipelineCollection->hasEmptySlots()) { |
|
119
|
25 |
|
$this->pipelineCollection->push($this->queuedProcesses->dequeue()); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|