Skip to content
Snippets Groups Projects
Commit 33beadde authored by Alex Barth's avatar Alex Barth
Browse files

#698356 alex_b: Refactor and clean up FeedsScheduler::work() to allow more

  scheduled tasks than 'import' and 'expire'.
parent ba2fe1ed
No related branches found
No related tags found
No related merge requests found
// $Id$
Feeds 6.x 1.0 xxxxx xx, xxxx-xx-xx
----------------------------------
- #698356 alex_b: Refactor and clean up FeedsScheduler::work() to allow more
scheduled tasks than 'import' and 'expire'.
Feeds 6.x 1.0 Alpha 10, 2010-01-25
----------------------------------
......
......@@ -72,22 +72,52 @@ class FeedsImporter extends FeedsConfigurable {
}
}
/**
* Callback for scheduler to invoke task. Do not execute if this importer is
* not persistent at all.
*
* @param $feed_info
* FeedsScheduler feed infor array.
*
* @see FeedsScheduler::work().
*/
public function work($feed_info) {
if ($this->export_type == FEEDS_EXPORT_NONE) {
return;
}
switch ($feed_info['callback']) {
case 'import':
feeds_source($feed_info['importer_id'], $feed_info['feed_nid'])->import();
break;
case 'expire':
$this->expire();
break;
}
}
/**
* Get the refresh period for import() or expire().
*/
public function getSchedulePeriod($callback) {
if ($callback == 'import') {
return $this->config['import_period'];
}
if ($callback == 'expire') {
// If a processor has expiry time set, run expiry every hour.
if (FEEDS_EXPIRE_NEVER != $this->processor->expiryTime()) {
return 3600;
}
return FEEDS_SCHEDULE_NEVER;
switch ($callback) {
case 'import':
return $this->config['import_period'];
case 'expire':
// If a processor has expiry time set, run expiry every hour.
if (FEEDS_EXPIRE_NEVER != $this->processor->expiryTime()) {
return 3600;
}
return FEEDS_SCHEDULE_NEVER;
}
}
/**
* Expose available schedule callbacks.
*/
public function getScheduleCallbacks() {
return array('import', 'expire');
}
/**
* Save configuration.
*/
......
......@@ -85,8 +85,6 @@ class FeedsScheduler implements FeedsSchedulerInterface {
*
* If drupal_queue is present, only pushes refresh tasks to queue and
* returns. If drupal_queue is not available, works off tasks.
*
* @todo: Make import/expire agnostic, define general job callback interface.
*/
public function cron() {
......@@ -113,7 +111,7 @@ class FeedsScheduler implements FeedsSchedulerInterface {
// 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) {
foreach ($importer->getScheduleCallbacks() as $callback) {
// Check whether jobs are scheduled.
$period = $importer->getSchedulePeriod($callback);
......@@ -188,42 +186,16 @@ class FeedsScheduler implements FeedsSchedulerInterface {
*
* Used as worker callback invoked from feeds_scheduler_refresh() or
* if drupal_queue is not enabled, directly from $this->cron().
*
* @todo: Make import/expire agnostic, define general job callback interface.
*/
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']);
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();
}
catch (Exception $e) {
watchdog('FeedsScheduler', $e->getMessage(), array(), WATCHDOG_ERROR);
}
}
// Remove scheduled flag, if we fail after this we'd like to try again asap.
$this->unflag($feed_info['importer_id'], $feed_info['callback'], $feed_info['feed_nid']);
try {
$importer->work($feed_info);
}
catch (Exception $e) {
watchdog('FeedsScheduler', $e->getMessage(), array(), WATCHDOG_ERROR);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment