You are here

public function PostRequestIndexing::destruct in Search API 8

Performs destruct operations.

Overrides DestructableInterface::destruct

File

src/Utility/PostRequestIndexing.php, line 56

Class

PostRequestIndexing
Provides a service for indexing items at the end of the page request.

Namespace

Drupal\search_api\Utility

Code

public function destruct() {
  foreach ($this->operations as $index_id => $item_ids) {
    try {
      $storage = $this->entityTypeManager
        ->getStorage('search_api_index');
    } catch (\Exception $e) {

      // It might be possible that the module got uninstalled during the rest
      // of the page request, or something else happened. To be on the safe
      // side, catch the exception in case the entity type isn't found.
      return;
    }

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $storage
      ->load($index_id);

    // It's possible that the index was deleted in the meantime, so make sure
    // it's actually there.
    if (!$index) {
      continue;
    }
    try {

      // In case there are lots of items to index, take care to not load/index
      // all of them at once, so we don't run out of memory. Using the index's
      // cron batch size should always be safe.
      $batch_size = $index
        ->getOption('cron_limit', 50) ?: 50;
      if ($batch_size > 0) {
        $item_ids_batches = array_chunk($item_ids, $batch_size);
      }
      else {
        $item_ids_batches = [
          $item_ids,
        ];
      }
      foreach ($item_ids_batches as $item_ids_batch) {
        $items = $index
          ->loadItemsMultiple($item_ids_batch);
        if ($items) {
          $index
            ->indexSpecificItems($items);
        }
      }
    } catch (SearchApiException $e) {
      $vars['%index'] = $index
        ->label();
      watchdog_exception('search_api', $e, '%type while trying to index items on %index: @message in %function (line %line of %file).', $vars);
    }

    // We usually shouldn't be called twice in a page request, but no harm in
    // being too careful: Remove the operation once it was executed correctly.
    unset($this->operations[$index_id]);
  }

  // Make sure that no new items were added while processing the previous
  // ones. Otherwise, call this method again to index those as well. (But also
  // guard against infinite recursion.)
  if ($this->operations && ++$this->recursion <= 5) {
    $this
      ->destruct();
  }
}