3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Deploy \Process ;
7
9
8
10
use Magento \Deploy \Package \Package ;
@@ -125,6 +127,8 @@ public function __construct(
125
127
}
126
128
127
129
/**
130
+ * Adds deployment package.
131
+ *
128
132
* @param Package $package
129
133
* @param Package[] $dependencies
130
134
* @return bool true on success
@@ -140,6 +144,8 @@ public function add(Package $package, array $dependencies = [])
140
144
}
141
145
142
146
/**
147
+ * Returns packages array.
148
+ *
143
149
* @return Package[]
144
150
*/
145
151
public function getPackages ()
@@ -163,6 +169,7 @@ public function process()
163
169
$ this ->assertAndExecute ($ name , $ packages , $ packageJob );
164
170
}
165
171
$ this ->logger ->info ('. ' );
172
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
166
173
sleep (3 );
167
174
foreach ($ this ->inProgress as $ name => $ package ) {
168
175
if ($ this ->isDeployed ($ package )) {
@@ -183,8 +190,6 @@ public function process()
183
190
* @param array $packages
184
191
* @param array $packageJob
185
192
* @return void
186
- *
187
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
188
193
*/
189
194
private function assertAndExecute ($ name , array & $ packages , array $ packageJob )
190
195
{
@@ -208,13 +213,23 @@ private function assertAndExecute($name, array & $packages, array $packageJob)
208
213
}
209
214
}
210
215
}
216
+ $ this ->executePackage ($ package , $ name , $ packages , $ dependenciesNotFinished );
217
+ }
211
218
219
+ /**
220
+ * Executes deployment package.
221
+ *
222
+ * @param Package $package
223
+ * @param string $name
224
+ * @param array $packages
225
+ * @param bool $dependenciesNotFinished
226
+ * @return void
227
+ */
228
+ private function executePackage (Package $ package , string $ name , array &$ packages , bool $ dependenciesNotFinished )
229
+ {
212
230
if (!$ dependenciesNotFinished
213
231
&& !$ this ->isDeployed ($ package )
214
- && (
215
- $ this ->maxProcesses < 2
216
- || (count ($ this ->inProgress ) < $ this ->maxProcesses )
217
- )
232
+ && ($ this ->maxProcesses < 2 || (count ($ this ->inProgress ) < $ this ->maxProcesses ))
218
233
) {
219
234
unset($ packages [$ name ]);
220
235
$ this ->execute ($ package );
@@ -235,6 +250,7 @@ private function awaitForAllProcesses()
235
250
}
236
251
}
237
252
$ this ->logger ->info ('. ' );
253
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
238
254
sleep (5 );
239
255
}
240
256
if ($ this ->isCanBeParalleled ()) {
@@ -244,6 +260,8 @@ private function awaitForAllProcesses()
244
260
}
245
261
246
262
/**
263
+ * Checks if can be parallel.
264
+ *
247
265
* @return bool
248
266
*/
249
267
private function isCanBeParalleled ()
@@ -252,9 +270,10 @@ private function isCanBeParalleled()
252
270
}
253
271
254
272
/**
273
+ * Executes the process.
274
+ *
255
275
* @param Package $package
256
276
* @return bool true on success for main process and exit for child process
257
- * @SuppressWarnings(PHPMD.ExitExpression)
258
277
* @throws \RuntimeException
259
278
*/
260
279
private function execute (Package $ package )
@@ -283,6 +302,7 @@ function () use ($package) {
283
302
);
284
303
285
304
if ($ this ->isCanBeParalleled ()) {
305
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
286
306
$ pid = pcntl_fork ();
287
307
if ($ pid === -1 ) {
288
308
throw new \RuntimeException ('Unable to fork a new process ' );
@@ -297,6 +317,7 @@ function () use ($package) {
297
317
// process child process
298
318
$ this ->inProgress = [];
299
319
$ this ->deployPackageService ->deploy ($ package , $ this ->options , true );
320
+ // phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
300
321
exit (0 );
301
322
} else {
302
323
$ this ->deployPackageService ->deploy ($ package , $ this ->options );
@@ -305,6 +326,8 @@ function () use ($package) {
305
326
}
306
327
307
328
/**
329
+ * Checks if package is deployed.
330
+ *
308
331
* @param Package $package
309
332
* @return bool
310
333
*/
@@ -320,6 +343,7 @@ private function isDeployed(Package $package)
320
343
return false ;
321
344
}
322
345
346
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
323
347
$ result = pcntl_waitpid ($ pid , $ status , WNOHANG );
324
348
if ($ result === $ pid ) {
325
349
$ package ->setState (Package::STATE_COMPLETED );
@@ -334,6 +358,7 @@ private function isDeployed(Package $package)
334
358
);
335
359
336
360
unset($ this ->inProgress [$ package ->getPath ()]);
361
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
337
362
return pcntl_wexitstatus ($ status ) === 0 ;
338
363
} elseif ($ result === -1 ) {
339
364
$ errno = pcntl_errno ();
@@ -350,17 +375,19 @@ private function isDeployed(Package $package)
350
375
}
351
376
352
377
/**
378
+ * Returns process ID or null if not found.
379
+ *
353
380
* @param Package $package
354
381
* @return int|null
355
382
*/
356
383
private function getPid (Package $ package )
357
384
{
358
- return isset ($ this ->processIds [$ package ->getPath ()])
359
- ? $ this ->processIds [$ package ->getPath ()]
360
- : null ;
385
+ return $ this ->processIds [$ package ->getPath ()] ?? null ;
361
386
}
362
387
363
388
/**
389
+ * Checks timeout.
390
+ *
364
391
* @return bool
365
392
*/
366
393
private function checkTimeout ()
@@ -373,6 +400,7 @@ private function checkTimeout()
373
400
*
374
401
* Protect against zombie process
375
402
*
403
+ * @throws \RuntimeException
376
404
* @return void
377
405
*/
378
406
public function __destruct ()
@@ -387,6 +415,7 @@ public function __destruct()
387
415
]
388
416
);
389
417
418
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
390
419
if (pcntl_waitpid ($ pid , $ status ) === -1 ) {
391
420
$ errno = pcntl_errno ();
392
421
$ strerror = pcntl_strerror ($ errno );
0 commit comments