Completed
Pull Request — master (#28)
by Luke
03:31
created

Manager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 17
c 6
b 1
f 2
lcom 1
cbo 9
dl 0
loc 138
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B factory() 0 25 6
A __construct() 0 6 2
A addPool() 0 4 1
B run() 0 25 5
A setOutput() 0 4 1
A getOutput() 0 8 2
1
<?php
2
/**
3
 * Nyx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2016 Luke Steadman
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
 * IN THE SOFTWARE.
24
 *
25
 * @package Nyx
26
 */
27
namespace Nyx;
28
29
use Noodlehaus\Config;
30
31
final class Manager implements OutputableInterface
32
{
33
    /**
34
     * @string
35
     */
36
    const VERSION = '0.1.2';
37
38
    /**
39
     * @string
40
     */
41
    const NAME = 'Nyx - Process Manager';
42
43
    /**
44
     * @var
45
     */
46
    protected $pools;
47
48
    /**
49
     * @var OutputInterface
50
     */
51
    protected $output;
52
53
    /**
54
     * @var Handler
55
     */
56
    protected $handler;
57
58
    /**
59
     * Create a Manager instance using config
60
     *
61
     * @param  $path
62
     * @return static
63
     * @throws \Exception
64
     */
65
    public static function factory($path)
66
    {
67
        $pools = array();
68
69
        try {
70
            if (file_exists($path)) {
71
                $config = Config::load($path);
72
73
                foreach ($config->get('workers', array()) as $worker) {
74
                    $count = array_key_exists('count', $worker) ? $worker['count'] : null;
75
                    $options = array_key_exists('options', $worker) ? $worker['options'] : array();
76
77
                    $pool = new Pool($count);
78
                    $pool->add(new Worker(new Process(new Command($worker['path'], $options))));
79
                    $pools[] = $pool;
80
                }
81
            }
82
        } catch (\Exception $e) {
83
            throw $e;
84
        }
85
86
        $instance = new static($pools);
87
88
        return $instance;
89
    }
90
91
    /**
92
     * Manager constructor
93
     *
94
     * @param array $pools
95
     */
96
    public function __construct(array $pools)
97
    {
98
        foreach ($pools as $pool) {
99
            $this->addPool($pool);
100
        }
101
    }
102
103
    /**
104
     * Add a pool to the manager
105
     *
106
     * @param PoolInterface $pool
107
     */
108
    public function addPool(PoolInterface $pool)
109
    {
110
        $this->pools[] = $pool;
111
    }
112
113
    /**
114
     * Run the manager by looping through the pools and starting the workers.
115
     *
116
     * @return void
117
     */
118
    public function run()
119
    {
120
        if (empty($this->pools)) {
121
            $this->getOutput()->write('[*] No pools found. Exiting.');
122
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
123
        } else {
124
            $this->getOutput()->write("[*] Current PID: " . getmypid());
125
126
            foreach ($this->pools as $pool) {
127
                $pool->boot();
128
            }
129
130
            $this->getOutput()->write('[*] Workers started');
131
132
            $this->handler = new Handler($this);
133
134
            while (true) {
135
                foreach ($this->pools as $pool) {
136
                    $pool->ping();
137
                }
138
139
                sleep(1);
140
            }
141
        }
142
    }
143
144
    /**
145
     * Set the instance of OutputInterface
146
     *
147
     * @param  OutputInterface $output
148
     * @return mixed
149
     */
150
    public function setOutput(OutputInterface $output)
151
    {
152
        $this->output = $output;
153
    }
154
155
    /**
156
     * Returns an instance of OutputInterface
157
     *
158
     * @return OutputInterface
159
     */
160
    public function getOutput()
161
    {
162
        if (is_null($this->output)) {
163
            $this->output = new Console();
164
        }
165
166
        return $this->output;
167
    }
168
}
169