You are here

function background_process_cron in Background Process 7.2

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

Implements hook_cron().

File

./background_process.module, line 265

Code

function background_process_cron() {

  // Don't run for more than 2 minutes. If timeout exceeds, the rest will be handled during the next run.
  $expire = 120;
  set_time_limit($expire);

  // Redispatch requests that didn't make it through
  $time = time();
  $pids = array(
    0,
  );

  // Easier to just do a NOT IN (0), than make a conditional query build below :-)
  do {
    if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
      break;
    }
    $results = db_select('background_process', 'bp', array(
      'target' => 'background_process',
    ))
      ->fields('bp')
      ->condition('bp.created', time() - 10, '<')
      ->condition('bp.created', time() - variable_get('background_process_cleanup_age', BACKGROUND_PROCESS_CLEANUP_AGE), '>')
      ->condition('bp.exec_status', BACKGROUND_PROCESS_STATUS_LOCKED)
      ->condition('bp.pid', $pids, 'NOT IN')
      ->range(0, 10)
      ->orderBy('bp.pid')
      ->execute()
      ->fetchAll(PDO::FETCH_OBJ);
    foreach ($results as $result) {
      $pids[] = $result->pid;
      $process = BackgroundProcess::create($result);
      $process
        ->reDispatch();
      watchdog('bg_process', 'Redispatched process %handle (%pid)', array(
        '%handle' => $process->handle,
        '%pid' => $process->pid,
      ), WATCHDOG_INFO);
    }
  } while (!empty($results));

  // Cleanup old requests that never started
  $time = time();
  $msg = t('Never started (auto unlock due to timeout)');
  do {
    if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
      break;
    }
    $results = db_select('background_process', 'bp', array(
      'target' => 'background_process',
    ))
      ->fields('bp')
      ->condition('bp.created', time() - variable_get('background_process_cleanup_age', BACKGROUND_PROCESS_CLEANUP_AGE), '<')
      ->condition('bp.exec_status', BACKGROUND_PROCESS_STATUS_LOCKED)
      ->range(0, 10)
      ->orderBy('bp.pid')
      ->execute()
      ->fetchAll(PDO::FETCH_OBJ);
    foreach ($results as $result) {
      $process = BackgroundProcess::create($result);
      $process
        ->log($msg)
        ->unlock();
      watchdog('bg_process', 'Background process %handle (%pid) unlocked: !msg', array(
        '%handle' => $process->handle,
        '%pid' => $process->pid,
        '!msg' => $msg,
      ), WATCHDOG_INFO);
    }
  } while (!empty($results));

  // 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_select('background_process', 'bp', array(
      'target' => 'background_process',
    ))
      ->fields('bp')
      ->condition('bp.start_stamp', $time - variable_get('background_process_running_cleanup_age', BACKGROUND_PROCESS_RUNNING_CLEANUP_AGE), '<')
      ->condition('bp.exec_status', BACKGROUND_PROCESS_STATUS_RUNNING)
      ->range(0, 10)
      ->execute()
      ->fetchAll(PDO::FETCH_OBJ);
    foreach ($results as $result) {
      $process = BackgroundProcess::create($result);
      $process
        ->log($msg)
        ->unlock();
      watchdog('bg_process', 'Background process %handle (%pid) unlocked: !msg', array(
        '%handle' => $process->handle,
        '%pid' => $process->pid,
        '!msg' => $msg,
      ), WATCHDOG_INFO);
    }
  } while (!empty($results));

  // Cleanup queued requests that were never processed
  $time = time();
  $msg = t('Never started (auto unlock due to timeout)');
  do {
    if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
      break;
    }
    $results = db_select('background_process', 'bp', array(
      'target' => 'background_process',
    ))
      ->fields('bp')
      ->condition('bp.created', $time - variable_get('background_process_queue_cleanup_age', BACKGROUND_PROCESS_QUEUE_CLEANUP_AGE), '<')
      ->condition('bp.exec_status', BACKGROUND_PROCESS_STATUS_QUEUED)
      ->range(0, 10)
      ->execute()
      ->fetchAll(PDO::FETCH_OBJ);
    foreach ($results as $result) {
      $process = BackgroundProcess::create($result);
      $process
        ->log($msg)
        ->unlock();
      watchdog('bg_process', 'Background process %handle (%pid) unlocked: !msg', array(
        '%handle' => $process->handle,
        '%pid' => $process->pid,
        '!msg' => $msg,
      ), WATCHDOG_INFO);
    }
  } while (!empty($results));

  // Cleanup result log
  $time = time();
  do {
    if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
      break;
    }
    $pids = db_select('background_process_result', 'r', array(
      'target' => 'background_process',
    ))
      ->fields('r', array(
      'pid',
    ))
      ->condition('r.created', $time - variable_get('background_process_result_cleanup_age', BACKGROUND_PROCESS_RESULT_CLEANUP_AGE), '<')
      ->range(0, 100)
      ->execute()
      ->fetchAllKeyed(0, 0);
    if ($pids) {
      db_delete('background_process_result', array(
        'target' => 'background_process',
      ))
        ->condition('pid', $pids, 'IN')
        ->execute();
    }
  } while (!empty($pids));
}