You are here

public function QueueWorker::generateSitemap in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 src/Queue/QueueWorker.php \Drupal\simple_sitemap\Queue\QueueWorker::generateSitemap()

Parameters

string $from:

Return value

$this

Throws

\Drupal\Component\Plugin\Exception\PluginException

File

src/Queue/QueueWorker.php, line 205

Class

QueueWorker

Namespace

Drupal\simple_sitemap\Queue

Code

public function generateSitemap(string $from = self::GENERATE_TYPE_FORM) : QueueWorker {
  $this->generatorSettings = [
    'base_url' => $this->settings
      ->get('base_url', ''),
    'xsl' => $this->settings
      ->get('xsl', TRUE),
    'default_variant' => $this->settings
      ->get('default_variant', NULL),
    'skip_untranslated' => $this->settings
      ->get('skip_untranslated', FALSE),
    'remove_duplicates' => $this->settings
      ->get('remove_duplicates', TRUE),
    'excluded_languages' => $this->settings
      ->get('excluded_languages', []),
  ];
  $this->maxLinks = $this->settings
    ->get('max_links');
  $max_execution_time = $this->settings
    ->get('generate_duration', 10000);
  Timer::start('simple_sitemap_generator');
  $this
    ->unstashResults();
  if (!$this
    ->generationInProgress()) {
    $this
      ->rebuildQueue();
  }

  // Acquire a lock for max execution time + 5 seconds. If max_execution time
  // is unlimited then lock for 1 hour.
  $lock_timeout = $max_execution_time > 0 ? $max_execution_time / 1000 + 5 : static::GENERATE_LOCK_TIMEOUT;
  if (!$this->lock
    ->acquire(static::LOCK_ID, $lock_timeout)) {
    throw new \RuntimeException('Unable to acquire a lock for sitemap generation');
  }
  foreach ($this->queue
    ->yieldItem() as $element) {
    if (!empty($max_execution_time) && Timer::read('simple_sitemap_generator') >= $max_execution_time) {
      break;
    }
    try {
      if ($this->variantProcessedNow === NULL || $element->data['sitemap'] !== $this->variantProcessedNow
        ->id()) {
        if (NULL !== $this->variantProcessedNow) {
          $this
            ->generateVariantChunksFromResults(TRUE);
          $this
            ->publishCurrentVariant();
        }
        $this->variantProcessedNow = $this->entityTypeManager
          ->getStorage('simple_sitemap')
          ->load($element->data['sitemap']);
        $this->processedPaths = [];
      }
      $this
        ->generateResultsFromElement($element);
      if (!empty($this->maxLinks) && count($this->results) >= $this->maxLinks) {
        $this
          ->generateVariantChunksFromResults();
      }
    } catch (\Exception $e) {
      watchdog_exception('simple_sitemap', $e);
    }
    $this->queue
      ->deleteItem($element);

    //todo May want to use deleteItems() instead.
    $this->elementsRemaining--;
  }
  if ($this
    ->getQueuedElementCount() === 0) {
    $this
      ->generateVariantChunksFromResults(TRUE);
    $this
      ->publishCurrentVariant();
  }
  else {
    $this
      ->stashResults();
  }
  $this->lock
    ->release(static::LOCK_ID);
  return $this;
}