1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* job scheduler module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Collects and returns scheduler info.
|
10
|
*
|
11
|
* @see hook_cron_job_scheduler_info()
|
12
|
*
|
13
|
* @param $name
|
14
|
* Name of the schedule
|
15
|
* @return array
|
16
|
* Information for the schedule if $name, all the information if not
|
17
|
*/
|
18
|
function job_scheduler_info($name = NULL) {
|
19
|
$info = &drupal_static(__FUNCTION__);
|
20
|
if (!$info) {
|
21
|
$info = module_invoke_all('cron_job_scheduler_info');
|
22
|
drupal_alter('cron_job_scheduler_info', $info);
|
23
|
}
|
24
|
if ($name) {
|
25
|
return isset($info[$name]) ? $info[$name] : NULL;
|
26
|
}
|
27
|
else {
|
28
|
return $info;
|
29
|
}
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Implements hook_cron().
|
34
|
*/
|
35
|
function job_scheduler_cron() {
|
36
|
// Reschedule all jobs if requested.
|
37
|
if (variable_get('job_scheduler_rebuild_all', FALSE)) {
|
38
|
foreach (job_scheduler_info() as $name => $info) {
|
39
|
job_scheduler_rebuild_scheduler($name, $info);
|
40
|
}
|
41
|
variable_set('job_scheduler_rebuild_all', FALSE);
|
42
|
return;
|
43
|
}
|
44
|
|
45
|
// Reschedule stuck periodic jobs after one hour.
|
46
|
db_update('job_schedule')
|
47
|
->fields(array(
|
48
|
'scheduled' => 0,
|
49
|
))
|
50
|
->condition('scheduled', REQUEST_TIME - 3600, '<')
|
51
|
->condition('periodic', 1)
|
52
|
->execute();
|
53
|
|
54
|
// Query and dispatch scheduled jobs.
|
55
|
// Process a maximum of 200 jobs in a maximum of 30 seconds.
|
56
|
$start = time();
|
57
|
$total =
|
58
|
$failed = 0;
|
59
|
$jobs = db_select('job_schedule', NULL, array('fetch' => PDO::FETCH_ASSOC))
|
60
|
->fields('job_schedule')
|
61
|
->condition('scheduled', 0)
|
62
|
->condition('next', REQUEST_TIME, '<=')
|
63
|
->orderBy('next', 'ASC')
|
64
|
->range(0, 200)
|
65
|
->execute();
|
66
|
foreach ($jobs as $job) {
|
67
|
try {
|
68
|
JobScheduler::get($job['name'])->dispatch($job);
|
69
|
}
|
70
|
catch (Exception $e) {
|
71
|
watchdog('job_scheduler', $e->getMessage(), array(), WATCHDOG_ERROR);
|
72
|
$failed++;
|
73
|
// Drop jobs that have caused exceptions
|
74
|
JobScheduler::get($job['name'])->remove($job);
|
75
|
}
|
76
|
$total++;
|
77
|
if (time() > ($start + 30)) {
|
78
|
break;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
// Leave a note on how much time we spent processing.
|
83
|
watchdog('job_scheduler', 'Finished processing scheduled jobs (!time s, !total total, !failed failed).', array('!time' => format_interval(time() - $start), '!total' => $total, '!failed' => $failed));
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Implements hook_modules_enabled().
|
88
|
*/
|
89
|
function job_scheduler_modules_enabled($modules) {
|
90
|
job_scheduler_rebuild_all();
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Implements hook_modules_disabled().
|
95
|
*/
|
96
|
function job_scheduler_modules_disabled($modules) {
|
97
|
job_scheduler_rebuild_all();
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Rebuild scheduled information after enable/disable modules
|
102
|
*
|
103
|
* @todo What should we do about missing ones when disabling their module?
|
104
|
*/
|
105
|
function job_scheduler_rebuild_all() {
|
106
|
variable_set('job_scheduler_rebuild_all', TRUE);
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Rebuild a single scheduler
|
111
|
*/
|
112
|
function job_scheduler_rebuild_scheduler($name, $info = NULL) {
|
113
|
$info = $info ? $info : job_scheduler_info($name);
|
114
|
if (!empty($info['jobs'])) {
|
115
|
$scheduler = JobScheduler::get($name);
|
116
|
foreach ($info['jobs'] as $job) {
|
117
|
if (!$scheduler->check($job)) {
|
118
|
$scheduler->set($job);
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Implements hook_cron_queue_info().
|
126
|
*
|
127
|
* Provide queue worker information for jobs declared in
|
128
|
* hook_cron_job_scheduler_info().
|
129
|
*/
|
130
|
function job_scheduler_cron_queue_info() {
|
131
|
$queue = array();
|
132
|
foreach (job_scheduler_info() as $name => $info) {
|
133
|
if (!empty($info['jobs']) && !empty($info['queue name'])) {
|
134
|
$queue[$info['queue name']] = array(
|
135
|
'worker callback' => 'job_scheduler_cron_queue_worker',
|
136
|
'time' => 60, // Some reasonable default as we don't know
|
137
|
);
|
138
|
}
|
139
|
}
|
140
|
return $queue;
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Execute job worker from queue
|
145
|
*
|
146
|
* Providing our own worker has the advantage that we can reschedule the job or take care of cleanup
|
147
|
* Note that as we run the execute() action, the job won't be queued again this time.
|
148
|
*/
|
149
|
function job_scheduler_cron_queue_worker($job) {
|
150
|
try {
|
151
|
JobScheduler::get($job['name'])->execute($job);
|
152
|
}
|
153
|
catch (Exception $e) {
|
154
|
watchdog('job_scheduler', $e->getMessage(), array(), WATCHDOG_ERROR);
|
155
|
// Drop jobs that have caused exceptions
|
156
|
JobScheduler::get($job['name'])->remove($job);
|
157
|
}
|
158
|
}
|