Completed
Push — master ( cfd1ff...e920d0 )
by Hu
02:46
created

AbstractPool::aliveCount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
crap 3
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 3
                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 9
    public function wait($block = true, $sleep = 100)
106
    {
107
        do {
108 9
            foreach ($this->processes as $process) {
109 9
                if (!$process->isRunning()) {
110 9
                    continue;
111
                }
112 9
            }
113 9
            usleep($sleep);
114 9
        } while ($block && $this->aliveCount() > 0);
115
    }
116
}