You are here

function _background_batch_initiate in Background Process 7

Same name and namespace in other branches
  1. 6 background_batch/background_batch.pages.inc \_background_batch_initiate()
  2. 7.2 background_batch/background_batch.pages.inc \_background_batch_initiate()

Start a batch job in the background

3 calls to _background_batch_initiate()
_background_batch_page_do_js in background_batch/background_batch.pages.inc
Do one pass of execution and inform back the browser about progression (used for JavaScript-mode only).
_background_batch_page_do_nojs in background_batch/background_batch.pages.inc
Output a batch processing page without JavaScript support.
_background_batch_page_start in background_batch/background_batch.pages.inc

File

background_batch/background_batch.pages.inc, line 120
Pages for background batch.

Code

function _background_batch_initiate($process = NULL) {
  require_once 'includes/batch.inc';
  $batch =& batch_get();
  $id = $batch['id'];
  $handle = 'background_batch:' . $id;
  if (!$process) {
    $process = background_process_get_process($handle);
  }
  if ($process) {

    // If batch is already in progress, goto to the status page instead of starting it.
    if ($process->exec_status == BACKGROUND_PROCESS_STATUS_RUNNING) {
      return $process;
    }

    // If process is locked and hasn't started for X seconds, then relaunch
    if ($process->exec_status == BACKGROUND_PROCESS_STATUS_LOCKED && $process->start_stamp + variable_get('background_process_redispatch_threshold', BACKGROUND_PROCESS_REDISPATCH_THRESHOLD) < time()) {
      $process = BackgroundProcess::load($process);
      $process
        ->dispatch();
    }
    return $process;
  }
  else {

    // Hasn't run yet or has stopped. (re)start batch job.
    $process = new BackgroundProcess($handle);
    $process->service_host = 'background_batch';
    if ($process
      ->lock()) {
      $message = $batch['sets'][0]['init_message'];
      progress_initialize_progress('_' . $handle, $message);
      if (function_exists('progress_set_progress_start')) {
        progress_set_progress_start('_' . $handle, $batch['timestamp']);
      }
      else {
        db_query("UPDATE {progress} SET start = :start WHERE name = :name", array(
          ':start' => $batch['timestamp'],
          ':name' => '_' . $handle,
        ));
      }
      $result = $process
        ->execute('_background_batch_process', array(
        $id,
      ));
      return $process;
    }
  }
}