You are here

public function ExtractorQueue::processItem in Search API attachments 8

Same name and namespace in other branches
  1. 9.0.x src/Plugin/QueueWorker/ExtractorQueue.php \Drupal\search_api_attachments\Plugin\QueueWorker\ExtractorQueue::processItem()

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

src/Plugin/QueueWorker/ExtractorQueue.php, line 113

Class

ExtractorQueue
Processes Tasks for Search API Attachments.

Namespace

Drupal\search_api_attachments\Plugin\QueueWorker

Code

public function processItem($data) {
  $extractor_plugin = $this
    ->getExtractorPlugin();
  if (!isset($data->fid)) {
    return;
  }

  // Load file from queue item.
  $file = $this->entityTypeManager
    ->getStorage('file')
    ->load($data->fid);
  if ($file === NULL) {
    return;
  }
  try {
    $collection = 'search_api_attachments';
    $key = $collection . ':' . $file
      ->id();

    // Skip file if element is found in key_value collection.
    $extracted_data = $this->keyValue
      ->get($collection)
      ->get($key);
    if (empty($extracted_data)) {

      // Extract file and save it in key_value collection.
      $extracted_data = $extractor_plugin
        ->extract($file);
      $this->keyValue
        ->get($collection)
        ->set($key, $extracted_data);
    }
    $fallback_collection = $this->keyValue
      ->get(FilesExtractor::FALLBACK_QUEUE_KV);
    $fallback_collection
      ->delete($data->entity_type . ':' . $data->entity_id);
    $entity = $this->entityTypeManager
      ->getStorage($data->entity_type)
      ->load($data->entity_id);
    if (!$entity) {
      return;
    }
    $indexes = ContentEntity::getIndexesForEntity($entity);
    $item_ids = [];
    if (is_a($entity, TranslatableInterface::class)) {
      $translations = $entity
        ->getTranslationLanguages();
      foreach ($translations as $translation_id => $translation) {
        $item_ids[] = $entity
          ->id() . ':' . $translation_id;
      }
    }
    $datasource_id = 'entity:' . $data->entity_type;
    foreach ($indexes as $index) {
      $index
        ->trackItemsUpdated($datasource_id, $item_ids);
    }
    $this->moduleHandler
      ->invokeAll('search_api_attachments_content_extracted', [
      $file,
      $entity,
    ]);
  } catch (\Exception $exception) {
    if ($data->extract_attempts < 5) {
      $data->extract_attempts++;
      \Drupal::queue('search_api_attachments')
        ->createItem($data);
    }
    else {
      $message_params = [
        '@file_id' => $data->fid,
        '@entity_id' => $data->entity_id,
        '@entity_type' => $data->entity_type,
      ];
      $this->logger
        ->log(LogLevel::ERROR, 'Text extraction failed after 5 attempts @file_id for @entity_type @entity_id.', $message_params);
    }
  }
}