You are here

public function TaskManager::executeAllTasks in Search API 8

Executes all (or some) 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.

int|null $limit: (optional) If given, only this number of tasks will be executed.

Return value

bool TRUE if all tasks matching the conditions have been executed, FALSE if $limit was given and lower than the total count of pending tasks matching the conditions.

Throws

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

Overrides TaskManagerInterface::executeAllTasks

File

src/Task/TaskManager.php, line 242

Class

TaskManager
Provides a service for managing pending tasks.

Namespace

Drupal\search_api\Task

Code

public function executeAllTasks(array $conditions = [], $limit = NULL) {

  // We have to use this roundabout way because tasks, during their execution,
  // might create additional tasks. (For example, see
  // \Drupal\search_api\Task\IndexTaskManager::trackItems().)
  $executed = 0;
  while (TRUE) {
    $query = $this
      ->getTasksQuery($conditions);
    if (isset($limit)) {
      $query
        ->range(0, $limit - $executed);
    }
    $task_ids = $query
      ->execute();
    if (!$task_ids) {
      break;
    }

    // We can't use multi-load here as a task might delete other tasks, so we
    // have to make sure each tasks still exists right before it is executed.
    foreach ($task_ids as $task_id) {

      /** @var \Drupal\search_api\Task\TaskInterface $task */
      $task = $this
        ->getTaskStorage()
        ->load($task_id);
      if ($task) {
        $this
          ->executeSpecificTask($task);
      }
      else {
        --$executed;
      }
    }
    $executed += count($task_ids);
    if (isset($limit) && $executed >= $limit) {
      break;
    }
  }
  return !$this
    ->getTasksCount($conditions);
}