1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Jenner |
5
|
|
|
* Date: 2015/11/3 |
6
|
|
|
* Time: 14:37 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Jenner\SimpleFork; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* processes' pool |
14
|
|
|
* |
15
|
|
|
* @package Jenner\SimpleFork |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractPool |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* process list |
21
|
|
|
* |
22
|
|
|
* @var Process[] |
23
|
|
|
*/ |
24
|
|
|
protected $processes = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* get process by pid |
28
|
|
|
* |
29
|
|
|
* @param $pid |
30
|
|
|
* @return null|Process |
31
|
|
|
*/ |
32
|
3 |
|
public function getProcessByPid($pid) |
33
|
|
|
{ |
34
|
3 |
|
foreach ($this->processes as $process) { |
35
|
3 |
|
if ($process->getPid() == $pid) { |
36
|
3 |
|
return $process; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* shutdown sub process and no wait. it is dangerous, |
45
|
|
|
* maybe the sub process is working. |
46
|
|
|
*/ |
47
|
3 |
|
public function shutdownForce() |
48
|
|
|
{ |
49
|
3 |
|
$this->shutdown(SIGKILL); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* shutdown all process |
54
|
|
|
* |
55
|
|
|
* @param int $signal |
56
|
|
|
*/ |
57
|
9 |
|
public function shutdown($signal = SIGTERM) |
58
|
|
|
{ |
59
|
9 |
|
foreach ($this->processes as $process) { |
60
|
9 |
|
if ($process->isRunning()) { |
61
|
9 |
|
$process->shutdown(true, $signal); |
62
|
9 |
|
} |
63
|
9 |
|
} |
64
|
9 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* if all processes are stopped |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
3 |
|
public function isFinished() |
72
|
|
|
{ |
73
|
3 |
|
foreach ($this->processes as $process) { |
74
|
3 |
|
if (!$process->isStopped()) { |
75
|
3 |
|
return false; |
76
|
|
|
} |
77
|
3 |
|
} |
78
|
3 |
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* waiting for the sub processes to exit |
83
|
|
|
* |
84
|
|
|
* @param bool|true $block if true the parent process will be blocked until all |
85
|
|
|
* sub processes exit. else it will check if there are processes that had been exited once and return. |
86
|
|
|
* @param int $sleep when $block is true, it will check sub processes every $sleep minute |
87
|
|
|
*/ |
88
|
9 |
|
public function wait($block = true, $sleep = 100) |
89
|
|
|
{ |
90
|
|
|
do { |
91
|
9 |
|
foreach ($this->processes as $process) { |
92
|
9 |
|
if (!$process->isRunning()) { |
93
|
9 |
|
continue; |
94
|
|
|
} |
95
|
9 |
|
} |
96
|
9 |
|
usleep($sleep); |
97
|
9 |
|
} while ($block && $this->aliveCount() > 0); |
98
|
9 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* get the count of running processes |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
21 |
|
public function aliveCount() |
106
|
|
|
{ |
107
|
21 |
|
$count = 0; |
108
|
21 |
|
foreach ($this->processes as $process) { |
109
|
21 |
|
if ($process->isRunning()) { |
110
|
15 |
|
$count++; |
111
|
15 |
|
} |
112
|
21 |
|
} |
113
|
|
|
|
114
|
21 |
|
return $count; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* get process by name |
119
|
|
|
* |
120
|
|
|
* @param string $name process name |
121
|
|
|
* @return Process|null |
122
|
|
|
*/ |
123
|
3 |
|
public function getProcessByName($name) |
124
|
|
|
{ |
125
|
3 |
|
foreach ($this->processes as $process) { |
126
|
3 |
|
if ($process->name() == $name) { |
127
|
3 |
|
return $process; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* remove process by name |
136
|
|
|
* |
137
|
|
|
* @param string $name process name |
138
|
|
|
* @throws \RuntimeException |
139
|
|
|
*/ |
140
|
|
|
public function removeProcessByName($name) |
141
|
|
|
{ |
142
|
|
|
foreach ($this->processes as $key => $process) { |
143
|
|
|
if ($process->name() == $name) { |
144
|
|
|
if ($process->isRunning()) { |
145
|
|
|
throw new \RuntimeException("can not remove a running process"); |
146
|
|
|
} |
147
|
|
|
unset($this->processes[$key]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* remove exited process |
154
|
|
|
*/ |
155
|
|
|
public function removeExitedProcess() |
156
|
|
|
{ |
157
|
|
|
foreach ($this->processes as $key => $process) { |
158
|
|
|
if ($process->isStopped()) { |
159
|
|
|
unset($this->processes[$key]); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* return process count |
166
|
|
|
* |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
3 |
|
public function count() |
170
|
|
|
{ |
171
|
3 |
|
return count($this->processes); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* get all processes |
176
|
|
|
* |
177
|
|
|
* @return Process[] |
178
|
|
|
*/ |
179
|
3 |
|
public function getProcesses() |
180
|
|
|
{ |
181
|
3 |
|
return $this->processes; |
182
|
|
|
} |
183
|
|
|
} |