You are here

function search_api_solr_cron in Search API Solr 8

Same name and namespace in other branches
  1. 8.3 search_api_solr.module \search_api_solr_cron()
  2. 8.2 search_api_solr.module \search_api_solr_cron()
  3. 7 search_api_solr.module \search_api_solr_cron()
  4. 4.x search_api_solr.module \search_api_solr_cron()

Implements hook_cron().

Used to execute an optimization operation on all enabled Solr servers once a day.

File

./search_api_solr.module, line 39
Provides a Solr-based service class for the Search API.

Code

function search_api_solr_cron() {
  $action = \Drupal::config('search_api_solr.settings')
    ->get('cron_action');

  // We treat all unknown action settings as "none". However, we turn a blind
  // eye for Britons and other people who can spell.
  if (!in_array($action, array(
    'spellcheck',
    'optimize',
    'optimise',
  ))) {
    return;
  }

  // 86400 seconds is one day. We use slightly less here to allow for some
  // variation in the request time of the cron run, so that the time of day will
  // (more or less) stay the same.
  if (REQUEST_TIME - \Drupal::state()
    ->get('search_api_solr.last_optimize') > 86340) {
    \Drupal::state()
      ->set('search_api_solr.last_optimize', REQUEST_TIME);

    // Get the IDs of all enabled servers which use the Solr backend.
    $ids = \Drupal::entityQuery('search_api_server')
      ->condition('backend', 'search_api_solr')
      ->condition('status', TRUE)
      ->execute();
    $count = 0;

    /** @var \Drupal\search_api\ServerInterface $server */
    foreach (Server::loadMultiple($ids) as $server) {
      try {

        /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
        $backend = $server
          ->getBackend();
        $connector = $backend
          ->getSolrConnector();
        if ($action != 'spellcheck') {
          $connector
            ->optimize();
        }
        else {
          $solarium_query = $connector
            ->getSelectQuery();
          $solarium_query
            ->setRows(0);
          $spellcheck = $solarium_query
            ->getSpellcheck();
          $spellcheck
            ->setBuild(TRUE);
          $connector
            ->execute($solarium_query);
        }
        ++$count;
      } catch (SearchApiException $e) {
        watchdog_exception('search_api_solr', $e, '%type while optimizing Solr server @server: @message in %function (line %line of %file).', array(
          '@server' => $server
            ->label(),
        ));
      }
    }
    if ($count) {
      $vars['@count'] = $count;
      if ($action != 'spellcheck') {
        \Drupal::logger('search_api_solr')
          ->info('Optimized @count Solr server(s).', $vars);
      }
      else {
        \Drupal::logger('search_api_solr')
          ->info('Rebuilt spellcheck dictionary on @count Solr server(s).', $vars);
      }
    }

    // Delete cached endpoint data once a day.
    \Drupal::state()
      ->delete('search_api_solr.endpoint.data');
  }
}