You are here

public static function IndexTaskManager::processIndexTasks in Search API 8

Processes all pending index tasks inside a batch run.

Parameters

array|\ArrayAccess $context: The current batch context.

\Drupal\Core\Config\ConfigImporter $config_importer: The config importer.

File

src/Task/IndexTaskManager.php, line 70

Class

IndexTaskManager
Provides a service for managing pending index tasks.

Namespace

Drupal\search_api\Task

Code

public static function processIndexTasks(&$context, ConfigImporter $config_importer) {
  $index_task_manager = \Drupal::getContainer()
    ->get('search_api.index_task_manager');
  if (!isset($context['sandbox']['indexes'])) {
    $context['sandbox']['indexes'] = [];
    $indexes = \Drupal::entityTypeManager()
      ->getStorage('search_api_index')
      ->loadByProperties([
      'status' => TRUE,
    ]);
    $deleted = $config_importer
      ->getUnprocessedConfiguration('delete');

    /** @var \Drupal\search_api\IndexInterface $index */
    foreach ($indexes as $index_id => $index) {
      if (!$index_task_manager
        ->isTrackingComplete($index) && !in_array($index
        ->getConfigDependencyName(), $deleted)) {
        $context['sandbox']['indexes'][] = $index_id;
      }
    }
    $context['sandbox']['total'] = count($context['sandbox']['indexes']);
    if (!$context['sandbox']['total']) {
      $context['finished'] = 1;
      return;
    }
  }
  $index_id = array_shift($context['sandbox']['indexes']);
  $index = Index::load($index_id);
  try {
    if (!$index_task_manager
      ->addItemsOnce($index)) {
      array_unshift($context['sandbox']['indexes'], $index_id);
    }
  } catch (SearchApiException $e) {
    watchdog_exception('search_api', $e);
  }
  if (empty($context['sandbox']['indexes'])) {
    $context['finished'] = 1;
  }
  else {
    $finished = $context['sandbox']['total'] - count($context['sandbox']['indexes']);
    $context['finished'] = $finished / $context['sandbox']['total'];
    $args = [
      '%index' => $index
        ->label(),
      '@num' => $finished + 1,
      '@total' => $context['sandbox']['total'],
    ];
    $context['message'] = \Drupal::translation()
      ->translate('Tracking items for search index %index (@num of @total)', $args);
  }
}