-
Notifications
You must be signed in to change notification settings - Fork 8
/
ESDaemon.inc
55 lines (48 loc) · 1.67 KB
/
ESDaemon.inc
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
<?php
class ESDaemon extends DrushDaemon {
protected $loop_interval = 20;
/**
* Find and run queues.
*
* @param int $iteration_number
*/
protected function executeTask($iteration_number) {
// Get and run the dispatcher if any items are in it
$dispatcher = db_query("SELECT COUNT(item_id) FROM queue WHERE name = 'elasticsearch_dispatcher'")->fetchField();
if ($dispatcher > 0) {
$this->runQueue('elasticsearch_dispatcher');
}
// Check to see if there are tasks remaining
$queues = db_query("SELECT name, COUNT(item_id) FROM queue WHERE name LIKE 'elasticsearch_queue%' GROUP BY name ORDER BY name ASC")->fetchAll();
// Execute all queues
foreach ($queues as $queue) {
// Run the queue
$this->runQueue($queue->name);
}
}
/**
* Run a queue in its own thread.
*
* @param string $queue_name
*/
protected function runQueue($queue_name) {
$name = 'queue_' . $queue_name;
$thread = intval(substr($name, -1));
if (!($thread > 0)) {
$thread = 1;
}
// We use drush_invoke_process() to fork the daemon safely to run
// multiple jobs concurrently. We can't use the PHP-daemon built
// in functionality such as workers & tasks b/c they copy the
// entire environment. resulting in multiple processes using the same
// database connection (which causes errors).
drush_invoke_process('@self', // Obviously run on the current site.
'cron-run', // Run the cron
[$name], // Arguments
[
'options' => 'thread=' . $thread,
],
['fork' => TRUE] // This tells drush to spawn a new process.
);
}
}