1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yiicod\socketio; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\HtmlPurifier; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Process |
10
|
|
|
* |
11
|
|
|
* @package SfCod\SocketIoBundle |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Process |
16
|
|
|
* |
17
|
|
|
* @package yiicod\socketio |
18
|
|
|
*/ |
19
|
|
|
class Process |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $_inWork = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var |
28
|
|
|
*/ |
29
|
|
|
public $yiiAlias; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function getParallelEnv(): int |
35
|
|
|
{ |
36
|
|
|
return getenv('SOCKET_IO.PARALLEL') ? getenv('SOCKET_IO.PARALLEL') : 10; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Run process. If more then limit then wait and try run process on more time. |
41
|
|
|
* |
42
|
|
|
* @param string $handle |
43
|
|
|
* @param array $data |
44
|
|
|
* |
45
|
|
|
* @return \Symfony\Component\Process\Process |
46
|
|
|
*/ |
47
|
|
|
public function run(string $handle, array $data) |
48
|
|
|
{ |
49
|
|
|
$this->inWork(); |
50
|
|
|
|
51
|
|
|
while (count(self::$_inWork) >= $this->getParallelEnv()) { |
52
|
|
|
usleep(100); |
53
|
|
|
|
54
|
|
|
$this->inWork(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->push($handle, $data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* In work processes |
62
|
|
|
*/ |
63
|
|
|
private function inWork() |
64
|
|
|
{ |
65
|
|
|
foreach (self::$_inWork as $i => $proccess) { |
66
|
|
|
if (false === $proccess->isRunning()) { |
67
|
|
|
unset(self::$_inWork[$i]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create cmd process and push to queue. |
74
|
|
|
* |
75
|
|
|
* @param string $handle |
76
|
|
|
* @param array $data |
77
|
|
|
* |
78
|
|
|
* @return \Symfony\Component\Process\Process |
79
|
|
|
*/ |
80
|
|
|
private function push(string $handle, array $data): \Symfony\Component\Process\Process |
81
|
|
|
{ |
82
|
|
|
$cmd = HtmlPurifier::process(sprintf('php yii socketio/process %s %s', escapeshellarg($handle), escapeshellarg(json_encode($data)))); |
83
|
|
|
|
84
|
|
|
if (is_null($this->yiiAlias)) { |
85
|
|
|
if (file_exists(Yii::getAlias('@app/yii'))) { |
86
|
|
|
$this->yiiAlias = '@app'; |
87
|
|
|
} elseif (file_exists(Yii::getAlias('@app/../yii'))) { |
88
|
|
|
$this->yiiAlias = '@app/../'; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$process = new \Symfony\Component\Process\Process($cmd, Yii::getAlias($this->yiiAlias)); |
|
|
|
|
93
|
|
|
$process->setTimeout(10); |
94
|
|
|
$process->start(); |
95
|
|
|
|
96
|
|
|
self::$_inWork[] = $process; |
97
|
|
|
|
98
|
|
|
return $process; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.