You are here

public function SitemapSubmitter::processItem in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 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 120

Class

SitemapSubmitter
Process a queue of search engines to submit sitemaps.

Namespace

Drupal\simple_sitemap_engines\Plugin\QueueWorker

Code

public function processItem($engine_id) {
  if ($engine = SimpleSitemapEngine::load($engine_id)) {

    // Submit all variants that are enabled for this search engine.
    foreach (SimpleSitemap::loadMultiple($engine->sitemap_variants) as $sitemap_id => $sitemap) {
      if ($sitemap
        ->status()) {
        $submit_url = str_replace('[sitemap]', $sitemap
          ->toUrl()
          ->toString(), $engine->url);
        try {
          $this->httpClient
            ->request('GET', $submit_url);

          // Log if submission was successful.
          $this->logger
            ->m('Sitemap @variant submitted to @url', [
            '@variant' => $sitemap_id,
            '@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);
        }
      }
    }
  }
}