Unverified Commit 0d808920 authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #3006314 by Dave Reid: Use native Drush batch processing instead of...

Issue #3006314 by Dave Reid: Use native Drush batch processing instead of unprogressive running. Provides better command output.
parent 8467d723
......@@ -2,7 +2,6 @@
namespace Drupal\xmlsitemap\Commands;
use Drupal\Component\Utility\Timer;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
......@@ -60,16 +59,9 @@ class XmlSitemapCommands extends DrushCommands {
* @aliases xmlsitemap-regenerate
*/
public function regenerate() {
// Run the batch process.
Timer::start('xmlsitemap_regenerate');
xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
$vars = [
'@timer' => Timer::read('xmlsitemap_regenerate'),
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
];
$this->output()->writeln(dt('XML sitemap files regenerated in @timer ms. Peak memory usage: @memory-peak.', $vars));
Timer::stop('xmlsitemap_regenerate');
$batch = xmlsitemap_regenerate_batch();
batch_set($batch);
drush_backend_batch_process();
}
/**
......@@ -87,16 +79,9 @@ class XmlSitemapCommands extends DrushCommands {
throw new \Exception("No link types are rebuildable.");
}
// Run the batch process.
Timer::start('xmlsitemap_rebuild');
xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $rebuild_types, TRUE);
$vars = [
'@timer' => Timer::read('xmlsitemap_rebuild'),
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
];
$this->output()->writeln(dt('XML sitemap files rebuilt in @timer ms. Peak memory usage: @memory-peak.', $vars));
Timer::stop('xmlsitemap_rebuild');
$batch = xmlsitemap_rebuild_batch($rebuild_types, TRUE);
batch_set($batch);
drush_backend_batch_process();
}
/**
......
......@@ -359,7 +359,7 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
/**
* {@inheritdoc}
*/
public function regenerateBatchGenerate($smid, array &$context) {
public function regenerateBatchGenerate($smid, &$context) {
if (!isset($context['sandbox']['sitemap'])) {
$context['sandbox']['sitemap'] = $this->entityTypeManager->getStorage('xmlsitemap')->load($smid);
$context['sandbox']['sitemap']->setChunks(1);
......@@ -382,11 +382,11 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
$this->logger->error($e);
}
$context['message'] = t('Generated %sitemap-url.', [
'%sitemap-url' => Url::fromRoute('xmlsitemap.sitemap_xml', [], $sitemap->uri['options'] + ['query' => ['page' => $sitemap->getChunks()]])->toString(),
]);
if (!empty($links)) {
$context['message'] = t('Generated %sitemap-url with @count links.', [
'%sitemap-url' => Url::fromRoute('xmlsitemap.sitemap_xml', [], $sitemap->uri['options'] + ['query' => ['page' => $sitemap->getChunks()]])->toString(),
'@count' => $links,
]);
$sitemap->setLinks($sitemap->getLinks() + $links);
$sitemap->setChunks($sitemap->getChunks() + 1);
}
......@@ -416,9 +416,8 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
/**
* {@inheritdoc}
*/
public function regenerateBatchGenerateIndex($smid, array &$context) {
/** @var \Drupal\xmlsitemap\XmlSitemapInterface $sitemap */
$sitemap = $this->entityTypeManager->getStorage('xmlsitemap')->load($smid);
public function regenerateBatchGenerateIndex($smid, &$context) {
$sitemap = xmlsitemap_sitemap_load($smid);
if ($sitemap != NULL && $sitemap->getChunks() > 1) {
try {
$this->generateIndex($sitemap);
......@@ -427,7 +426,7 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
// @todo Should this use watchdog_exception()?
$this->logger->error($e);
}
$context['message'] = t('Now generating sitemap index %sitemap-url.', [
$context['message'] = t('Generated sitemap index %sitemap-url.', [
'%sitemap-url' => Url::fromRoute('xmlsitemap.sitemap_xml', [], $sitemap->uri['options'])->toString(),
]);
}
......@@ -470,7 +469,7 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
$query->execute();
}
$context['message'] = t('Purging links.');
$context['message'] = t('Links cleared');
}
/**
......@@ -523,7 +522,7 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
$info['xmlsitemap']['process callback']($entity_type_id, $result);
$context['sandbox']['last_id'] = end($result);
$context['sandbox']['progress'] += count($result);
$context['message'] = t('Now processing %entity_type_id @last_id (@progress of @count).', [
$context['message'] = t('Processed %entity_type_id @last_id (@progress of @count).', [
'%entity_type_id' => $entity_type_id,
'@last_id' => $context['sandbox']['last_id'],
'@progress' => $context['sandbox']['progress'],
......
......@@ -95,20 +95,20 @@ interface XmlSitemapGeneratorInterface {
*
* @param string $smid
* Sitemap id.
* @param array $context
* @param array|\ArrayAccess $context
* Sitemap context.
*/
public function regenerateBatchGenerate($smid, array &$context);
public function regenerateBatchGenerate($smid, &$context);
/**
* Batch callback; generate the index page of a sitemap.
*
* @param string $smid
* Sitemap id.
* @param array $context
* @param array|\ArrayAccess $context
* Sitemap context.
*/
public function regenerateBatchGenerateIndex($smid, array &$context);
public function regenerateBatchGenerateIndex($smid, &$context);
/**
* Batch callback; sitemap regeneration finished.
......@@ -132,7 +132,7 @@ interface XmlSitemapGeneratorInterface {
* Entity types to rebuild.
* @param bool $save_custom
* Save custom data.
* @param array $context
* @param array|\ArrayAccess $context
* Context to be rebuilt.
*/
public function rebuildBatchClear(array $entity_type_ids, $save_custom, &$context);
......
......@@ -7,8 +7,6 @@
* @ingroup xmlsitemap
*/
use Drupal\Component\Utility\Timer;
/**
* Implements hook_drush_command().
*/
......@@ -38,16 +36,9 @@ function xmlsitemap_drush_command() {
* Regenerate the sitemap files from existing data.
*/
function drush_xmlsitemap_regenerate() {
// Run the batch process.
Timer::start('xmlsitemap_regenerate');
xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
$vars = [
'@timer' => Timer::read('xmlsitemap_regenerate'),
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
];
drush_print(dt('XML sitemap files regenerated in @timer ms. Peak memory usage: @memory-peak.', $vars));
Timer::stop('xmlsitemap_regenerate');
$batch = xmlsitemap_regenerate_batch();
batch_set($batch);
drush_backend_batch_process();
}
/**
......@@ -60,16 +51,9 @@ function drush_xmlsitemap_rebuild() {
return drush_set_error("No link types are rebuildable.");
}
// Run the batch process.
Timer::start('xmlsitemap_rebuild');
xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $rebuild_types, TRUE);
$vars = [
'@timer' => Timer::read('xmlsitemap_rebuild'),
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
];
drush_print(dt('XML sitemap files rebuilt in @timer ms. Peak memory usage: @memory-peak.', $vars));
Timer::stop('xmlsitemap_rebuild');
$batch = xmlsitemap_rebuild_batch($rebuild_types, TRUE);
batch_set($batch);
drush_backend_batch_process();
}
/**
......
......@@ -2474,10 +2474,10 @@ function xmlsitemap_regenerate_batch(array $smids = []) {
*
* @param string $smid
* Sitemap entity id.
* @param array $context
* @param array|\ArrayAccess $context
* Sitemap context.
*/
function xmlsitemap_regenerate_batch_generate($smid, array &$context = []) {
function xmlsitemap_regenerate_batch_generate($smid, &$context = []) {
\Drupal::service('xmlsitemap_generator')->regenerateBatchGenerate($smid, $context);
}
......@@ -2486,10 +2486,10 @@ function xmlsitemap_regenerate_batch_generate($smid, array &$context = []) {
*
* @param string $smid
* Sitemap entity id.
* @param array $context
* @param array|\ArrayAccess $context
* Sitemap context.
*/
function xmlsitemap_regenerate_batch_generate_index($smid, array &$context = []) {
function xmlsitemap_regenerate_batch_generate_index($smid, &$context = []) {
\Drupal::service('xmlsitemap_generator')->regenerateBatchGenerateIndex($smid, $context);
}
......@@ -2507,7 +2507,7 @@ function xmlsitemap_regenerate_batch_generate_index($smid, array &$context = [])
*
* @codingStandardsIgnoreStart
*/
function xmlsitemap_regenerate_batch_finished($success, $results, $operations, $elapsed) {
function xmlsitemap_regenerate_batch_finished($success, array $results, array $operations, $elapsed) {
// @codingStandardsIgnoreEnd
\Drupal::service('xmlsitemap_generator')->regenerateBatchFinished($success, $results, $operations, $elapsed);
}
......@@ -2572,10 +2572,10 @@ function xmlsitemap_batch_variable_set(array $variables) {
* Entity types to rebuild.
* @param bool $save_custom
* Save custom data.
* @param array $context
* @param array|\ArrayAccess $context
* Context to be rebuilt.
*/
function xmlsitemap_rebuild_batch_clear(array $entity_type_ids, $save_custom, array &$context = []) {
function xmlsitemap_rebuild_batch_clear(array $entity_type_ids, $save_custom, &$context = []) {
\Drupal::service('xmlsitemap_generator')->rebuildBatchClear($entity_type_ids, $save_custom, $context);
}
......@@ -2584,10 +2584,10 @@ function xmlsitemap_rebuild_batch_clear(array $entity_type_ids, $save_custom, ar
*
* @param string $entity_type_id
* Entity type ID.
* @param array $context
* @param array|\ArrayAccess $context
* Sitemap context.
*/
function xmlsitemap_rebuild_batch_fetch($entity_type_id, array &$context) {
function xmlsitemap_rebuild_batch_fetch($entity_type_id, &$context) {
\Drupal::service('xmlsitemap_generator')->rebuildBatchFetch($entity_type_id, $context);
}
......
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