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