|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SfCod\SocketIoBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Process. |
|
7
|
|
|
* |
|
8
|
|
|
* @package SfCod\SocketIoBundle |
|
9
|
|
|
*/ |
|
10
|
|
|
class Process |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private static $_inWork = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Run script name. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $scriptName; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Bin folder path. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $binPath; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Process constructor. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(string $scriptName, string $binPath) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->scriptName = $scriptName; |
|
37
|
|
|
$this->binPath = $binPath; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get parallel processes count. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getParallelEnv(): int |
|
44
|
|
|
{ |
|
45
|
|
|
return getenv('SOCKET_IO.PARALLEL') ? getenv('SOCKET_IO.PARALLEL') : 10; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Run process. If more then limit then wait and try run process on more time. |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Symfony\Component\Process\Process |
|
52
|
|
|
*/ |
|
53
|
|
|
public function run(string $handle, array $data) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->inWork(); |
|
56
|
|
|
|
|
57
|
|
|
while (count(self::$_inWork) >= $this->getParallelEnv()) { |
|
58
|
|
|
usleep(100); |
|
59
|
|
|
|
|
60
|
|
|
$this->inWork(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->push($handle, $data); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* In work processes. |
|
68
|
|
|
*/ |
|
69
|
|
|
private function inWork() |
|
70
|
|
|
{ |
|
71
|
|
|
foreach (self::$_inWork as $i => $proccess) { |
|
72
|
|
|
/** @var \Symfony\Component\Process\Process $proccess * */ |
|
73
|
|
|
if (false === $proccess->isRunning()) { |
|
74
|
|
|
unset(self::$_inWork[$i]); |
|
75
|
|
|
} |
|
76
|
|
|
// Client should not get any errors. But if get any errors they will be displayed on a display. |
|
77
|
|
|
if ($proccess->getErrorOutput()) { |
|
78
|
|
|
echo $proccess->getErrorOutput(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Create cmd process and push to queue. |
|
85
|
|
|
*/ |
|
86
|
|
|
private function push(string $handle, array $data): \Symfony\Component\Process\Process |
|
87
|
|
|
{ |
|
88
|
|
|
$cmd = sprintf('php %s socket-io:process --handler=%s --data=%s --env=%s', $this->scriptName, escapeshellarg($handle), escapeshellarg(serialize($data)), getenv('APP_ENV')); |
|
89
|
|
|
|
|
90
|
|
|
$process = new \Symfony\Component\Process\Process($cmd, $this->binPath); |
|
|
|
|
|
|
91
|
|
|
$process->setTimeout(10); |
|
92
|
|
|
$process->start(); |
|
93
|
|
|
|
|
94
|
|
|
self::$_inWork[] = $process; |
|
95
|
|
|
|
|
96
|
|
|
return $process; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: