Why might a batch initiated in hook_install() sometimes not process?

I have two modules, module_alpha and module_beta.

Both modules install a database table with hook_schema() which they use to track information about published nodes. Data is logged in the table with service methods executed from hook_node_insert() and hook_node_update() hooks. This all works.

But, I need to process all existing nodes when the module is installed to backfill the custom tables with information about the already existing published nodes. Each module implements hook_install() and uses a batch process to process all existing published nodes. However, I am seeing strange inconsistencies. Sometimes the batch process executes, sometimes it does not. A scenario may work for one module, but not the other. All of this even though the code is nearly identical. Here are the scenarios I’ve tested:

Module install method Module Alpha Module Beta
drush en MYMODULE Batch executes Batch does not execute
drush cim Batch does not execute Batch does not execute
UI, enable module from /admin/modules Batch executes Batch executes
UI, sync config from /admin/config/development/configuration Batch executes Batch executes

Batch executes indicates that the batch executes completely as expected, with progress messages, and my table is filled with data. Batch does not execute indicates that the batch is not executed at all. No error messages shown or in logs. The weirdest part about this data is that drush en module_alpha always succeeds and drush en module_beta always fails.

What could be causing the batch process to work in some cases but not in others? How can I consistently execute this batch process across all 4 of these methods, whether the module is enabled via UI or drush? To be honest, the only method I’ll use is drush cim

Code:

In MYMODULE.install:

function MYMODULE_install() {   $batch = new DrupalCoreBatchBatchBuilder();   $batch     ->setTitle(t('Bulk processing existing items.'))     // This operation references a different function per module.     // In both cases, the function lives in MYMODULE.module and the code is nearly identical.     ->addOperation('_MYMODULE_initialize_items', []);    batch_set($batch->toArray()); } 

In MYMODULE.module:

function _MYMODULE_initialize_items(&$context) {   $batch_size = 25;   $entity_type = 'node';   // I target different bundles in module_alpha and module_beta.   $bundle = 'MY_TARGET_BUNDLE';    if (!isset($context['sandbox']['processed'])) {     $context['sandbox']['processed'] = 0;   }    if (empty($context['sandbox']['entity_ids'])) {     $context['sandbox']['entity_ids'] = Drupal::entityTypeManager()       ->getStorage($entity_type)       ->getQuery()       ->condition('type', $bundle)       ->condition('status', 1)       ->execute();      if (is_array($context['sandbox']['entity_ids'])) {       $context['sandbox']['total'] = count($context['sandbox']['entity_ids']);     }   }    if (!empty($context['sandbox']['entity_ids'])) {     $current_batch_ids = array_slice($context['sandbox']['entity_ids'], $context['sandbox']['processed'], $batch_size);     $current_batch_entities = Drupal::entityTypeManager()       ->getStorage($entity_type)       ->loadMultiple($current_batch_ids);      foreach ($current_batch_entities as $entity) {       // I use a different services and methods in module_alpha and module_beta.       Drupal::service('MYMODULE.my_service')         ->processEntity($entity);        $context['sandbox']['processed']++;     }   }    if (!empty($context['sandbox']['total'])) {     $context['finished'] = $context['sandbox']['processed'] / $context['sandbox']['total'];     $context['message'] = t('Processed @processed of @total items.', [       '@processed' => $context['sandbox']['processed'],       '@total' => $context['sandbox']['total'],     ]);   }   else {     $context['finished'] = 1;   } } 

Example service method called in the batch process:

public function processEntity(EntityInterface $entity) {   // $this->db is injected `@database` service, i.e. Drupal::service('database');   return $this->db     ->insert('MYMODULE_mytable')     ->fields([       'entity_type' => $entity->getEntityTypeId(),       'entity_bundle' => $entity->bundle(),       'entity_uuid' => $entity->uuid(),       // etc...     ])     ->execute(); } 
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Why might a batch initiated in hook_install() sometimes not process?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.