1 | <?php declare(strict_types=1); |
||
26 | class ParallelExecutor implements ExecutorInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var InputInterface |
||
30 | */ |
||
31 | private $input; |
||
32 | |||
33 | /** |
||
34 | * @var OutputInterface |
||
35 | */ |
||
36 | private $output; |
||
37 | |||
38 | /** |
||
39 | * @var Informer |
||
40 | */ |
||
41 | private $informer; |
||
42 | |||
43 | /** |
||
44 | * @var Application |
||
45 | */ |
||
46 | private $console; |
||
47 | |||
48 | public function __construct( |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function run(array $tasks, array $hosts) |
||
117 | |||
118 | /** |
||
119 | * Run task on hosts. |
||
120 | * |
||
121 | * @param Host[] $hosts |
||
122 | * @return int |
||
123 | */ |
||
124 | private function runTask(array $hosts, Task $task): int |
||
125 | { |
||
126 | $processes = []; |
||
127 | |||
128 | foreach ($hosts as $host) { |
||
129 | if ($task->shouldBePerformed($host)) { |
||
130 | $processes[$host->getHostname()] = $this->getProcess($host, $task); |
||
131 | if ($task->isOnce()) { |
||
132 | $task->setHasRun(); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | $callback = function (string $type, string $host, string $output) { |
||
138 | $output = rtrim($output); |
||
139 | if (strlen($output) !== 0) { |
||
140 | $this->output->writeln($output); |
||
141 | } |
||
142 | }; |
||
143 | |||
144 | $this->startProcesses($processes); |
||
145 | |||
146 | while ($this->areRunning($processes)) { |
||
147 | $this->gatherOutput($processes, $callback); |
||
148 | usleep(1000); |
||
149 | } |
||
150 | $this->gatherOutput($processes, $callback); |
||
151 | |||
152 | return $this->gatherExitCodes($processes); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Get process for task on host. |
||
157 | */ |
||
158 | protected function getProcess(Host $host, Task $task): Process |
||
159 | { |
||
160 | $dep = PHP_BINARY . ' ' . DEPLOYER_BIN; |
||
161 | $options = $this->generateOptions(); |
||
162 | $arguments = $this->generateArguments(); |
||
163 | $hostname = $host->getHostname(); |
||
164 | $taskName = $task->getName(); |
||
165 | $configFile = $host->get('host_config_storage'); |
||
166 | $value = $this->input->getOption('file'); |
||
167 | $file = $value ? "--file='$value'" : ''; |
||
168 | |||
169 | if ($this->output->isDecorated()) { |
||
170 | $options .= ' --ansi'; |
||
171 | } |
||
172 | |||
173 | $command = "$dep $file worker $arguments $options --hostname $hostname --task $taskName --config-file $configFile"; |
||
174 | $process = Process::fromShellCommandline($command); |
||
175 | |||
176 | if (!defined('DEPLOYER_PARALLEL_PTY')) { |
||
177 | $process->setPty(true); |
||
178 | } |
||
179 | |||
180 | return $process; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Start all of the processes. |
||
185 | * |
||
186 | * @param Process[] $processes |
||
187 | * @return void |
||
188 | */ |
||
189 | protected function startProcesses(array $processes) |
||
195 | |||
196 | /** |
||
197 | * Determine if any of the processes are running. |
||
198 | * |
||
199 | * @param Process[] $processes |
||
200 | * @return bool |
||
201 | */ |
||
202 | protected function areRunning(array $processes): bool |
||
212 | |||
213 | /** |
||
214 | * Gather the output from all of the processes. |
||
215 | * |
||
216 | * @param Process[] $processes |
||
217 | * @param callable $callback |
||
218 | * @return void |
||
219 | */ |
||
220 | protected function gatherOutput(array $processes, callable $callback) |
||
234 | |||
235 | /** |
||
236 | * Gather the cumulative exit code for the processes. |
||
237 | */ |
||
238 | protected function gatherExitCodes(array $processes): int |
||
248 | |||
249 | /** |
||
250 | * Generate options and arguments string. |
||
251 | */ |
||
252 | private function generateOptions(): string |
||
274 | |||
275 | private function generateArguments(): string |
||
286 | } |
||
287 |