Newer
Older
Alex Barth
committed
<?php
// $Id$
/**
* @file
* FeedsScheduler class and related.
*/
/**
* Describe a scheduler.
*/
interface FeedsSchedulerInterface {
Alex Barth
committed
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
/**
* Run Drupal cron.
*/
public function cron();
/**
* Add a feed to the schedule.
*
* @param $importer_id
* Id of a FeedsImporter object.
* @param $callback
* The callback to invoke on importer. Either 'import' or 'expire'.
* @param $feed_nid
* Feed nid that identifies the source for this configuration.
*/
public function add($importer_id, $callback, $feed_nid = 0);
/**
* Remove a feed from the schedule.
*
* @param $feed_nid
* Feed nid that identifies the source for this configuration.
*/
public function remove($importer_id, $callback, $feed_nid = 0);
/**
* Work off a given feed identified by $feed_info.
*
* @param $feed_info
* Array where 'importer_id' key is the id of a FeedsImporter object,
* and 'feed_nid' is the feed node id that identifies the
* source of a FeedsSource object.
*/
public function work($feed_info);
}
/**
* Implementation of FeedsSchedulerInterface.
Alex Barth
committed
*
* This scheduler uses the last_scheduled_time paradigm: By storing the time
* when a particular feed was scheduled to be refreshed last rather than
* storing when a feed should be _refreshed_ next, we gain two advantages:
*
* 1) If a feed's import_period setting changes, it has immediate effects -
* without batch updating an existing schedule.
* 2) The time between refreshes will always be scheduled based on when it
* has been scheduled last. Less drift occurs.
*/
class FeedsScheduler implements FeedsSchedulerInterface {
Alex Barth
committed
// Only used for debugging.
protected $debugTime;
/**
* Create a single instance of FeedsScheduler.
*/
public static function instance() {
static $instance;
if (!isset($instance)) {
$class = variable_get('feeds_scheduler_class', 'FeedsScheduler');
$instance = new $class();
}
return $instance;
}
/**
* Protect constructor.
*/
protected function __construct() {}
/**
* Implementation of FeedsSchedulerInterface::cron().
Alex Barth
committed
*
* Refreshes scheduled feeds.
*
* If drupal_queue is present, only pushes refresh tasks to queue and
* returns. If drupal_queue is not available, works off tasks.
*
* @todo: Run cleanup task that
* 1) Picks up items that are scheduled and not worked off for more than
* e. g. 6 hours.
* 2) Logs these items with watchdog.
*/
public function cron() {
// Check and set scheduler semaphore, take time.
Alex Barth
committed
if (variable_get('feeds_scheduler_cron', FALSE)) {
watchdog('FeedsScheduler', 'Last cron process did not finish.', array(), WATCHDOG_ERROR);
}
variable_set('feeds_scheduler_cron', TRUE);
Alex Barth
committed
// Get feeds configuration, check whether drupal_queue is present and set
// parameters accordingly.
if ($importers = feeds_importer_load_all()) {
if ($use_queue = module_exists('drupal_queue')) {
drupal_queue_include();
$queue = drupal_queue_get(FEEDS_SCHEDULER_QUEUE);
$num = variable_get('feeds_schedule_queue_num', 200);
}
else {
$num = variable_get('feeds_schedule_num', 5);
Alex Barth
committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Iterate over feed configurations, pick $num feeds for each
// configuration, push to queue or refresh feeds.
foreach ($importers as $importer) {
foreach (array('import', 'expire') as $callback) {
// Check whether jobs are scheduled.
$period = $importer->getSchedulePeriod($callback);
if ($period != FEEDS_SCHEDULE_NEVER) {
// Refresh feeds that have a refresh time older than now minus
// refresh period.
$time = $this->time() - $period;
$result = db_query_range('SELECT feed_nid, id AS importer_id, callback, last_scheduled_time FROM {feeds_schedule} WHERE id = "%s" AND callback = "%s" AND scheduled = 0 AND (last_scheduled_time < %d OR last_scheduled_time = 0) ORDER BY last_scheduled_time ASC', $importer->id, $callback, $time, 0, $num);
while ($feed_info = db_fetch_array($result)) {
// If drupal_queue is present, add to queue, otherwise work off
// immediately.
if ($use_queue) {
if ($queue->createItem($feed_info)) {
$this->flag($feed_info['importer_id'], $feed_info['callback'], $feed_info['feed_nid']);
}
else {
watchdog('FeedsScheduler', 'Error adding item to queue.', WATCHDOG_ALERT);
}
}
else {
$this->flag($feed_info['importer_id'], $feed_info['callback'], $feed_info['feed_nid']);
$this->work($feed_info);
}
}
}
}
}
}
// Unflag and post a message that we're done.
variable_set('feeds_scheduler_cron', FALSE);
watchdog('FeedsScheduler', 'Finished processing schedule after !time.', array('!time' => format_interval(time() - $start)));
Alex Barth
committed
}
/**
* Implementation of FeedsSchedulerInterface::add().
Alex Barth
committed
*
* Add a feed to the scheduler.
*
* @todo: Create optional parameter $last_scheduled_time to pass in.
* Set this value if a feed is refreshed on creation.
* @todo: Create an abstract interface for items that can be added?
*/
public function add($importer_id, $callback, $feed_nid = 0) {
$save = array(
'id' => $importer_id,
'callback' => $callback,
'feed_nid' => $feed_nid,
'last_scheduled_time' => 0,
'scheduled' => 0, // Means NOT scheduled at the moment.
);
drupal_write_record('feeds_schedule', $save, array('id', 'callback', 'feed_nid'));
if (!db_affected_rows()) {
drupal_write_record('feeds_schedule', $save);
}
}
/**
* Implementation of FeedsSchedulerInterface::remove().
Alex Barth
committed
*/
public function remove($importer_id, $callback, $feed_nid = 0) {
db_query('DELETE FROM {feeds_schedule} WHERE id = "%s" AND callback = "%s" AND feed_nid = %d', $importer_id, $callback, $feed_nid);
}
/**
* Implementation of FeedsSchedulerInterface::work().
Alex Barth
committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
*
* Refresh a feed.
*
* Used as worker callback invoked from feeds_scheduler_refresh() or
* if drupal_queue is not enabled, directly from $this->cron().
*/
public function work($feed_info) {
$importer = feeds_importer($feed_info['importer_id']);
// Only refresh if feed is actually in DB or in default configuration.
if ($importer->export_type != FEEDS_EXPORT_NONE) {
// Remove scheduled flag, if we fail after this we'd like to try again
// next time around.
$this->unflag($feed_info['importer_id'], $feed_info['callback'], $feed_info['feed_nid']);
// There are 2 possible callbacks: expire or 'import'.
if ($feed_info['callback'] == 'expire') {
try {
$importer->expire();
}
catch (Exception $e) {
watchdog('FeedsScheduler', $e->getMessage(), array(), WATCHDOG_ERROR);
}
}
elseif ($feed_info['callback'] == 'import') {
// Import feed if source is available.
$source = feeds_source($importer->id, $feed_info['feed_nid']);
Alex Barth
committed
if ($source->export_type & FEEDS_EXPORT_NONE) {
watchdog('FeedsScheduler', 'Expected source information in database for '. $importer->id .'/'. $feed_info['feed_nid'] .'. Could not find any.', array(), WATCHDOG_ERROR);
return;
}
try {
$source->import();
Alex Barth
committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
}
catch (Exception $e) {
watchdog('FeedsScheduler', $e->getMessage(), array(), WATCHDOG_ERROR);
}
}
}
}
/**
* Set the internal time of FeedsScheduler.
* Use for debugging.
*
* @param $time
* UNIX time that the scheduler should use for comparing the schedule. Set
* this time to test the behavior of the scheduler in the future or past.
* If set to 0, FeedsScheduler will use the current time.
*/
public function debugSetTime($time) {
$this->debugTime = $time;
}
/**
* Returns the internal time that the scheduler is operating on.
*
* Usually returns FEEDS_REQUEST_TIME, unless a debug time has been set
* with debugSetTime();
*
* @return
* An integer that is a UNIX time.
*/
public function time() {
return empty($this->debugTime) ? FEEDS_REQUEST_TIME : $this->debugTime;
}
/**
* Helper function to flag a feed scheduled.
*
* This function sets the feed's scheduled bit to 1 and updates
* last_scheduled_time to $this->time().
*
* @param $id
* Id of the importer configuration.
* @param $callback
* Callback of the job.
* @param $feed_nid
* Identifier of the feed node.
*/
protected function flag($id, $callback, $feed_nid) {
$save = array(
'id' => $id,
'callback' => $callback,
'feed_nid' => $feed_nid,
'last_scheduled_time' => $this->time(),
'scheduled' => 1,
);
drupal_write_record('feeds_schedule', $save, array('id', 'callback', 'feed_nid'));
}
/**
* Helper function to flag a feed unscheduled.
*
* This function sets the feed's scheduled bit to 0 and thus makes
* it eligible for being added to the queue again.
*
* @param $id
* Id of the importer configuration.
* @param $callback
* Callback of the job.
* @param $feed_nid
* Identifier of the feed node.
*/
protected function unflag($id, $callback, $feed_nid) {
$save = array(
'id' => $id,
'callback' => $callback,
'feed_nid' => $feed_nid,
'scheduled' => 0,
);
drupal_write_record('feeds_schedule', $save, array('id', 'callback', 'feed_nid'));
}
}