You are here

public function TaskManager::processBatch in Search API 8

Processes a single pending task as part of a batch operation.

Parameters

int[] $task_ids: An array of task IDs to execute. Might not contain all task IDs.

array $conditions: An array of conditions defining the tasks to be executed. Should be used to retrieve more task IDs if necessary.

array|\ArrayAccess $context: The context of the current batch, as defined in the Batch operations documentation.

Throws

\Drupal\search_api\SearchApiException Thrown if any error occurred while processing the task.

File

src/Task/TaskManager.php, line 342

Class

TaskManager
Provides a service for managing pending tasks.

Namespace

Drupal\search_api\Task

Code

public function processBatch(array $task_ids, array $conditions, &$context) {

  // Initialize context information.
  if (!isset($context['sandbox']['task_ids'])) {
    $context['sandbox']['task_ids'] = $task_ids;
  }
  if (!isset($context['results']['total'])) {
    $context['results']['total'] = $this
      ->getTasksCount($conditions);
  }
  $task_id = array_shift($context['sandbox']['task_ids']);

  /** @var \Drupal\search_api\Task\TaskInterface $task */
  $task = $this
    ->getTaskStorage()
    ->load($task_id);
  if ($task) {
    $this
      ->executeSpecificTask($task);
  }
  if (!$context['sandbox']['task_ids']) {
    $context['sandbox']['task_ids'] = $this
      ->getTasksQuery($conditions)
      ->range(0, 100)
      ->execute();
    if (!$context['sandbox']['task_ids']) {
      $context['finished'] = 1;
      return;
    }
  }
  $pending = $this
    ->getTasksCount($conditions);
  $context['finished'] = 1 - $pending / $context['results']['total'];
  $executed = $context['results']['total'] - $pending;
  if ($executed > 0) {
    $context['message'] = $this
      ->formatPlural($executed, 'Successfully executed @count pending task.', 'Successfully executed @count pending tasks.');
  }
}