-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobDispatcher.php
executable file
·82 lines (61 loc) · 2.36 KB
/
JobDispatcher.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
#!/usr/bin/env php
<?php
// This takes events from the event queue, and dispatches some of them as
// jobs.
use shanemcc\phpdb\DB;
require_once(dirname(__FILE__) . '/../functions.php');
echo showTime(), ' ', 'Job Dispatcher started.', "\n";
function createJob($job, $args) {
return JobQueue::get()->create($job, $args);
}
function dispatchJob($job) {
echo showTime(), ' ', 'Dispatching: ', $job->getName(), '(', json_encode($job->getJobData()), ')', "\n";
JobQueue::get()->publish($job);
}
foreach (recursiveFindFiles(__DIR__ . '/handlers') as $file) {
echo showTime(), ' ', 'Loading from: ', $file, "\n";
include_once($file);
}
EventQueue::get()->consumeEvents(function ($event) {
if (is_array($event) && isset($event['event'])) {
echo showTime(), ' ', 'Event: ', $event['event'], '(', json_encode($event['args']), ')', "\n";
// TODO: We probably want to check this less-often than every event.
checkDBAlive();
EventQueue::get()->handleSubscribers($event);
} else {
echo showTime(), ' ', 'Unknown Event: ', $event, "\n";
}
});
EventQueue::get()->subscribe('job.finished', function($jobid) {
$job = Job::load(DB::get(), $jobid);
$dependants = $job->getDependants();
echo showTime(), ' ', 'Job Finished: ', $jobid, "\n";
foreach ($dependants as $j) {
echo showTime(), ' ', "\t", 'Dependant: ', $j->getID(), "\n";
if (!in_array($j->getState(), ['created', 'blocked'])) {
echo showTime(), ' ', "\t\t", 'Job in invalid state for starting, ignoring.', "\n";
continue;
}
$canRun = true;
foreach ($j->getDependsOn() as $j2) {
echo showTime(), ' ', "\t\t", 'Depends on: ', $j2->getID(), ' which has state: ', $j2->getState(), "\n";
if ($j2->getState() == 'error') {
echo showTime(), ' ', "\t\t", 'Job unable to run due to error, marking as failed.', "\n";
$resultMsg = 'PARENT ERROR';
$j->setState('error')->setResult($resultMsg)->save();
EventQueue::get()->publish('job.finished', [$j->getID(), $resultMsg]);
$canRun = false;
}
if ($j2->getState() != 'finished') {
echo showTime(), ' ', "\t\t", 'Job unable to run due to incomplete parent.', "\n";
$canRun = false;
}
}
if ($canRun) {
echo showTime(), ' ', "\t\t", 'Job able to be run, dispatching.', "\n";
$j->setState('created')->save();
dispatchJob($j);
}
}
});
RabbitMQ::get()->consume();