Esempio n. 1
0
 /**
  * Processes cron queues.
  */
 protected function processQueues()
 {
     // Grab the defined cron queues.
     foreach ($this->queueManager->getDefinitions() 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();
             $queue_worker = $this->queueManager->createInstance($queue_name);
             $end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
             $queue = $this->queueFactory->get($queue_name);
             $lease_time = isset($info['cron']['time']) ?: NULL;
             while (time() < $end && ($item = $queue->claimItem($lease_time))) {
                 try {
                     $queue_worker->processItem($item->data);
                     $queue->deleteItem($item);
                 } catch (RequeueException $e) {
                     // The worker requested the task be immediately requeued.
                     $queue->releaseItem($item);
                 } catch (SuspendQueueException $e) {
                     // If the worker indicates there is a problem with the whole queue,
                     // release the item and skip to the next queue.
                     $queue->releaseItem($item);
                     watchdog_exception('cron', $e);
                     // Skip to the next queue.
                     continue 2;
                 } catch (\Exception $e) {
                     // In case of any other kind of exception, log it and leave the item
                     // in the queue to be processed again later.
                     watchdog_exception('cron', $e);
                 }
             }
         }
     }
 }
Esempio n. 2
0
 /**
  * @param $worker
  *
  * @return array
  */
 private function runQueue($worker)
 {
     $result['count'] = 0;
     $result['total'] = $this->queue->numberOfItems();
     while ($item = $this->queue->claimItem()) {
         try {
             $worker->processItem($item->data);
             $this->queue->deleteItem($item);
             $result['count']++;
         } catch (SuspendQueueException $e) {
             $this->queue->releaseItem($item);
             $result['error'] = $e;
         }
     }
     return $result;
 }
Esempio n. 3
0
 /**
  * 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);
             while (time() < $end && ($item = $queue->claimItem())) {
                 try {
                     call_user_func_array($callback, array($item->data));
                     $queue->deleteItem($item);
                 } catch (SuspendQueueException $e) {
                     // If the worker indicates there is a problem with the whole queue,
                     // release the item and skip to the next queue.
                     $queue->releaseItem($item);
                     watchdog_exception('cron', $e);
                     // Skip to the next queue.
                     continue 2;
                 } catch (\Exception $e) {
                     // In case of any other kind of exception, log it and leave the item
                     // in the queue to be processed again later.
                     watchdog_exception('cron', $e);
                 }
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     /** @var QueueInterface $queue */
     $queue = $this->queueFactory->get('manual_node_publisher');
     /** @var QueueWorkerInterface $queue_worker */
     $queue_worker = $this->queueManager->createInstance('manual_node_publisher');
     while ($item = $queue->claimItem()) {
         try {
             $queue_worker->processItem($item->data);
             $queue->deleteItem($item);
         } catch (SuspendQueueException $e) {
             // If the worker indicates there is a problem with the whole queue,
             // release the item and skip to the next queue.
             $queue->releaseItem($item);
             break;
         } catch (\Exception $e) {
             // In case of any other kind of exception, log it and leave the item
             // in the queue to be processed again later.
             watchdog_exception('npq', $e);
         }
     }
 }
 /**
  * Add the items to the queue when signaled by the form.
  */
 public function addItems(array &$form, FormStateInterface &$form_state)
 {
     $values = $form_state->getValues();
     $queue_name = $form['cron_queue_setup']['queue'][$values['queue']]['#title'];
     $num_items = $form_state->getValue('num_items');
     // Queues are defined by a QueueWorker Plugin which are selected by their
     // id attritbute.
     // @see \Drupal\cron_example\Plugin\QueueWorker\ReportWorkerOne
     $queue = $this->queue->get($values['queue']);
     for ($i = 1; $i <= $num_items; $i++) {
         // Create a new item, a new data object, which is passed to the
         // QueueWorker's processItem() method.
         $item = new \stdClass();
         $item->created = REQUEST_TIME;
         $item->sequence = $i;
         $queue->createItem($item);
     }
     $args = ['%num' => $num_items, '%queue' => $queue_name];
     drupal_set_message($this->t('Added %num items to %queue', $args));
 }
Esempio n. 6
0
 /**
  * Constructs a UpdateProcessor.
  *
  * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  *   The config factory.
  * @param \Drupal\Core\Queue\QueueFactory $queue_factory
  *   The queue factory
  * @param \Drupal\update\UpdateFetcherInterface $update_fetcher
  *   The update fetcher service
  * @param \Drupal\Core\State\StateInterface $state_store
  *   The state service.
  * @param \Drupal\Core\PrivateKey $private_key
  *   The private key factory service.
  * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
  *   The key/value factory.
  * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_expirable_factory
  *   The expirable key/value factory.
  */
 public function __construct(ConfigFactoryInterface $config_factory, QueueFactory $queue_factory, UpdateFetcherInterface $update_fetcher, StateInterface $state_store, PrivateKey $private_key, KeyValueFactoryInterface $key_value_factory, KeyValueFactoryInterface $key_value_expirable_factory)
 {
     $this->updateFetcher = $update_fetcher;
     $this->updateSettings = $config_factory->get('update.settings');
     $this->fetchQueue = $queue_factory->get('update_fetch_tasks');
     $this->tempStore = $key_value_expirable_factory->get('update');
     $this->fetchTaskStore = $key_value_factory->get('update_fetch_task');
     $this->availableReleasesTempStore = $key_value_expirable_factory->get('update_available_releases');
     $this->stateStore = $state_store;
     $this->privateKey = $private_key;
     $this->fetchTasks = array();
     $this->failed = array();
 }