You are here

function _background_batch_initiate in Background Process 7.2

Same name and namespace in other branches
  1. 6 background_batch/background_batch.pages.inc \_background_batch_initiate()
  2. 7 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 103
Pages for background batch.

Code

function _background_batch_initiate($page) {
  require_once 'includes/batch.inc';
  $batch =& batch_get();
  $id = $batch['id'];
  $handle = 'background_batch:' . $id;
  $process = BackgroundProcess::loadByHandle($handle);
  if ($process) {
    if ($process
      ->getStatus() == BACKGROUND_PROCESS_STATUS_RUNNING) {

      // If batch is already in progress, goto to the status page instead of starting it.
      return;
    }

    // If process is locked and hasn't started for 10 seconds, then relaunch
    $process
      ->reDispatch();
    return;
  }

  // Let's start it!
  try {
    global $user;
    $process = BackgroundProcess::lock($handle)
      ->setServiceGroup(variable_get('background_batch_default_service_group', variable_get('background_process_default_service_group', 'default')))
      ->setUID($user->uid)
      ->setProgress(0, $batch['sets'][0]['init_message'])
      ->setCallback('_background_batch_process', array(
      $id,
    ))
      ->setOption('batch_lifespan', variable_get('background_batch_process_lifespan', BACKGROUND_BATCH_PROCESS_LIFESPAN))
      ->setShutdownCallback('_batch_shutdown')
      ->keepAlive()
      ->ensureServiceHost();
    if ($process
      ->getDispatcher() == 'foreground') {
      if ($page) {
        drupal_set_message(t('Batch is using the "foreground" dispatcher. If you leave this page, the batch processing will stop/pause.'), 'warning');
        return;
      }
      else {

        // Lifespan defaults to 1 second when running in foreground ... just like core.
        $process
          ->setOption('batch_lifespan', 1);
      }
    }
    $process
      ->dispatch();
  } catch (Exception $e) {

    // Race condition? Job already running?
    throw $e;
  }
}