From 33beaddeabea19c753ae2347da4bc72c79c7535f Mon Sep 17 00:00:00 2001
From: Alex Barth <alex_b@53995.no-reply.drupal.org>
Date: Thu, 28 Jan 2010 21:00:45 +0000
Subject: [PATCH] #698356 alex_b: Refactor and clean up FeedsScheduler::work()
 to allow more   scheduled tasks than 'import' and 'expire'.

---
 CHANGELOG.txt               |  6 +++++
 includes/FeedsImporter.inc  | 48 ++++++++++++++++++++++++++++++-------
 includes/FeedsScheduler.inc | 44 +++++++---------------------------
 3 files changed, 53 insertions(+), 45 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 11f59bc2..b5123e8a 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -1,5 +1,11 @@
 // $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
 ----------------------------------
 
diff --git a/includes/FeedsImporter.inc b/includes/FeedsImporter.inc
index 3a2d8e5b..9e1696e9 100644
--- a/includes/FeedsImporter.inc
+++ b/includes/FeedsImporter.inc
@@ -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.
    */
diff --git a/includes/FeedsScheduler.inc b/includes/FeedsScheduler.inc
index a61a8ee8..5a3ee342 100644
--- a/includes/FeedsScheduler.inc
+++ b/includes/FeedsScheduler.inc
@@ -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);
     }
   }
 
-- 
GitLab