Score:1

Batch API drush command running once then shows Solr error

ve flag

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);
  }

}
Score:2
ve flag

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.

Score:1
id flag

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.

jacksparrow avatar
ve flag
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?
id flag
That is a new question. Please accept my answer and ask another.
jacksparrow avatar
ve flag
I already did it, thanks for your help!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.