-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProcess.php
More file actions
105 lines (90 loc) · 2.3 KB
/
Process.php
File metadata and controls
105 lines (90 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace SfCod\SocketIoBundle\Service;
/**
* Class Process.
*
* @package SfCod\SocketIoBundle
*/
class Process
{
/**
* @var array
*/
private static $_inWork = [];
/**
* Run script name.
*
* @var string
*/
private $scriptName;
/**
* Bin folder path.
*
* @var string
*/
private $binPath;
/**
* Process constructor.
*/
public function __construct(string $scriptName, string $binPath)
{
$this->scriptName = $scriptName;
$this->binPath = $binPath;
}
/**
* Get parallel processes count.
*/
public function getParallelEnv(): int
{
return getenv('SOCKET_IO.PARALLEL') ? getenv('SOCKET_IO.PARALLEL') : 10;
}
/**
* Run process. If more then limit then wait and try run process on more time.
*
* @return \Symfony\Component\Process\Process
*/
public function run(string $handle, array $data)
{
$this->inWork();
while (count(self::$_inWork) >= $this->getParallelEnv()) {
usleep(100);
$this->inWork();
}
return $this->push($handle, $data);
}
/**
* In work processes.
*/
private function inWork()
{
foreach (self::$_inWork as $i => $proccess) {
/** @var \Symfony\Component\Process\Process $proccess * */
if (false === $proccess->isRunning()) {
unset(self::$_inWork[$i]);
}
// Client should not get any errors. But if get any errors they will be displayed on a display.
if ($proccess->getErrorOutput()) {
echo $proccess->getErrorOutput();
}
}
}
/**
* Create cmd process and push to queue.
*/
private function push(string $handle, array $data): \Symfony\Component\Process\Process
{
$cmd = [
'php',
$this->scriptName,
'socket-io:process',
'--handler=' . escapeshellarg($handle),
'--data=' . escapeshellarg(serialize($data)),
'--env=prod',
];
$process = new \Symfony\Component\Process\Process($cmd, $this->binPath);
$process->setTimeout(10);
$process->start();
self::$_inWork[] = $process;
return $process;
}
}