FixedPool::wait()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 7.7777
c 1
b 1
f 0
cc 8
eloc 12
nc 9
nop 2
crap 8
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Jenner
5
 * Date: 2015/11/2
6
 * Time: 17:45
7
 */
8
9
namespace Jenner\SimpleFork;
10
11
12
/**
13
 * fixed pool
14
 *
15
 * @package Jenner\SimpleFork
16
 */
17
class FixedPool extends AbstractPool
18
{
19
    /**
20
     * @var int max process count
21
     */
22
    protected $max;
23
24
    /**
25
     * @param int $max
26
     */
27 3
    public function __construct($max = 4)
28
    {
29 3
        $this->max = $max;
30 3
    }
31
32 3
    public function execute(Process $process)
33
    {
34 3
        Utils::checkOverwriteRunMethod(get_class($process));
35
36 3
        if ($this->aliveCount() < $this->max && !$process->isStarted()) {
37 3
            $process->start();
38 3
        }
39 3
        array_push($this->processes, $process);
40 3
    }
41
42
    /**
43
     * wait for all process done
44
     *
45
     * @param bool $block block the master process
46
     * to keep the sub process count all the time
47
     * @param int $interval check time interval
48
     */
49 3
    public function wait($block = false, $interval = 100)
50
    {
51
        do {
52 3
            if ($this->isFinished()) {
53 3
                return;
54
            }
55 3
            parent::wait(false);
56 3
            if ($this->aliveCount() < $this->max) {
57 3
                foreach ($this->processes as $process) {
58 3
                    if ($process->isStarted()) continue;
59 3
                    $process->start();
60 3
                    if ($this->aliveCount() >= $this->max) break;
61 3
                }
62 3
            }
63 3
            $block ? usleep($interval) : null;
64 3
        } while ($block);
65 3
    }
66
67
}