You are here

function background_process_cron in Background Process 6

Same name and namespace in other branches
  1. 8 background_process.module \background_process_cron()
  2. 7.2 background_process.module \background_process_cron()
  3. 7 background_process.module \background_process_cron()

Implements hook_cron().

File

./background_process.module, line 126

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 \n      FROM {background_process}\n      WHERE start_stamp < '%d'\n      AND exec_status = %d", $time - variable_get('background_process_cleanup_age', BACKGROUND_PROCESS_CLEANUP_AGE), BACKGROUND_PROCESS_STATUS_LOCKED, 0, 10);
    $handles = array();
    while ($row = db_fetch_array($result)) {
      $handles[$row['handle']] = $row;
    }
    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\n      FROM {background_process}\n      WHERE start_stamp < '%d'\n      AND exec_status = %d", $time - variable_get('background_process_cleanup_age_running', BACKGROUND_PROCESS_CLEANUP_AGE_RUNNING), BACKGROUND_PROCESS_STATUS_RUNNING, 0, 10);
    $handles = array();
    while ($row = db_fetch_array($result)) {
      $handles[$row['handle']] = $row;
    }
    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\n      FROM {background_process}\n      WHERE start_stamp < '%d'\n      AND exec_status = %d", $time - variable_get('background_process_cleanup_age_queue', BACKGROUND_PROCESS_CLEANUP_AGE_QUEUE), BACKGROUND_PROCESS_STATUS_QUEUED, 0, 10);
    $handles = array();
    while ($row = db_fetch_array($result)) {
      $handles[$row['handle']] = $row;
    }
    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));
}