Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDST-58: Added drush command for OAI rebuild #25

Merged
merged 11 commits into from
May 14, 2024
6 changes: 6 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ services:
- '@logger.islandora_drush_utils'
tags:
- { name: drush.command }
islandora_drush_utils.rebuild_oai:
class: \Drupal\islandora_drush_utils\Commands\RebuildOaiEntries
arguments:
- '@queue'
tags:
- { name: drush.command }
106 changes: 106 additions & 0 deletions src/Commands/RebuildOaiEntries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Drupal\islandora_drush_utils\Commands;

use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Queue\QueueFactory;
use Drush\Commands\DrushCommands;

/**
* RebuildOaiEntries commands.
*
* These commands rebuild the OAI entries and consume them.
*/
class RebuildOaiEntries extends DrushCommands {

use DependencySerializationTrait;

/**
* The queue service.
*
* @var \Drupal\Core\Queue\QueueFactory
*/
protected $queue;

/**
* Set variables to used across commands.
*
* @param \Drupal\Core\Queue\QueueFactory $queue
* The queue service.
*/
public function __construct(QueueFactory $queue) {
$this->queue = $queue;
}

/**
* Rebuild OAI entries.
*
* @param array $options
* Additional command options.
*
* @option batchSize An integer of the amount of entries to process per batch.
*
* @command islandora_drush_utils:rebuild-oai
* @aliases idr:roai
*/
public function rebuild(array $options = [
'batchSize' => 10,
]) {
rest_oai_pmh_cache_views();

$batch = [
'title' => 'Processing OAI rebuild',
adam-vessey marked this conversation as resolved.
Show resolved Hide resolved
'finished' => 'rest_oai_pmh_batch_finished',
'operations' => [
[
[$this, 'rebuildBatch'],
[$options['batchSize']],
],
],
];

batch_set($batch);
drush_backend_batch_process();
}

/**
* Batch for processing OAI queue.
*
* @param int $batch_size
* The number of nodes to process at a single time.
* @param array|\DrushBatchContext $context
* The batch context.
*/
public function rebuildBatch(int $batch_size, &$context) {
adam-vessey marked this conversation as resolved.
Show resolved Hide resolved
$queue = $this->queue->get('rest_oai_pmh_views_cache_cron');
$queue_total_items = $queue->numberOfItems();

// If no items are found, set the context finished and show a message.
if (!($queue_total_items > 0)) {
$context['finished'] = 1;
}
else {
// Setting the defaults.
if (empty($context['sandbox'])) {
$context['sandbox']['processed_items'] = 0;
$context['sandbox']['total_items'] = $queue_total_items;
}

$start = $context['sandbox']['processed_items'];
$end = $start + $batch_size;
$end = min($end, $context['sandbox']['total_items']);

// Processing the queue items.
while ($context['sandbox']['processed_items'] < $end) {
$item = $queue->claimItem();
rest_oai_pmh_process_queue($item);

$context['sandbox']['processed_items']++;
}

// Set the batch progress percentage.
$context['finished'] = $context['sandbox']['processed_items'] / $context['sandbox']['total_items'];
}
axelerant-hardik marked this conversation as resolved.
Show resolved Hide resolved
}

}
Loading