You are here

function apachesolr_cron in Apache Solr Search 5.2

Same name and namespace in other branches
  1. 8 apachesolr.module \apachesolr_cron()
  2. 6.3 apachesolr.module \apachesolr_cron()
  3. 6 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 492
Integration with the Apache Solr search application.

Code

function apachesolr_cron() {

  // Mass update and delete functions are in the include file.
  include_once drupal_get_path('module', 'apachesolr') . '/apachesolr.index.inc';
  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', WATCHDOG_ERROR);
  }
}