You are here

function apachesolr_index_batch_index_entities in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 apachesolr.admin.inc \apachesolr_index_batch_index_entities()
  2. 7 apachesolr.admin.inc \apachesolr_index_batch_index_entities()

Batch Operation Callback

Parameters

string $env_id: The machine name of the environment.

$total_limit: The total number of items to index across all batches

array $context:

Return value

false return false when an exception was caught

Throws

Exception When solr gives an error, throw an exception that solr is not available

1 string reference to 'apachesolr_index_batch_index_entities'
apachesolr_index_batch_index_remaining in ./apachesolr.admin.inc
Submit a batch job to index the remaining, non-indexed content.

File

./apachesolr.admin.inc, line 1146
Administrative pages for the Apache Solr framework.

Code

function apachesolr_index_batch_index_entities($env_id, $total_limit = NULL, &$context) {
  module_load_include('inc', 'apachesolr', 'apachesolr.index');
  if (empty($context['sandbox'])) {
    try {

      // Get the $solr object
      $solr = apachesolr_get_solr($env_id);

      // If there is no server available, don't continue.
      if (!$solr
        ->ping()) {
        throw new Exception(t('No Solr instance available during indexing.'));
      }
    } catch (Exception $e) {
      watchdog('Apache Solr', $e
        ->getMessage(), NULL, WATCHDOG_ERROR);
      return FALSE;
    }
    $status = apachesolr_index_status($env_id);
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['submitted'] = 0;

    // How many items do we want to index? All or a limited set of items
    if (empty($total_limit)) {
      $context['sandbox']['max'] = $status['remaining'];
    }
    else {
      $context['sandbox']['max'] = $total_limit;
    }
  }

  // We can safely process the apachesolr_cron_limit nodes at a time without a
  // timeout or out of memory error.
  $limit = variable_get('apachesolr_cron_limit', 50);

  // Reduce the limit for our final batch if we would be processing more than had been requested
  if ($limit + $context['sandbox']['progress'] > $context['sandbox']['max']) {
    $limit = $context['sandbox']['max'] - $context['sandbox']['progress'];
  }
  if ($context['sandbox']['max'] >= $context['sandbox']['progress'] + $limit) {
    $context['sandbox']['progress'] += $limit;
  }
  else {
    $context['sandbox']['progress'] = $context['sandbox']['max'];
  }
  $context['sandbox']['submitted'] += apachesolr_index_entities($env_id, $limit);
  $arguments = array(
    '@current' => $context['sandbox']['progress'],
    '@total' => $context['sandbox']['max'],
    '@submitted' => $context['sandbox']['submitted'],
  );
  $context['message'] = t('Inspected @current of @total entities. Submitted @submitted documents to Solr', $arguments);

  // Inform the batch engine that we are not finished, and provide an
  // estimation of the completion level we reached.
  $context['finished'] = empty($context['sandbox']['max']) ? 1 : $context['sandbox']['progress'] / $context['sandbox']['max'];

  // Put the total into the results section when we're finished so we can
  // show it to the admin.
  if ($context['finished']) {
    $context['results']['count'] = $context['sandbox']['progress'];
    $context['results']['submitted'] = $context['sandbox']['submitted'];
    $context['results']['env_id'] = $env_id;
  }
}