-
Notifications
You must be signed in to change notification settings - Fork 6
/
dispatch.hosting.inc
75 lines (70 loc) · 2.87 KB
/
dispatch.hosting.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* @file
* Drush include for the Hosting module's dispatch command.
*/
/**
* Main queue processing drush command for hostmaster.
*
* This is a single command, which will (based on configuration) run all the
* other queue commands (cron, backup, tasks, stats). This is done so that there
* is only one cron job to configure, and allow the frequency of calls to be
* configured from the frontend interface.
*/
function drush_hosting_dispatch() {
$now = REQUEST_TIME;
variable_set("hosting_dispatch_last_run", $now);
drush_log(dt("Dispatching queues."));
$platform = node_load(HOSTING_OWN_PLATFORM);
$root = $platform->publish_path;
if (variable_get('hosting_dispatch_enabled', FALSE)) {
$queues = hosting_get_queues();
foreach ($queues as $queue => $info) {
$semaphore = "hosting_dispatch_{$queue}_running";
$lock_wait = drush_get_option('lock-wait', HOSTING_QUEUE_DEFAULT_LOCK_WAIT);
if (!lock_wait($semaphore, $lock_wait) || drush_get_option('force', FALSE)) {
if (lock_acquire($semaphore, HOSTING_QUEUE_LOCK_TIMEOUT)) {
drush_log(dt('Acquired lock on @queue queue.', array('@queue' => $queue)));
}
elseif (drush_get_option('force', FALSE)) {
drush_log(dt('Bypassing lock on @queue queue.', array('@queue' => $queue)), 'warning');
}
else {
drush_die(dt('Cannot acquire lock on @queue queue.', array('@queue' => $queue)));
}
}
else {
drush_die(dt("Cannot acquire lock on @queue queue after waiting @wait seconds. A longer wait time can be set with the --lock-wait option.", array('@queue' => $queue, '@wait' => $lock_wait)));
}
if ($info['enabled']) {
if (($now - $info["last"]) >= $info["calc_frequency"] || drush_get_option('force', FALSE)) {
$count = $info['calc_items'] - $info['running_items'];
if ($count <= 0) {
drush_log(dt("Maximum number of tasks (@count) already running.", array('@count' => $info['running_items'])));
}
else {
drush_log(dt("Found @running running tasks, starting @count out of @max items.",
array(
'@running' => $info['running_items'],
'@count' => $count,
'@max' => $info['calc_items'])
)
);
drush_invoke_process('@self', "hosting-" . $queue, array(), array('items' => $count, 'strict' => FALSE), array('fork' => TRUE));
}
}
else {
drush_log(dt("Too early for queue @queue.", array('@queue' => $queue)));
}
}
else {
drush_log(dt("Queue @queue disabled.", array('@queue' => $queue)));
}
drush_log(dt('Releasing @queue lock.', array('@queue' => $queue)));
lock_release($semaphore);
}
}
else {
drush_log(dt("Dispatching disabled."));
}
}