You are here

protected function SmartlingTestBase::processQueue in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/Functional/SmartlingTestBase.php \Drupal\Tests\tmgmt_smartling\Functional\SmartlingTestBase::processQueue()

Processes cron queue.

Parameters

$name:

4 calls to SmartlingTestBase::processQueue()
JobsTest::testAddToJobQueueAsync in tests/src/Functional/JobsTest.php
Add to job form, async mode: two jobs (queue mode).
JobsTest::testAddToJobSingleAsync in tests/src/Functional/JobsTest.php
Add to job form, async mode: single job.
JobsTest::testCreateJobQueueAsync in tests/src/Functional/JobsTest.php
Create job form, async mode: two jobs (queue mode).
JobsTest::testCreateJobSingleAsync in tests/src/Functional/JobsTest.php
Create job form, async mode: single job.

File

tests/src/Functional/SmartlingTestBase.php, line 297

Class

SmartlingTestBase
Basic tests for the Smartling translator.

Namespace

Drupal\Tests\tmgmt_smartling\Functional

Code

protected function processQueue($name) {
  $queue_factory = Drupal::service('queue');
  $queue_manager = Drupal::service('plugin.manager.queue_worker');

  // Grab the defined cron queues.
  foreach ($queue_manager
    ->getDefinitions() as $queue_name => $info) {
    if ($queue_name != $name) {
      continue;
    }
    if (isset($info['cron'])) {

      // Make sure every queue exists. There is no harm in trying to recreate
      // an existing queue.
      $queue_factory
        ->get($queue_name)
        ->createQueue();
      $queue_worker = $queue_manager
        ->createInstance($queue_name);
      $end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15);
      $queue = $queue_factory
        ->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);
        }
      }
    }
  }
}