1 |
85ad3d82
|
Assos Assos
|
|
2 |
|
|
Job Scheduler
|
3 |
|
|
=============
|
4 |
|
|
|
5 |
|
|
Simple API for scheduling tasks once at a predetermined time or periodically at
|
6 |
|
|
a fixed interval.
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
Usage
|
10 |
|
|
=====
|
11 |
|
|
|
12 |
|
|
Declare scheduler.
|
13 |
|
|
|
14 |
|
|
function example_cron_job_scheduler_info() {
|
15 |
|
|
$schedulers = array();
|
16 |
|
|
$schedulers['example_unpublish'] = array(
|
17 |
|
|
'worker callback' => 'example_unpublish_nodes',
|
18 |
|
|
);
|
19 |
|
|
return $schedulers;
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
Add a job.
|
23 |
|
|
|
24 |
|
|
$job = array(
|
25 |
|
|
'type' => 'story',
|
26 |
|
|
'id' => 12,
|
27 |
|
|
'period' => 3600,
|
28 |
|
|
'periodic' => TRUE,
|
29 |
|
|
);
|
30 |
|
|
JobScheduler::get('example_unpublish')->set($job);
|
31 |
|
|
|
32 |
|
|
Work off a job.
|
33 |
|
|
|
34 |
|
|
function example_unpublish_nodes($job) {
|
35 |
|
|
// Do stuff.
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
Remove a job.
|
39 |
|
|
|
40 |
|
|
$job = array(
|
41 |
|
|
'type' => 'story',
|
42 |
|
|
'id' => 12,
|
43 |
|
|
);
|
44 |
|
|
JobScheduler::get('example_unpublish')->remove($job);
|
45 |
|
|
|
46 |
|
|
Optionally jobs can declared together with a schedule in a hook_cron_job_scheduler_info().
|
47 |
|
|
|
48 |
|
|
function example_cron_job_scheduler_info() {
|
49 |
|
|
$schedulers = array();
|
50 |
|
|
$schedulers['example_unpublish'] = array(
|
51 |
|
|
'worker callback' => 'example_unpublish_nodes',
|
52 |
|
|
'jobs' => array(
|
53 |
|
|
array('type' => 'story', 'id' => 12, 'period' => 3600, 'periodic' => TRUE),
|
54 |
|
|
)
|
55 |
|
|
);
|
56 |
|
|
return $schedulers;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
Jobs can have a 'crontab' instead of a period. Crontab syntax are Unix-like formatted crontab lines.
|
60 |
|
|
Example of job with crontab.
|
61 |
|
|
|
62 |
|
|
// This will create a job that will be triggered from monday to friday, from january to july, every two hours
|
63 |
|
|
function example_cron_job_scheduler_info() {
|
64 |
|
|
$schedulers = array();
|
65 |
|
|
$schedulers['example_unpublish'] = array(
|
66 |
|
|
'worker callback' => 'example_unpublish_nodes',
|
67 |
|
|
'jobs' => array(
|
68 |
|
|
array('type' => 'story', 'id' => 12, 'crontab' => '0 */2 * january-july mon-fri', 'periodic' => TRUE),
|
69 |
|
|
)
|
70 |
|
|
);
|
71 |
|
|
return $schedulers;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
Read more about crontab syntax, http://linux.die.net/man/5/crontab
|
75 |
|
|
|
76 |
|
|
Drupal Queue integration
|
77 |
|
|
========================
|
78 |
|
|
|
79 |
|
|
Optionally, at the scheduled time Job Scheduler can queue a job for execution,
|
80 |
|
|
rather than executing the job directly. This is useful when many jobs need to
|
81 |
|
|
be executed or when the job's expected execution time is very long.
|
82 |
|
|
|
83 |
|
|
More information on Drupal Queue: http://api.drupal.org/api/group/queue/7
|
84 |
|
|
|
85 |
|
|
Instead of declaring a worker callback, declare a queue name.
|
86 |
|
|
|
87 |
|
|
function example_cron_job_scheduler_info() {
|
88 |
|
|
$schedulers = array();
|
89 |
|
|
$schedulers['example_unpublish'] = array(
|
90 |
|
|
'queue name' => 'example_unpublish_queue',
|
91 |
|
|
);
|
92 |
|
|
return $schedulers;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
This of course assumes that you have declared a queue. Notice how in this
|
96 |
|
|
pattern the queue callback contains the actual worker callback.
|
97 |
|
|
|
98 |
|
|
function example_cron_queue_info() {
|
99 |
|
|
$schedulers = array();
|
100 |
|
|
$schedulers['example_unpublish_queue'] = array(
|
101 |
|
|
'worker callback' => 'example_unpublish_nodes',
|
102 |
|
|
);
|
103 |
|
|
return $schedulers;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
Work off a job: when using a queue, Job Scheduler reserves a job for one hour
|
108 |
|
|
giving the queue time to work off a job before it reschedules it. This means
|
109 |
|
|
that the worker callback needs to reset the job's schedule flag in order to
|
110 |
|
|
allow renewed scheduling.
|
111 |
|
|
|
112 |
|
|
function example_unpublish_nodes($job) {
|
113 |
|
|
// Do stuff.
|
114 |
|
|
// Set the job again so that its reserved flag is reset.
|
115 |
|
|
JobScheduler::get('example_unpublish')->set($job);
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
Example
|
119 |
|
|
=======
|
120 |
|
|
|
121 |
|
|
See Feeds module.
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
Hidden settings
|
125 |
|
|
===============
|
126 |
|
|
|
127 |
|
|
Hidden settings are variables that you can define by adding them to the $conf
|
128 |
|
|
array in your settings.php file.
|
129 |
|
|
|
130 |
|
|
Name: 'job_scheduler_class_' . $name
|
131 |
|
|
Default: 'JobScheduler'
|
132 |
|
|
Description: The class to use for managing a particular schedule. |