From 463bf4269da942f1dd201436f7c25462e142f5bd Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Wed, 30 Apr 2014 12:02:36 +0100
Subject: [PATCH] Issue #2249899 by damiankloip: Refactor Drupal\Core\Cron.

---
 core/lib/Drupal/Core/Cron.php | 80 +++++++++++++++++++++--------------
 1 file changed, 49 insertions(+), 31 deletions(-)

diff --git a/core/lib/Drupal/Core/Cron.php b/core/lib/Drupal/Core/Cron.php
index c2e8d4f05e31..046216e9efa3 100644
--- a/core/lib/Drupal/Core/Cron.php
+++ b/core/lib/Drupal/Core/Cron.php
@@ -107,9 +107,6 @@ public function run() {
     drupal_set_time_limit(240);
 
     $return = FALSE;
-    // Grab the defined cron queues.
-    $queues = $this->moduleHandler->invokeAll('queue_info');
-    $this->moduleHandler->alter('queue_info', $queues);
 
     // Try to acquire cron lock.
     if (!$this->lock->acquire('cron', 240.0)) {
@@ -117,28 +114,8 @@ public function run() {
       watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
     }
     else {
-      // Make sure every queue exists. There is no harm in trying to recreate an
-      // existing queue.
-      foreach ($queues as $queue_name => $info) {
-        if (isset($info['cron'])) {
-          $this->queueFactory->get($queue_name)->createQueue();
-        }
-      }
-
-      // Iterate through the modules calling their cron handlers (if any):
-      foreach ($this->moduleHandler->getImplementations('cron') as $module) {
-        // Do not let an exception thrown by one module disturb another.
-        try {
-          $this->moduleHandler->invoke($module, 'cron');
-        }
-        catch (\Exception $e) {
-          watchdog_exception('cron', $e);
-        }
-      }
-
-      // Record cron time.
-      $this->state->set('system.cron_last', REQUEST_TIME);
-      watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE);
+      $this->invokeCronHandlers();
+      $this->setCronLastTime();
 
       // Release cron lock.
       $this->lock->release('cron');
@@ -147,8 +124,41 @@ public function run() {
       $return = TRUE;
     }
 
+    // Process cron queues.
+    $this->processQueues();
+
+    // Restore the user.
+    $this->currentUser->setAccount($original_user);
+    if ($original_session_saving) {
+      $this->sessionManager->enable();
+    }
+
+    return $return;
+  }
+
+  /**
+   * Records and logs the request time for this cron invocation.
+   */
+  protected function setCronLastTime() {
+    // Record cron time.
+    $this->state->set('system.cron_last', REQUEST_TIME);
+    watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE);
+  }
+
+  /**
+   * Processes cron queues.
+   */
+  protected function processQueues() {
+    // Grab the defined cron queues.
+    $queues = $this->moduleHandler->invokeAll('queue_info');
+    $this->moduleHandler->alter('queue_info', $queues);
+
     foreach ($queues as $queue_name => $info) {
       if (isset($info['cron'])) {
+        // Make sure every queue exists. There is no harm in trying to recreate
+        // an existing queue.
+        $this->queueFactory->get($queue_name)->createQueue();
+
         $callback = $info['worker callback'];
         $end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
         $queue = $this->queueFactory->get($queue_name);
@@ -165,14 +175,22 @@ public function run() {
         }
       }
     }
+  }
 
-    // Restore the user.
-    $this->currentUser->setAccount($original_user);
-    if ($original_session_saving) {
-      $this->sessionManager->enable();
+  /**
+   * Invokes any cron handlers implementing hook_cron.
+   */
+  protected function invokeCronHandlers() {
+    // Iterate through the modules calling their cron handlers (if any):
+    foreach ($this->moduleHandler->getImplementations('cron') as $module) {
+      // Do not let an exception thrown by one module disturb another.
+      try {
+        $this->moduleHandler->invoke($module, 'cron');
+      }
+      catch (\Exception $e) {
+        watchdog_exception('cron', $e);
+      }
     }
-
-    return $return;
   }
 
 }
-- 
GitLab