You are here

function search_api_solr_cron in Search API Solr 7

Same name and namespace in other branches
  1. 8.3 search_api_solr.module \search_api_solr_cron()
  2. 8 search_api_solr.module \search_api_solr_cron()
  3. 8.2 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 73
Provides a Solr-based service class for the Search API.

Code

function search_api_solr_cron() {
  $action = variable_get('search_api_solr_cron_action', 'spellcheck');

  // 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 - variable_get('search_api_solr_last_optimize', 0) > 86340) {
    variable_set('search_api_solr_last_optimize', REQUEST_TIME);
    $conditions = array(
      'class' => 'search_api_solr_service',
      'enabled' => TRUE,
    );
    $count = 0;
    foreach (search_api_server_load_multiple(FALSE, $conditions) as $server) {
      try {
        $solr = $server
          ->getSolrConnection();
        if ($action != 'spellcheck') {
          $solr
            ->optimize(FALSE);
        }
        else {
          $params['rows'] = 0;
          $params['spellcheck'] = 'true';
          $params['spellcheck.build'] = 'true';
          $solr
            ->search(NULL, $params);
        }
        ++$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->name,
        ));
      }
    }
    if ($count) {
      $vars['@count'] = $count;
      if ($action != 'spellcheck') {
        watchdog('search_api_solr', 'Optimized @count Solr server(s).', $vars, WATCHDOG_INFO);
      }
      else {
        watchdog('search_api_solr', 'Rebuilt spellcheck dictionary on @count Solr server(s).', $vars, WATCHDOG_INFO);
      }
    }
  }
}