function apachesolr_cron in Apache Solr Search 7
Same name and namespace in other branches
- 8 apachesolr.module \apachesolr_cron()
- 5.2 apachesolr.module \apachesolr_cron()
- 6.3 apachesolr.module \apachesolr_cron()
- 6 apachesolr.module \apachesolr_cron()
- 6.2 apachesolr.module \apachesolr_cron()
Implements hook_cron(). Runs the indexing process on all writable environments or just a given environment.
1 call to apachesolr_cron()
- apachesolr_index_action_form_cron_submit in ./
apachesolr.admin.inc - Submit handler for the deletion form.
File
- ./
apachesolr.module, line 940 - Integration with the Apache Solr search application.
Code
function apachesolr_cron($env_id = NULL) {
$environments = array();
if (empty($env_id)) {
$environments = array_keys(apachesolr_load_all_environments());
}
else {
$environments[] = $env_id;
}
module_load_include('inc', 'apachesolr', 'apachesolr.index');
// Optimize the index (by default once a day).
$optimize_interval = variable_get('apachesolr_optimize_interval', 60 * 60 * 24);
// Protect from too-frequent optimizations.
$optimize_attempt_interval = variable_get('apachesolr_optimize_attempt_interval', 60 * 60);
foreach ($environments as $env_id) {
// Indexes in read-only mode do not change the index, so will not update, delete, or optimize during cron.
if (apachesolr_environment_variable_get($env_id, 'apachesolr_read_only', APACHESOLR_READ_WRITE) == APACHESOLR_READ_ONLY) {
continue;
}
// For every entity type that requires extra validation
foreach (entity_get_info() as $type => $info) {
$bundles = apachesolr_get_index_bundles($env_id, $type);
// If we're not checking any bundles of this entity type, just skip them all.
if (empty($bundles)) {
continue;
}
if (isset($info['apachesolr']['cron_check'])) {
$callback = $info['apachesolr']['cron_check'];
call_user_func($callback);
}
}
// Optimize index.
$time = time();
$last_optimize_success = apachesolr_environment_variable_get($env_id, 'apachesolr_last_optimize_success', 0);
$last_optimize_attempt = apachesolr_environment_variable_get($env_id, 'apachesolr_last_optimize_attempt', 0);
if ($optimize_interval && $optimize_attempt_interval && $time - $last_optimize_success >= $optimize_interval && $time - $last_optimize_attempt >= $optimize_attempt_interval) {
try {
$solr = apachesolr_get_solr($env_id);
apachesolr_environment_variable_set($env_id, 'apachesolr_last_optimize_attempt', $time);
$solr
->optimize(FALSE, FALSE, 600);
apachesolr_environment_variable_set($env_id, 'apachesolr_last_optimize_success', $time);
apachesolr_set_last_index_updated($env_id, $time);
} catch (Exception $e) {
apachesolr_log_exception($env_id, $e);
}
}
// Only clear the cache if the index changed.
// TODO: clear on some schedule if running multi-site.
$updated = apachesolr_get_last_index_updated($env_id);
if ($updated > 0) {
try {
$solr = apachesolr_get_solr($env_id);
$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_set_last_index_updated($env_id);
}
} catch (Exception $e) {
apachesolr_log_exception($env_id, $e);
}
}
// We can safely process the apachesolr_cron_limit nodes at a time without a
// timeout or out of memory error.
$limit = variable_get('apachesolr_cron_limit', 50);
apachesolr_index_entities($env_id, $limit);
}
}