function background_process_cron in Background Process 7
Same name and namespace in other branches
- 8 background_process.module \background_process_cron()
- 6 background_process.module \background_process_cron()
- 7.2 background_process.module \background_process_cron()
Implements hook_cron().
1 string reference to 'background_process_cron'
- background_process_cron_alter in ./
background_process.module - Implements hook_cron_alter().
File
- ./
background_process.module, line 108
Code
function background_process_cron() {
// Don't use more than 120 seconds to unlock.
$expire = 120;
@set_time_limit($expire);
// Cleanup old handles.
$time = time();
$msg = t('Never started (auto unlock due to timeout)');
do {
if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
break;
}
$result = db_query_range("SELECT handle, start_stamp FROM {background_process} WHERE start_stamp < :start AND exec_status = :status", 0, 10, array(
':start' => $time - variable_get('background_process_cleanup_age', BACKGROUND_PROCESS_CLEANUP_AGE),
':status' => BACKGROUND_PROCESS_STATUS_LOCKED,
));
$handles = $result
->fetchAllAssoc('handle', PDO::FETCH_ASSOC);
foreach ($handles as $handle => $process) {
// Unlock the process.
if (background_process_unlock($handle, $msg, $process['start_stamp'])) {
drupal_set_message(t("%handle unlocked: !msg", array(
'%handle' => $handle,
'!msg' => $msg,
)));
}
else {
drupal_set_message(t("%handle could not be unlocked: !msg", array(
'%handle' => $handle,
'!msg' => $msg,
)), 'error');
}
}
} while (!empty($handles));
// Cleanup stale requests.
$time = time();
$msg = t('Never finished (auto unlock due to long run)');
do {
if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
break;
}
$result = db_query_range("SELECT handle, start_stamp FROM {background_process} WHERE start_stamp < :start AND exec_status = :status", 0, 10, array(
':start' => $time - variable_get('background_process_cleanup_age_running', BACKGROUND_PROCESS_CLEANUP_AGE_RUNNING),
':status' => BACKGROUND_PROCESS_STATUS_RUNNING,
));
$handles = $result
->fetchAllAssoc('handle', PDO::FETCH_ASSOC);
foreach ($handles as $handle => $process) {
// Unlock the process.
if (background_process_unlock($handle, $msg, $process['start_stamp'])) {
drupal_set_message(t("%handle unlocked: !msg", array(
'%handle' => $handle,
'!msg' => $msg,
)));
}
else {
drupal_set_message(t("%handle could not be unlocked: !msg", array(
'%handle' => $handle,
'!msg' => $msg,
)), 'error');
}
}
} while (!empty($results));
// Cleanup queued requests that were never processed.
$time = time();
$msg = t('Never picked up by cron worker (auto unlock due to timeout)');
do {
if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
break;
}
$result = db_query_range("SELECT handle, start_stamp FROM {background_process} WHERE start_stamp < :start AND exec_status = :status", 0, 10, array(
':start' => $time - variable_get('background_process_cleanup_age_queue', BACKGROUND_PROCESS_CLEANUP_AGE_QUEUE),
':status' => BACKGROUND_PROCESS_STATUS_QUEUED,
));
$handles = $result
->fetchAllAssoc('handle', PDO::FETCH_ASSOC);
foreach ($handles as $handle => $process) {
// Unlock the process.
if (background_process_unlock($handle, $msg, $process['start_stamp'])) {
drupal_set_message(t("%handle unlocked: !msg", array(
'%handle' => $handle,
'!msg' => $msg,
)));
}
else {
drupal_set_message(t("%handle could not be unlocked: !msg", array(
'%handle' => $handle,
'!msg' => $msg,
)), 'error');
}
}
} while (!empty($results));
}