1

I am trying to create a drush command to run a batch process. When I run the command, the batch runs just once, and I get this error :

in Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase->handleHttpException() (line 1026 of /var/www/html/web/modules/contrib/search_api_solr/src/SolrConnector/SolrConnectorPluginBase.php). Drupal\search_api_solr\SearchApiSolrException: Solr endpoint http://solr:8983/ not found (code: 404

This is my code, am I doing something wrong? Thanks for your help:

class Drush9CustomCommands extends DrushCommands {

  use StringTranslationTrait;
  private $entityTypeManager;
  protected $logger;
  protected $batchService;
  
  public function __construct(EntityTypeManagerInterface $entityTypeManager, LoggerInterface $logger, BatchService $batch_service) {
    parent::__construct();
    $this->entityTypeManager = $entityTypeManager;
    $this->logger = $logger;
    $this->batchService = $batch_service;
  }

  /**
   * Update Node.
   *
   * @command update:node
   * @aliases update-node
   *
   * @usage update:node foo
   *   foo is the type of node to update
   */
  public function updateNode() {

    $batch = array(
      'title' => t('Exporting'),
      'operations' => array(
        array([$this->batchService,'processMyNode'], array()),
      ),
      'finished' => [$this->batchService,'processMyNodeFinished'],
    );
    batch_set($batch);

    drush_backend_batch_process();
  }
}

Batchservice.php

class BatchService implements ContainerInjectionInterface {

  use StringTranslationTrait;
  protected $messenger;

  public function __construct(MessengerInterface $messenger) {
    $this->messenger = $messenger;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('messenger')
    );
  }

  /**
   * Batch process callback.
   *
   * @param int $id
   *   Id of the batch.
   * @param string $operation_details
   *   Details of the operation.
   * @param object $context
   *   Context for operations.
   */
  public function processMyNode( &$context) {
    if (!isset($context['sandbox']['total'])) {
      // Get node experience ids.
      $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
      $nids = $query
        ->condition('type', 'mynode')
        ->range(0,1000)
        ->accessCheck(FALSE)
        ->execute();

      $context['sandbox']['total'] = count($nids);
      $context['sandbox']['node_ids'] = $nids;
      $context['sandbox']['current'] = 0;
    }
    $node_ids = array_slice($context['sandbox']['node_ids'], $context['sandbox']['current'], 25);

    foreach ($node_ids as $nid) {
      $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
      $node->set('field_my_field', 'my field value');
      $node->save();
    }

    $context['sandbox']['current'] += count($node_ids);

    \Drupal::logger('test')->notice($context['sandbox']['current'] . ' Experiences passed / ' . $context['sandbox']['total']);

    if ($context['sandbox']['total'] == 0) {
      $context['sandbox']['#finished'] = 1;
    }
    else {

      $context['sandbox']['#finished'] = ($context['sandbox']['current'] / $context['sandbox']['total']);
    }
  }

  /**
   * 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  function processMyNodeFinished($success, array $results, array $operations) {

    if ($success) {
      $message = \Drupal::translation()->formatPlural(count($results), 'One post processed.', '@count posts processed.');
    }
    else {
      $message = t('Finished with an error.');
    }
    \Drupal::logger('teset')->notice($message);
  }

}

2 Answers 2

1

Search API Solr module cannot connect to the configured Solr server instance each time the script saves a node. The configured Solr instance is http://solr:8983/. You must configure a reachable instance.

3
  • Thanks @cilefen, I just configured the solr, you were right, there were no core, however my batch still runs once then stops, no error , nothing..are my two functions wrong?
    – el sparrow
    Commented May 1, 2022 at 23:03
  • That is a new question. Please accept my answer and ask another.
    – mona lisa
    Commented May 1, 2022 at 23:15
  • I already did it, thanks for your help!
    – el sparrow
    Commented May 1, 2022 at 23:16
2

Adding to @cilefen's answer, there was en error in the code : it should be $context['finished'] instead of $context['sandbox']['#finished'].After modifying this, the batch was running successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.