function background_process_cron in Background Process 8
Same name and namespace in other branches
- 6 background_process.module \background_process_cron()
- 7.2 background_process.module \background_process_cron()
- 7 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 57 - This module implements a framework for calling funtions in the background.
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, [
':start' => $time - \Drupal::config('background_process.settings')
->get('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", [
'%handle' => $handle,
'@msg' => $msg,
]));
}
else {
drupal_set_message(t("%handle could not be unlocked: @msg", [
'%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;
}
$results = db_query_range("SELECT handle, start_stamp FROM {background_process} WHERE start_stamp < :start AND exec_status = :status", 0, 10, [
':start' => $time - \Drupal::config('background_process.settings')
->get('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", [
'%handle' => $handle,
'@msg' => $msg,
]));
}
else {
drupal_set_message(t("%handle could not be unlocked: @msg", [
'%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;
}
$results = db_query_range("SELECT handle, start_stamp FROM {background_process} WHERE start_stamp < :start AND exec_status = :status", 0, 10, [
':start' => $time - \Drupal::config('background_process.settings')
->get('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", [
'%handle' => $handle,
'@msg' => $msg,
]));
}
else {
drupal_set_message(t("%handle could not be unlocked: @msg", [
'%handle' => $handle,
'@msg' => $msg,
]), 'error');
}
}
} while (!empty($results));
}