-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunOne.php
executable file
·83 lines (68 loc) · 2.3 KB
/
runOne.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
#!/usr/bin/env php
<?php
require_once(dirname(__FILE__) . '/../functions.php');
require_once(dirname(__FILE__) . '/classes/JobInfo.php');
require_once(dirname(__FILE__) . '/classes/TaskServer.php');
if (!isset($argv[1])) {
echo 'Usage: ', $argv[0], ' <function> [payload]', "\n";
exit(1);
}
function getNextWorkerCommand() {
global $__WorkerCommands;
return array_shift($__WorkerCommands);
}
if (empty($config['redis']) || !class_exists('Redis')) {
die('Redis is required for Task Running.' . "\n");
}
$taskServer = new class extends TaskServer {
private $workers = [];
private $jobs = [];
function __construct() { }
function runBackgroundJob($jobinfo) {
$this->jobs[] = $jobinfo;
}
function runJob($jobinfo) {
$this->jobs[] = $jobinfo;
}
function addTaskWorker($function, $worker) { $this->workers[$function] = $worker; }
function run() {
while (true) {
$jobinfo = !empty($this->jobs) ? array_shift($this->jobs) : FALSE;
if ($jobinfo == FALSE) {
break;
}
echo "========================================", "\n";
echo "| ", $jobinfo->getFunction(), ' => ', json_encode($jobinfo->getPayload()), "\n";
echo "========================================", "\n";
$worker = $this->workers[$jobinfo->getFunction()];
try {
$worker->run($jobinfo);
} catch (Throwable $ex) {
echo 'Uncaught Exception: ', $ex->getMessage(), "\n";
echo $ex->getTraceAsString(), "\n";
}
if ($jobinfo->hasError()) {
$resultMsg = 'ERROR';
$resultState = 'error';
echo 'EXCEPTION There was an error: ' . $jobinfo->getError(), "\n";
} else {
if ($jobinfo->hasResult()) {
echo 'RESULT ', $jobinfo->getResult(), "\n";
} else {
echo 'EXCEPTION There was no result.', "\n";
}
}
echo "========================================", "\n";
}
}
function stop() { }
};
$newjob = new JobInfo('', $argv[1], @json_decode(isset($argv[2]) ? $argv[2] : '', true));
$taskServer->runJob($newjob);
$__WorkerCommands = [];
foreach (getJobWorkers('*') as $func => $args) {
$__WorkerCommands[] = 'addFunction ' . $func;
}
$__WorkerCommands[] = 'setRedisHost ' . $config['redis'] . ' ' . (isset($config['redisPort']) ? $config['redisPort'] : '');
$__WorkerCommands[] = 'run';
require_once(__DIR__ . '/runWorker.php');