11<?php
2-
2+ declare (strict_types= 1 );
33
44namespace Async ;
55
66use SuperClosure \Serializer ;
77use Symfony \Component \Console \Exception \InvalidArgumentException ;
88
9- /**
10- * Class AsyncCall
11- * @package Async
12- */
139class AsyncCall
1410{
15- const CONSOLE_EXECUTE = ' php ' . __DIR__ . '/../../bin/console app:run-child-process ' ;
11+ private const CONSOLE_LOCATION = __DIR__ . '/../../bin/console ' ;
1612
1713 /**
1814 * @var bool
@@ -30,87 +26,76 @@ class AsyncCall
3026 * @var int
3127 */
3228 private static $ processesLimit = 0 ;
29+ private static $ processAmount = 0 ;
3330
34- /**
35- * @param $processesLimit
36- * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
37- */
38- public static function setProcessLimit ($ processesLimit )
31+ public static function setProcessLimit (int $ processesLimit ): void
3932 {
4033 if ($ processesLimit < 0 ) {
41- throw new InvalidArgumentException ('Processes limit Must be possitive itiger ' );
34+ throw new InvalidArgumentException ('Processes limit Must be positive integer ' );
4235 }
43- self ::$ processesLimit = ( int ) $ processesLimit ;
36+ self ::$ processesLimit = $ processesLimit ;
4437 }
4538
46- /**
47- * @param callable $job
48- * @param callable $callback
49- * @param callable $onError
50- * @param float $timeout
51- * @param float $idleTimeout
52- * @throws \Symfony\Component\Process\Exception\InvalidArgumentException
53- * @throws \Symfony\Component\Process\Exception\RuntimeException
54- * @throws \Symfony\Component\Process\Exception\LogicException
55- * @throws \RuntimeException
56- */
5739 public static function run (
5840 callable $ job ,
5941 callable $ callback = null ,
6042 callable $ onError = null ,
61- $ timeout = null ,
62- $ idleTimeout = null
63- ) {
43+ float $ timeout = null ,
44+ float $ idleTimeout = null
45+ ): void {
6446 self ::registerShutdownFunction ();
6547
6648 if (!self ::$ serializer ) {
6749 self ::$ serializer = new Serializer ();
6850 }
6951
7052 // we got process limit so wait for them to finish
71- if (0 !== self ::$ processesLimit && self ::$ processesLimit >= count ( self ::$ processList ) ) {
53+ if (0 !== self ::$ processesLimit && self ::$ processesLimit >= self ::$ processAmount ) {
7254 self ::waitForProcessesToFinish (self ::$ processesLimit );
7355 }
7456
75- $ process = new AsyncProcess (self ::CONSOLE_EXECUTE . base64_encode (self ::$ serializer ->serialize ($ job )));
57+ $ process = new AsyncProcess (
58+ [
59+ self ::CONSOLE_LOCATION ,
60+ AsyncChildCommand::COMMAND_NAME ,
61+ base64_encode (self ::$ serializer ->serialize ($ job ))
62+ ]
63+ );
7664 $ process ->setTimeout ($ timeout );
7765 $ process ->setIdleTimeout ($ idleTimeout );
7866 $ process ->startJob ($ callback , $ onError );
7967
80- //echo $process->getCommandLine() . PHP_EOL;
8168 self ::$ processList [] = $ process ;
69+ self ::$ processAmount ++;
8270 }
8371
84- private static function registerShutdownFunction ()
72+ private static function registerShutdownFunction (): void
8573 {
8674 if (!self ::$ shutdownFunctionRegistered ) {
8775 register_shutdown_function (
88- function () {
76+ static function () {
8977 self ::waitForProcessesToFinish ();
9078 }
9179 );
9280 self ::$ shutdownFunctionRegistered = true ;
9381 }
9482 }
9583
96- /**
97- * @param int $maxProcessToWait
98- */
99- private static function waitForProcessesToFinish ($ maxProcessToWait = 0 )
84+ private static function waitForProcessesToFinish (int $ maxProcessToWait = 0 ): void
10085 {
101- while (true ) {
102- $ processAmount = count (self ::$ processList );
103-
104- if (0 === $ processAmount ) {
105- break ;
106- }
107- if ($ maxProcessToWait > $ processAmount ) {
86+ for (; ;) {
87+ if (0 === self ::$ processAmount || $ maxProcessToWait > self ::$ processAmount ) {
10888 break ;
10989 }
11090
11191 foreach (self ::$ processList as $ i => $ process ) {
112- if ($ process ->getStatus () === AsyncProcess::STATUS_TERMINATED || (!$ process ->hasCallbackSet () && !$ process ->hasOnErrorSet ())) {
92+ if (
93+ $ process ->getStatus () === AsyncProcess::STATUS_TERMINATED ||
94+ (!$ process ->hasCallbackSet () && !$ process ->hasOnErrorSet ())
95+ ) {
11396 unset(self ::$ processList [$ i ]);
97+ self ::$ processAmount --;
98+
11499 continue ;
115100 }
116101 }
0 commit comments