Skip to content

Commit

Permalink
Merge pull request #529 from City-of-Helsinki/SATT-34-Batch_process
Browse files Browse the repository at this point in the history
SATT-34: Batch process
  • Loading branch information
OscarJonasson authored Mar 18, 2024
2 parents 660741b + aad19a7 commit 4cd45c4
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 13 deletions.
1 change: 1 addition & 0 deletions public/modules/custom/asu_content/drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ services:
class: Drupal\asu_content\Commands\AsuContentDrushCommands
tags:
- { name: drush.command }
arguments: ['@entity_type.manager', '@database']
56 changes: 56 additions & 0 deletions public/modules/custom/asu_content/src/BatchService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Drupal\asu_content;

/**
* Batch service to update node aliases.
*/
class BatchService {

/**
* Batch process callback.
*
* @param int $id
* Id of the batch.
* @param array $nodes
* Nodes for batch.
* @param object $context
* Context for operations.
*/
public static function processContentAliasUpdate($id, $nodes, &$context) {
foreach ($nodes as $node) {
usleep(100);
$entityService = \Drupal::entityTypeManager();
$entity = $entityService->getStorage('node')->load($node->nid);

if ($entity) {
/** @var \Drupal\node\Entity $entity */
$entity->path->pathauto = 1;
$entity->save();
$context['results'][] = $id;
$context['message'] = t('processing "@id"',
['@id' => $id]
);
}
}
}

/**
* Batch Finished callback.
*
* @param bool $success
* Success of the operation.
* @param array $results
* Array of results for post processing.
* @param array $operations
* Array of operations.
*/
public static function processContentAliasUpdateFinished($success, array $results, array $operations) {
$messenger = \Drupal::messenger();

if ($success) {
$messenger->addMessage(t('@count results processed.', ['@count' => count($results)]));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Drupal\asu_content\Commands;

use Drupal\node\Entity\NodeType;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;

Expand All @@ -13,35 +15,100 @@
*/
class AsuContentDrushCommands extends DrushCommands {

use StringTranslationTrait;

/**
* Entity type service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;

/**
* Constructs a new AsuContentDrushCommands object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type service.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $connection) {
$this->entityTypeManager = $entityTypeManager;
$this->connection = $connection;
}

/**
* Create content aliases automatically.
*
* @param string $type
* The content type.
*
* @command asu_content:content-alias-create
*/
#[CLI\Command(name: 'asu_content:ContentAliasCreate', aliases: ['ac-cac'])]
#[CLI\Command(name: 'asu_content:contentAliasCreate', aliases: ['ac-cac'])]
#[CLI\Argument(name: 'type', description: 'Content type.')]
#[CLI\Usage(name: 'drush ac-cac', description: 'Create node alias automatically to content type.')]
public function ContentAliasCreate(string $type) {
$node_types = array_keys(NodeType::loadMultiple());
public function contentAliasCreate(string $type) {
$nodes = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
$node_types = array_keys($nodes);

if (!in_array($type, $node_types)) {
$this->output()->writeln('Content type doesnt exists');
return;
}

$node_ids = \Drupal::entityQuery('node')
->condition('type', $type)
->accessCheck(TRUE)
->execute();
// Create the operations array for the batch.
$operations = [];
$num_operations = 0;
$batch_id = 1;

$query = $this->connection->select('node_field_data', 'd');
$query->condition('d.type', $type);
$query->fields('d', ['nid']);
$results_count = count($query->execute()->fetchAll());

if ($results_count > 0) {
for ($i = 0; $i < $results_count; $i = $i + 100) {
$query->range($i, 100);
$results = $query->execute()->fetchAll();

// Prepare the operation. Here we could do other operations on nodes.
$this->output()->writeln("Preparing batch: " . $batch_id);

$operations[] = [
'\Drupal\asu_content\BatchService::processContentAliasUpdate',
[
$batch_id,
$results,
],
];

$batch_id++;
$num_operations++;
}

// Create the batch.
$batch = [
'title' => $this->t('Updating @num node aliases', ['@num' => $num_operations]),
'operations' => $operations,
'finished' => '\Drupal\asu_content\BatchService::processContentAliasUpdateFinished',
];

// Add batch operations as new batch sets.
batch_set($batch);

$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($node_ids);
// Process the batch sets.
drush_backend_batch_process();

foreach($nodes as $node) {
/** @var \Drupal\node\Entity $node */
$node->path->pathauto = 1;
$node->save();
// Show some information.
$this->logger()->notice("Batch operations end.");
}
}

Expand Down

0 comments on commit 4cd45c4

Please sign in to comment.