You are here

public function SitemapSubmitter::processItem in Simple XML sitemap 8.3

Same name and namespace in other branches
  1. 4.x modules/simple_sitemap_engines/src/Plugin/QueueWorker/SitemapSubmitter.php \Drupal\simple_sitemap_engines\Plugin\QueueWorker\SitemapSubmitter::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

modules/simple_sitemap_engines/src/Plugin/QueueWorker/SitemapSubmitter.php, line 131

Class

SitemapSubmitter
Process a queue of search engines to submit sitemaps.

Namespace

Drupal\simple_sitemap_engines\Plugin\QueueWorker

Code

public function processItem($engine_id) {

  /** @var \Drupal\simple_sitemap_engines\Entity\SearchEngine $engine */
  if ($engine = $this->engineStorage
    ->load($engine_id)) {
    $sitemap_urls = [];
    $manager = $this->generator
      ->getSitemapManager();
    foreach ($manager
      ->getSitemapTypes() as $type_name => $type_definition) {
      $sitemap_generator = $manager
        ->getSitemapGenerator($type_definition['sitemapGenerator']);

      // Submit all variants that are enabled for this search engine.
      foreach ($manager
        ->getSitemapVariants($type_name, FALSE) as $variant_id => $variant_definition) {
        if (in_array($variant_id, $engine->sitemap_variants) && FALSE !== $this->generator
          ->setVariants($variant_id)
          ->getSitemap()) {
          $sitemap_urls[$variant_definition['label']] = $sitemap_generator
            ->setSitemapVariant($variant_id)
            ->getSitemapUrl();
        }
      }
    }

    // Submit all URLs.
    foreach ($sitemap_urls as $variant => $sitemap_url) {
      $submit_url = str_replace('[sitemap]', $sitemap_url, $engine->url);
      try {
        $this->httpClient
          ->request('GET', $submit_url);

        // Log if submission was successful.
        $this->logger
          ->m('Sitemap @variant submitted to @url', [
          '@variant' => $variant,
          '@url' => $submit_url,
        ])
          ->log();

        // Record last submission time. This is purely informational; the
        // variable that determines when the next submission should be run is
        // stored in the global state.
        $this->state
          ->set("simple_sitemap_engines.simple_sitemap_engine.{$engine_id}.last_submitted", $this->time
          ->getRequestTime());
      } catch (RequestException $e) {

        // Catch and log exceptions so this submission gets removed from the
        // queue whether or not it succeeded.
        // If the error was caused by network failure, it's fine to just wait
        // until next time the submission is queued to try again.
        // If the error was caused by a malformed URL, keeping the submission
        // in the queue to retry is pointless since it will always fail.
        watchdog_exception('simple_sitemap', $e);
      }
    }
  }
}