Commit 1e7190e2 authored by generalredneck's avatar generalredneck

Moving batch processes out of service

Turns out that if you serialize a service which contains a container... it will cause an exception. This means I can't use the service and called to $this->finishRebuild() becuase it would have to serialize $this which is the VNS Service.
parent 55ab0302
......@@ -137,7 +137,7 @@ class ConfigurationForm extends ConfigFormBase {
* The form state.
*/
public function submitFormReindexOnly(array &$form, FormStateInterface $form_state) {
$this->viewsNaturalSort->queueDataForRebuild();
views_natural_sort_queue_data_for_rebuild();
}
}
......@@ -130,6 +130,7 @@ class ViewsNaturalSortService {
if (empty($views_supported_properties)) {
$supported_entity_properties = $this->getSupportedEntityProperties();
$views_data = $this->viewsData->getAll();
if (empty($views_data)) {
return FALSE;
}
......@@ -137,8 +138,8 @@ class ViewsNaturalSortService {
foreach ($properties as $property => $schema_info) {
if (!empty($views_data[$schema_info['base_table']][$schema_info['schema_field']]) &&
!empty($views_data[$schema_info['base_table']][$schema_info['schema_field']]['sort']) &&
!empty($views_data[$schema_info['base_table']][$schema_info['schema_field']]['sort']['handler']) &&
in_array($views_data[$schema_info['base_table']][$schema_info['schema_field']]['sort']['handler'], array('views_natural_sort_handler_sort', 'views_handler_sort'))) {
!empty($views_data[$schema_info['base_table']][$schema_info['schema_field']]['sort']['id']) &&
$views_data[$schema_info['base_table']][$schema_info['schema_field']]['sort']['id'] == 'natural') {
$views_supported_properties[$entity][$property] = $schema_info;
}
}
......@@ -195,50 +196,6 @@ class ViewsNaturalSortService {
batch_set($batch);
}
public function rebuildIndex($queue_name, &$context) {
/** @var QueueInterface $queue */
$queue = $this->queueFactory->get($queue_name);
/** @var QueueWorkerInterface $queue_worker */
$queue_worker = $this->queueManager->createInstance($queue_name);
$config = $this->configFactory->get('views_natural_sort.settings');
// Alias sandbox for easier referencing.
$sandbox = &$context['sandbox'];
// Alias results for easier referencing.
$results = &$context['results'];
if (empty($sandbox)) {
$sandbox['current'] = 0;
$sandbox['max'] = $queue->numberOfItems();
$sandbox['items_per_batch'] = $config->get('rebuild_items_per_batch');
}
for ($i = 0; $i < $sandbox['items_per_batch'] && $sandbox['current'] < $sandbox['max']; $i++) {
while($item = $queue->claimItem()) {
try {
$queue_worker->processItem($item->data);
$queue->deleteItem($item);
}
catch (SuspendQueueException $e) {
$queue->releaseItem($item);
break;
}
catch (\Exception $e) {
watchdog_exception('npq', $e);
}
}
$item = $queue->claimItem(10);
if ($item) {
views_natural_sort_process_index_queue($item->data);
$queue->deleteItem($item);
}
$sandbox['current']++;
}
$results['entries'] = $sandbox['current'];
if ($sandbox['current'] != $sandbox['max']) {
$context['finished'] = $sandbox['current'] / $sandbox['max'];
}
}
public function finishRebuild($success, $results, $operations) {
if ($success) {
drupal_set_message($this->t('Index rebuild has completed.'));
......
......@@ -43,13 +43,10 @@ function views_natural_sort_module_implements_alter(&$implements, $hook) {
function views_natural_sort_views_natural_sort_get_entry_types() {
$service = Drupal::service('views_natural_sort.service');
$supported_entity_properties = $service->getViewsSupportedEntityProperties();
$entry_types = array();
$entry_types = [];
foreach ($supported_entity_properties as $entity_type => $properties) {
foreach ($properties as $property => $schema_info) {
$entry_types[] = array(
'entity_type' => $entity_type,
'field' => $property,
);
$entry_types = new IndexRecordType($entity_type, $property);
}
}
return $entry_types;
......@@ -58,23 +55,25 @@ function views_natural_sort_views_natural_sort_get_entry_types() {
/**
* Implements hook_views_natural_sort_queue_rebuild_data().
*/
function views_natural_sort_views_natural_sort_queue_rebuild_data(IndexRecordType $entry_type){
function views_natural_sort_views_natural_sort_queue_rebuild_data(array $entry_types){
$service = Drupal::service('views_natural_sort.service');
$supported_entity_properties = $service->getViewsSupportedEntityProperties();
$entity_type = $entry_type->getEntityType();
$field = $entry_type->getField();
if (empty($supported_entity_properties[$entity_type]) ||
empty($supported_entity_properties[$entity_type][$field])) {
return;
}
$queue = Drupal::queue('views_natural_sort_entity_index');
$query = Drupal::entityQuery($entity_type);
$entity_ids = $query->execute();
foreach ($entity_ids as $entity_id) {
$queue->createItem($entity_id);
foreach ($entry_types as $entry_type) {
$entity_type = $entry_type->getEntityType();
$field = $entry_type->getField();
if (empty($supported_entity_properties[$entity_type]) ||
empty($supported_entity_properties[$entity_type][$field])) {
continue;
}
$query = Drupal::entityQuery($entity_type);
$entity_ids = $query->execute();
foreach ($entity_ids as $entity_id) {
$queue->createItem($entity_id);
}
}
return 'views_natural_sort_entity_index';
return ['views_natural_sort_entity_index'];
}
/**
......@@ -136,3 +135,79 @@ function views_natural_sort_remove($index_entry) {
}
$query->execute();
}
function views_natural_sort_queue_data_for_rebuild(array $entry_types = []) {
$moduleHandler = \Drupal::moduleHandler();
if (empty($entry_types)) {
$entry_types = $moduleHandler->invokeAll('views_natural_sort_get_entry_types');
}
$queues = array_unique($moduleHandler->invokeAll('views_natural_sort_queue_rebuild_data', $entry_types));
kpr($queues);
exit();
$operations = [];
foreach ($queues as $queue) {
$operations[] = [
'views_natural_sort_rebuild_index'
[$queue],
];
}
$batch = [
'operations' => $operations,
'title' => t('Rebuilding Views Natural Sort Indexing Entries'),
'finished' => 'views_natural_sort_finish_rebuild',
];
batch_set($batch);
}
function views_natural_sort_rebuild_index($queue_name, &$context) {
/** @var QueueInterface $queue */
$queue = \Drupal::queue($queue_name);
/** @var QueueWorkerInterface $queue_worker */
$queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name);
$config = \Drupal::configFactory()->get('views_natural_sort.settings');
// Alias sandbox for easier referencing.
$sandbox = &$context['sandbox'];
// Alias results for easier referencing.
$results = &$context['results'];
if (empty($sandbox)) {
$sandbox['current'] = 0;
$sandbox['max'] = $queue->numberOfItems();
$sandbox['items_per_batch'] = $config->get('rebuild_items_per_batch');
}
for ($i = 0; $i < $sandbox['items_per_batch'] && $sandbox['current'] < $sandbox['max']; $i++) {
while ($item = $queue->claimItem()) {
try {
$queue_worker->processItem($item->data);
$queue->deleteItem($item);
}
catch (SuspendQueueException $e) {
$queue->releaseItem($item);
break;
}
catch (\Exception $e) {
watchdog_exception('npq', $e);
}
}
$item = $queue->claimItem(10);
if ($item) {
views_natural_sort_process_index_queue($item->data);
$queue->deleteItem($item);
}
$sandbox['current']++;
}
$results['entries'] = $sandbox['current'];
if ($sandbox['current'] != $sandbox['max']) {
$context['finished'] = $sandbox['current'] / $sandbox['max'];
}
}
function views_natural_sort_finish_rebuild($success, $results, $operations) {
if ($success) {
drupal_set_message(t('Index rebuild has completed.'));
drupal_set_message(t('Indexed %count.', [
'%count' => \Drupal::translation()->formatPlural($results['entries'], '1 entry', '@count entries'),
]));
}
}
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