You are here

function apachesolr_cron in Apache Solr Search 6

Same name and namespace in other branches
  1. 8 apachesolr.module \apachesolr_cron()
  2. 5.2 apachesolr.module \apachesolr_cron()
  3. 6.3 apachesolr.module \apachesolr_cron()
  4. 6.2 apachesolr.module \apachesolr_cron()
  5. 7 apachesolr.module \apachesolr_cron()

Implementation of hook_cron().

File

./apachesolr.module, line 531
Integration with the Apache Solr search application.

Code

function apachesolr_cron() {

  // Indexes in read-only mode do not change the index, so will not update, delete, or optimize during cron.
  if (variable_get('apachesolr_read_only', APACHESOLR_READ_WRITE) == APACHESOLR_READ_ONLY) {
    return;
  }

  // Mass update and delete functions are in the include file.
  module_load_include('inc', 'apachesolr', 'apachesolr.index');
  apachesolr_cron_check_node_table();
  try {
    $solr = apachesolr_get_solr();

    // Optimize the index (by default once a day).
    $optimize_interval = variable_get('apachesolr_optimize_interval', 60 * 60 * 24);
    $last = variable_get('apachesolr_last_optimize', 0);
    $time = time();
    if ($optimize_interval && $time - $last > $optimize_interval) {
      $solr
        ->optimize(FALSE, FALSE);
      variable_set('apachesolr_last_optimize', $time);
      apachesolr_index_updated($time);
    }

    // Only clear the cache if the index changed.
    // TODO: clear on some schedule if running multi-site.
    $updated = apachesolr_index_updated();
    if ($updated) {
      $solr
        ->clearCache();

      // Re-populate the luke cache.
      $solr
        ->getLuke();

      // TODO: an admin interface for setting this.  Assume for now 5 minutes.
      if ($time - $updated >= variable_get('apachesolr_cache_delay', 300)) {

        // Clear the updated flag.
        apachesolr_index_updated(FALSE);
      }
    }
  } catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e
      ->getMessage())) . ' in apachesolr_cron', NULL, WATCHDOG_ERROR);
  }
}