You are here

function apachesolr_cron in Apache Solr Search 6.2

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 apachesolr.module \apachesolr_cron()
  5. 7 apachesolr.module \apachesolr_cron()

Implementation of hook_cron().

File

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

Code

function apachesolr_cron() {

  // 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();

    // Check for unpublished content that wasn't deleted from the index.
    $result = db_query("SELECT n.nid, n.status FROM {apachesolr_search_node} asn INNER JOIN {node} n ON n.nid = asn.nid WHERE asn.status <> n.status");
    while ($node = db_fetch_object($result)) {
      apachesolr_nodeapi_update($node, FALSE);
    }

    // Check for deleted content that wasn't deleted from the index.
    $result = db_query("SELECT asn.nid FROM {apachesolr_search_node} asn LEFT JOIN {node} n ON n.nid = asn.nid WHERE n.nid IS NULL");
    while ($node = db_fetch_object($result)) {
      apachesolr_nodeapi_delete($node, FALSE);
    }

    // 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_set_last_updated($time);
    }

    // Only clear the cache if the index changed.
    // TODO: clear on some schedule if running multi-site.
    $updated = apachesolr_index_get_last_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_set_last_updated(0);
      }
    }
  } catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e
      ->getMessage())) . ' in apachesolr_cron', NULL, WATCHDOG_ERROR);
  }
}