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