Cron.php 5.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

/**
 * @file
 * Contains \Drupal\Core\Cron.
 */

namespace Drupal\Core;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\KeyValueStore\StateInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Queue\QueueFactory;
14
use Drupal\Core\Session\AccountProxyInterface;
15
use Drupal\Core\Session\AnonymousUserSession;
16
use Drupal\Core\Session\SessionManagerInterface;
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

/**
 * The Drupal core Cron service.
 */
class Cron implements CronInterface {

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The lock service.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lock;

  /**
   * The queue service.
   *
   * @var \Drupal\Core\Queue\QueueFactory
   */
  protected $queueFactory;

  /**
   * The state service.
   *
   * @var \Drupal\Core\KeyValueStore\StateInterface
   */
  protected $state;

51
52
53
54
55
56
57
  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

58
59
60
61
62
63
64
  /**
   * The session manager.
   *
   * @var \Drupal\Core\Session\SessionManagerInterface
   */
  protected $sessionManager;

65
66
67
68
69
70
71
72
73
74
75
  /**
   * Constructs a cron object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock service.
   * @param \Drupal\Core\Queue\QueueFactory $queue_factory
   *   The queue service.
   * @param \Drupal\Core\KeyValueStore\StateInterface $state
   *   The state service.
76
77
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *    The current user.
78
79
   * @param \Drupal\Core\Session\SessionManagerInterface $session_manager
   *   The session manager.
80
   */
81
  public function __construct(ModuleHandlerInterface $module_handler, LockBackendInterface $lock, QueueFactory $queue_factory, StateInterface $state, AccountProxyInterface $current_user, SessionManagerInterface $session_manager) {
82
83
84
85
    $this->moduleHandler = $module_handler;
    $this->lock = $lock;
    $this->queueFactory = $queue_factory;
    $this->state = $state;
86
    $this->currentUser = $current_user;
87
    $this->sessionManager = $session_manager;
88
89
90
91
92
93
94
95
96
97
  }

  /**
   * {@inheritdoc}
   */
  public function run() {
    // Allow execution to continue even if the request gets cancelled.
    @ignore_user_abort(TRUE);

    // Prevent session information from being saved while cron is running.
98
99
    $original_session_saving = $this->sessionManager->isEnabled();
    $this->sessionManager->disable();
100
101
102

    // Force the current user to anonymous to ensure consistent permissions on
    // cron runs.
103
104
    $original_user = $this->currentUser->getAccount();
    $this->currentUser->setAccount(new AnonymousUserSession());
105
106
107
108
109
110
111
112
113
114
115
116
117
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
157
158
159
160
161
162
163
164
165
166
167
168
169

    // Try to allocate enough time to run all the hook_cron implementations.
    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)) {
      // Cron is still running normally.
      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);

      // Release cron lock.
      $this->lock->release('cron');

      // Return TRUE so other functions can check if it did run successfully
      $return = TRUE;
    }

    foreach ($queues as $queue_name => $info) {
      if (isset($info['cron'])) {
        $callback = $info['worker callback'];
        $end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
        $queue = $this->queueFactory->get($queue_name);
        while (time() < $end && ($item = $queue->claimItem())) {
          try {
            call_user_func_array($callback, array($item->data));
            $queue->deleteItem($item);
          }
          catch (\Exception $e) {
            // In case of exception log it and leave the item in the queue
            // to be processed again later.
            watchdog_exception('cron', $e);
          }
        }
      }
    }

    // Restore the user.
170
    $this->currentUser->setAccount($original_user);
171
172
173
    if ($original_session_saving) {
      $this->sessionManager->enable();
    }
174
175
176
177
178

    return $return;
  }

}