You are here

public function TaskManager::setTasksBatch in Search API 8

Sets a batch for executing all pending tasks.

Parameters

array $conditions: (optional) An array of conditions to be matched for the tasks, with property names mapped to the value (or values, for multiple possibilities) that the property should have.

Overrides TaskManagerInterface::setTasksBatch

File

src/Task/TaskManager.php, line 283

Class

TaskManager
Provides a service for managing pending tasks.

Namespace

Drupal\search_api\Task

Code

public function setTasksBatch(array $conditions = []) {
  $task_ids = $this
    ->getTasksQuery($conditions)
    ->range(0, 100)
    ->execute();
  if (!$task_ids) {
    return;
  }
  $batch_definition = [
    'operations' => [
      [
        [
          $this,
          'processBatch',
        ],
        [
          $task_ids,
          $conditions,
        ],
      ],
    ],
    'finished' => [
      $this,
      'finishBatch',
    ],
  ];

  // If called inside of Drush, we want to start the batch immediately.
  // However, we first need to determine whether there already is one running,
  // since we don't want to start a second one – our new batch will
  // automatically be appended to the currently running batch operation.
  $batch = batch_get();
  $run_drush_batch = function_exists('drush_backend_batch_process') && empty($batch['running']);

  // Schedule the batch.
  batch_set($batch_definition);

  // Now run the Drush batch, if applicable.
  if ($run_drush_batch) {
    $result = drush_backend_batch_process();

    // Drush performs batch processing in a separate PHP request. When the
    // last batch is processed the batch list is cleared, but this only takes
    // effect in the other request. Take the same action here to ensure that
    // we are not requeueing stale batches when there are multiple tasks being
    // handled in a single request.
    // (Drush 9.6 changed the structure of $result, so check for both variants
    // as long as we support earlier Drush versions, too.)
    if (!empty($result['context']['drush_batch_process_finished']) || !empty($result['drush_batch_process_finished'])) {
      $batch =& batch_get();
      $batch = NULL;
      unset($batch);
    }
  }
}