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 all process |
45
|
|
|
* |
46
|
|
|
* @param int $signal |
47
|
|
|
*/ |
48
|
9 |
|
public function shutdown($signal = SIGTERM) |
49
|
|
|
{ |
50
|
9 |
|
foreach ($this->processes as $process) { |
51
|
9 |
|
if ($process->isRunning()) { |
52
|
9 |
|
$process->shutdown(true, $signal); |
53
|
9 |
|
} |
54
|
9 |
|
} |
55
|
9 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* shutdown sub process and no wait. it is dangerous, |
59
|
|
|
* maybe the sub process is working. |
60
|
|
|
*/ |
61
|
3 |
|
public function shutdownForce() |
62
|
|
|
{ |
63
|
3 |
|
$this->shutdown(SIGKILL); |
64
|
3 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* get the count of running processes |
68
|
|
|
* |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
21 |
|
public function aliveCount() |
72
|
|
|
{ |
73
|
21 |
|
$count = 0; |
74
|
21 |
|
foreach ($this->processes as $process) { |
75
|
21 |
|
if ($process->isRunning()) { |
76
|
15 |
|
$count++; |
77
|
15 |
|
} |
78
|
21 |
|
} |
79
|
|
|
|
80
|
21 |
|
return $count; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* if all processes are stopped |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
3 |
|
public function isFinished() |
89
|
|
|
{ |
90
|
3 |
|
foreach ($this->processes as $process) { |
91
|
3 |
|
if (!$process->isStopped()) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
3 |
|
} |
95
|
3 |
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* waiting for the sub processes to exit |
100
|
|
|
* |
101
|
|
|
* @param bool|true $block if true the parent process will be blocked until all |
102
|
|
|
* sub processes exit. else it will check if thers are processes that had been exited once and return. |
103
|
|
|
* @param int $sleep when $block is true, it will check sub processes every $sleep minute |
104
|
|
|
*/ |
105
|
6 |
|
public function wait($block = true, $sleep = 100) |
106
|
|
|
{ |
107
|
|
|
do { |
108
|
6 |
|
foreach ($this->processes as $process) { |
109
|
6 |
|
if (!$process->isRunning()) { |
110
|
6 |
|
continue; |
111
|
|
|
} |
112
|
6 |
|
} |
113
|
6 |
|
usleep($sleep); |
114
|
6 |
|
} while ($block && $this->aliveCount() > 0); |
115
|
|
|
} |
116
|
|
|
} |