-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskRunner.php
executable file
·301 lines (248 loc) · 9.26 KB
/
TaskRunner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env php
<?php
require_once(dirname(__FILE__) . '/../functions.php');
class ProcessManager {
/** Job server information. */
private $server;
/** Redis Host */
private $redisHost;
/** React-PHP Event Loop. */
private $loop;
/** Job worker information. */
private $jobs = [];
/** Worker check Timer. */
private $checkTimer;
/** Are we stopping? */
private $stopping = true;
/**
* Create a new ProcessManager
*
* @param $server Job Server information
*/
public function __construct($server, $redis) {
$server['type'] = 'rabbitmq';
$this->server = $server;
$loop = React\EventLoop\Factory::create();
$this->loop = $loop;
$this->redisHost = $redis['host'];
$this->redisPort = isset($redis['port']) ? $redis['port'] : '';
echo showTime(), ' ', 'Creating ProcessManager for server type: ', $server['type'], "\n";
echo showTime(), ' ', "\t", 'Server: ', $server['host'], ':', $server['port'], "\n";
echo showTime(), ' ', "\t", 'Redis Host: ', $this->redisHost, (!empty($this->redisPort) ? ':' . $this->redisPort : ''), "\n";
}
/**
* Run the process manager.
*/
public function run() {
$this->stopping = false;
// Timer to ensure that we have all of our workers running.
$this->checkTimer = $this->loop->addPeriodicTimer(10, function() {
$this->checkWorkers();
});
// Force start all the workers
$this->checkWorkers();
// Begin the event loop.
echo showTime(), ' ', 'Running.', "\n";
$this->loop->run();
}
/**
* Stop the process manager.
*/
public function stop() {
if ($this->stopping) {
echo showTime(), ' ', 'Force Stopping!', "\n";
$this->loop->stop();
return;
}
echo showTime(), ' ', 'Stopping...', "\n";
$this->stopping = true;
// Stop the check timer
$this->loop->cancelTimer($this->checkTimer);
// Kill all the worker processes.
foreach ($this->jobs as $function => $functionInfo) {
$this->jobs[$function]['maxWorkers'] = 0;
$this->jobs[$function]['maxJobs'] = 0;
foreach ($functionInfo['workers'] as $pid => $proc) {
$proc['process']->terminate(SIGTERM);
}
}
}
/**
* Add a new Job Type.
*
* @param $function Function Name
* @param $workerConfig Worker config.
*/
public function addJob($function, $workerConfig) {
if (isset($workerConfig['include']) && !$workerConfig['include']) {
echo showTime(), ' ', 'Excluding worker type: ', $function, "\n";
return;
}
if (isset($this->jobs[$function])) { return; }
$this->jobs[$function] = ['maxWorkers' => isset($workerConfig['processes']) ? $workerConfig['processes'] : 1,
'maxJobs' => isset($workerConfig['maxJobs']) ? $workerConfig['maxJobs'] : 1,
'workers' => []
];
echo showTime(), ' ', 'Adding worker type: ', $function, "\n";
echo showTime(), ' ', "\t", 'Processes: ', $this->jobs[$function]['maxWorkers'], "\n";
echo showTime(), ' ', "\t", 'Max Jobs: ', $this->jobs[$function]['maxJobs'], "\n";
}
/**
* Check that we have all of our required workers.
*/
private function checkWorkers() {
if ($this->stopping) { return; }
foreach (array_keys($this->jobs) as $function) {
$this->startWorkers($function);
}
}
/**
* Start all the workers for a given process type.
*
* @param $function Function to start workers for.
*/
private function startWorkers($function) {
if ($this->stopping) { return; }
while (count($this->jobs[$function]['workers']) < $this->jobs[$function]['maxWorkers']) {
$this->startWorker($function);
}
}
/**
* Start a new worker for a given function.
*
* @param $function Function to start worker for.
*/
private function startWorker($function) {
if ($this->stopping) { return; }
// Create a new runWorker process.
$process = new React\ChildProcess\Process('exec /usr/bin/env php ' . escapeshellarg(__DIR__ . '/runWorker.php') . ' ' . escapeshellarg($function));
$process->start($this->loop);
// Store the process.
$pid = $process->getPid();
$this->jobs[$function]['workers'][$pid] = ['jobcount' => 0, 'process' => $process, 'buffers' => ['stdout' => '', 'stderr' => ''], 'currentJob' => null];
// Register handlers for output from the worker.
// STDOUT data from the worker.
$process->stdout->on('data', function ($data) use ($function, $pid) {
$this->jobs[$function]['workers'][$pid]['buffers']['stdout'] .= $data;
$lines = explode("\n", $this->jobs[$function]['workers'][$pid]['buffers']['stdout']);
$this->jobs[$function]['workers'][$pid]['buffers']['stdout'] = array_pop($lines);
foreach ($lines as $line) {
if (!empty($line)) {
$this->processWorkerData($function, $pid, $line);
}
}
});
// STDERR data from the worker.
$process->stderr->on('data', function ($data) use ($function, $pid) {
$this->jobs[$function]['workers'][$pid]['buffers']['stderr'] .= $data;
$lines = explode("\n", $this->jobs[$function]['workers'][$pid]['buffers']['stderr']);
$this->jobs[$function]['workers'][$pid]['buffers']['stderr'] = array_pop($lines);
foreach ($lines as $line) {
if (!empty($line)) {
$this->processWorkerData($function, $pid, '# STDERR: ' . $line);
}
}
});
// Process terminated.
$process->on('exit', function($exitCode, $termSignal) use ($function, $pid) {
$this->processWorkerExit($function, $pid, $exitCode, $termSignal);
});
// Start the worker.
$this->processWorkerStart($function, $pid);
}
/**
* Called when a worker starts.
*
* @param $function Function that this worker is for
* @param $pid Process ID for this worker
*/
private function processWorkerStart($function, $pid) {
echo showTime(), ' ', '[', $function, '::', $pid, '] Process started.', "\n";
// Configure the worker.
$process = $this->jobs[$function]['workers'][$pid]['process'];
$process->stdin->write('setRedisHost ' . $this->redisHost . ' ' . $this->redisPort . "\n");
$process->stdin->write('addFunction ' . $function . "\n");
$process->stdin->write('setTaskServer ' . $this->server['type'] . ' ' . $this->server['host'] . ' ' . $this->server['port'] . "\n");
$process->stdin->write('run' . "\n");
}
private function showIdent($function, $pid) {
$jobfunc = isset($this->jobs[$function]) ? $this->jobs[$function] : NULL;
$proc = isset($jobfunc['workers'][$pid]) ? $jobfunc['workers'][$pid] : NULL;
$result = '[' . $function . '::' . $pid;
if ($jobfunc != null && $proc != null) {
$result .= ' (';
$result .= $proc['jobcount'];
$result .= '/';
$result .= $jobfunc['maxJobs'];
$result .= ')';
} else {
$result .= ' (END)';
}
$result .= ']';
return $result;
}
/**
* Called when a worker starts sends data.
*
* @param $function Function that this worker is for
* @param $pid Process ID for this worker
* @param $data Data from the worker
*/
private function processWorkerData($function, $pid, $data) {
echo showTime(), ' ', $this->showIdent($function, $pid), '> ', trim($data), "\n";
$bits = explode(" ", $data, 2);
$cmd = $bits[0];
$args = isset($bits[1]) ? $bits[1] : '';
$jobFinished = false;
// Count the jobs from the worker, restarting it as needed.
if ($cmd == 'JOB') {
$this->jobs[$function]['workers'][$pid]['jobcount']++;
$process = $this->jobs[$function]['workers'][$pid];
if ($process['jobcount'] >= $this->jobs[$function]['maxJobs']) {
echo showTime(), ' ', $this->showIdent($function, $pid), ' Terminating process after ' . $process['jobcount'] . ' jobs.', "\n";
$process['process']->terminate(SIGTERM);
// Replace the worker immediately if we stop it.
$this->startWorker($function);
}
// Also, learn this worker's job ID.
$this->jobs[$function]['workers'][$pid]['currentJob'] = $args;
} else if ($cmd == 'EXCEPTION') {
EventQueue::get()->publish('worker.error', [$function, $args]);
$jobFinished = true;
} else if ($cmd == 'RESULT') {
$jobFinished = true;
}
if ($this->jobs[$function]['workers'][$pid]['currentJob'] !== null) {
EventQueue::get()->publish('job.log', [$this->jobs[$function]['workers'][$pid]['currentJob'], $data]);
if ($jobFinished) {
$this->jobs[$function]['workers'][$pid]['currentJob'] = null;
}
}
}
/**
* Called when a worker exits.
*
* @param $function Function that this worker is for
* @param $pid Process ID for this worker
*/
private function processWorkerExit($function, $pid, $exitCode, $termSignal) {
echo showTime(), ' ', $this->showIdent($function, $pid), ' Process exited. (', $exitCode, '/', $termSignal, ')', "\n";
unset($this->jobs[$function]['workers'][$pid]);
}
}
if (empty($config['redis']) || !class_exists('Redis')) {
die('Redis is required for TaskRunner.');
}
// Create the process manager
$pm = new ProcessManager($config['rabbitmq'], ['host' => $config['redis'], 'port' => $config['redisPort']]);
// Add the workers.
foreach ($config['jobworkers'] as $worker => $conf) {
$pm->addJob($worker, $conf);
}
// Deal with shutdown requests
$shutdownFunc = function() use ($pm) { $pm->stop(); };
pcntl_signal(SIGINT, $shutdownFunc);
pcntl_signal(SIGTERM, $shutdownFunc);
pcntl_async_signals(true);
// Run the ProcessManager
$pm->run();