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
|
|
|
abstract class AbstractPool |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* process list |
16
|
|
|
* |
17
|
|
|
* @var Process[] |
18
|
|
|
*/ |
19
|
|
|
protected $processes = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* get process by pid |
23
|
|
|
* |
24
|
|
|
* @param $pid |
25
|
|
|
* @return null|Process |
26
|
|
|
*/ |
27
|
3 |
|
public function getProcessByPid($pid) |
28
|
|
|
{ |
29
|
3 |
|
foreach ($this->processes as $process) { |
30
|
3 |
|
if ($process->getPid() == $pid) { |
31
|
3 |
|
return $process; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* shutdown all process |
40
|
|
|
* |
41
|
|
|
* @param int $signal |
42
|
|
|
*/ |
43
|
9 |
|
public function shutdown($signal = SIGTERM) |
44
|
|
|
{ |
45
|
9 |
|
foreach ($this->processes as $process) { |
46
|
9 |
|
if ($process->isRunning()) { |
47
|
9 |
|
$process->shutdown(true, $signal); |
48
|
9 |
|
} |
49
|
9 |
|
} |
50
|
9 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* shutdown sub process and no wait. it is dangerous, |
54
|
|
|
* maybe the sub process is working. |
55
|
|
|
*/ |
56
|
3 |
|
public function shutdownForce() |
57
|
|
|
{ |
58
|
3 |
|
$this->shutdown(SIGKILL); |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* get the count of running processes |
63
|
|
|
* |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
21 |
|
public function aliveCount() |
67
|
|
|
{ |
68
|
21 |
|
$count = 0; |
69
|
21 |
|
foreach ($this->processes as $process) { |
70
|
21 |
|
if ($process->isRunning()) { |
71
|
15 |
|
$count++; |
72
|
15 |
|
} |
73
|
21 |
|
} |
74
|
|
|
|
75
|
21 |
|
return $count; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* if all processes are stopped |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
3 |
|
public function isFinished() |
84
|
|
|
{ |
85
|
3 |
|
foreach ($this->processes as $process) { |
86
|
3 |
|
if (!$process->isStopped()) { |
87
|
3 |
|
return false; |
88
|
|
|
} |
89
|
3 |
|
} |
90
|
3 |
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* waiting for the sub processes to exit |
95
|
|
|
* |
96
|
|
|
* @param bool|true $block if true the parent process will be blocked until all |
97
|
|
|
* sub processes exit. else it will check if thers are processes that had been exited once and return. |
98
|
|
|
* @param int $sleep when $block is true, it will check sub processes every $sleep minute |
99
|
|
|
*/ |
100
|
9 |
|
public function wait($block = true, $sleep = 100) |
101
|
|
|
{ |
102
|
|
|
do { |
103
|
9 |
|
foreach ($this->processes as $process) { |
104
|
9 |
|
if (!$process->isRunning()) { |
105
|
9 |
|
continue; |
106
|
|
|
} |
107
|
9 |
|
} |
108
|
9 |
|
usleep($sleep); |
109
|
9 |
|
} while ($block && $this->aliveCount() > 0); |
110
|
|
|
} |
111
|
|
|
} |