Commit 7a2028c7 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #3000718 by wells, slasher13: Respect cron_generate_interval only after full generation

parent bb433bee
......@@ -12,7 +12,7 @@ use Drupal\system\MenuInterface;
use Drupal\language\ConfigurableLanguageInterface;
/**
*Implements hook_help.
*Implements hook_help().
*
* @param $route_name
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
......@@ -24,7 +24,7 @@ function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
}
/**
* Implements hook_form_alter.
* Implements hook_form_alter().
*
* Adds sitemap settings to entity types that are supported via plugins.
*
......@@ -137,17 +137,27 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
}
/**
* Implements hook_cron.
* Implements hook_cron().
*/
function simple_sitemap_cron() {
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
if ($generator->getSetting('cron_generate')) {
$interval = (int) $generator->getSetting('cron_generate_interval', 0) * 60 * 60;
$requestTime = \Drupal::service('datetime.time')->getRequestTime();
if ($interval === 0 || ((\Drupal::state()->get('simple_sitemap.last_cron_generate', 0) + $interval) <= $requestTime)) {
\Drupal::state()->set('simple_sitemap.last_cron_generate', $requestTime);
$request_time = \Drupal::service('datetime.time')->getRequestTime();
$generation_in_progress = $generator->getQueueWorker()->generationInProgress();
$state = \Drupal::state();
if ($interval === 0
|| $generation_in_progress
|| (($state->get('simple_sitemap.last_cron_generate', 0) + $interval) <= $request_time)) {
if (!$generation_in_progress) {
$state->set('simple_sitemap.last_cron_generate', $request_time);
}
$generator->generateSitemap('cron');
}
}
......
......@@ -178,7 +178,7 @@ class QueueWorker {
if (!empty($data_sets)) {
$this->queueElements($data_sets);
}
$this->getRemainingElementCount(TRUE);
$this->getQueuedElementCount(TRUE);
return $this;
}
......@@ -207,12 +207,11 @@ class QueueWorker {
];
$this->maxLinks = $this->settings->getSetting('max_links');
$max_execution_time = $this->settings->getSetting('generate_duration', 10000);
$elements_processed = 0;
Timer::start('simple_sitemap_generator');
$this->unstashResults();
if ($this->getRemainingElementCount() === 0 && empty($this->results)) {
if (!$this->generationInProgress()) {
$this->rebuildQueue($variants);
}
......@@ -245,11 +244,10 @@ class QueueWorker {
}
$this->queue->deleteItem($element); //todo May want to use deleteItems() instead.
$elements_processed++;
$this->elementsRemaining--;
}
if ($this->getRemainingElementCount() === 0) {
if ($this->getQueuedElementCount() === 0) {
$this->generateVariantChunksFromResults(TRUE);
$this->publishCurrentVariant();
}
......@@ -331,7 +329,7 @@ class QueueWorker {
return $this->elementsTotal;
}
public function getRemainingElementCount($force_recount = FALSE) {
public function getQueuedElementCount($force_recount = FALSE) {
if ($force_recount || NULL === $this->elementsRemaining) {
$this->elementsRemaining = $this->queue->numberOfItems();
}
......@@ -339,11 +337,22 @@ class QueueWorker {
return $this->elementsRemaining;
}
public function getStashedResultCount() {
return ($this->state->has('simple_sitemap.queue_stashed_results')
&& !empty($results = $this->state->get('simple_sitemap.queue_stashed_results')['results']))
? count($results)
: 0;
}
public function getProcessedElementCount() {
$original = $this->getInitialElementCount();
$remaining = $this->getRemainingElementCount();
$remaining = $this->getQueuedElementCount();
return $remaining <= $original ? ($original - $remaining) : 0;
}
public function generationInProgress() {
return 0 < ($this->getQueuedElementCount() + $this->getStashedResultCount());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment