You are here

function search_api_solr_cron in Search API Solr 8.2

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. 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.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\search_api\SearchApiException

File

./search_api_solr.module, line 40

Code

function search_api_solr_cron() {
  $request_time = \Drupal::time()
    ->getRequestTime();
  $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, [
    '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);
    foreach (search_api_solr_get_servers() as $server) {
      try {

        /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
        $backend = $server
          ->getBackend();
        $connector = $backend
          ->getSolrConnector();
        $indexes = $server
          ->getIndexes();
        if (!$indexes) {
          continue;
        }
        foreach ($indexes as $index) {
          $backend
            ->finalizeIndex($index);
          $backend
            ->getLogger()
            ->info('Finalized Solr server @server.', [
            '@server' => $server
              ->label(),
          ]);
        }
        if ($action != 'spellcheck') {
          $connector
            ->optimize();
          $backend
            ->getLogger()
            ->info('Optimized Solr server @server.', [
            '@server' => $server
              ->label(),
          ]);
        }
        else {
          $autocomplete_query = $connector
            ->getAutocompleteQuery();
          $spellcheck_component = $autocomplete_query
            ->getSpellcheck();
          $spellcheck_component
            ->setBuild(TRUE);

          // Terms don't need to be build. Suggesters are configured to be
          // buildOnCommit.
          $connector
            ->execute($autocomplete_query);
          $backend
            ->getLogger()
            ->info('Rebuilt spellcheck dictionary on Solr server @server.', [
            '@server' => $server
              ->label(),
          ]);
        }
      } catch (SearchApiException $e) {
        watchdog_exception('search_api', $e, '%type while optimizing Solr server @server: @message in %function (line %line of %file).', [
          '@server' => $server
            ->label(),
        ]);
      }
    }

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