You are here

function _background_batch_page_do_js in Background Process 7.2

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

Do one pass of execution and inform back the browser about progression (used for JavaScript-mode only).

1 call to _background_batch_page_do_js()
background_batch_page in background_batch/background_batch.pages.inc
State-based dispatcher for the batch processing page.

File

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

Code

function _background_batch_page_do_js() {

  // HTTP POST required.
  if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    drupal_set_message(t('HTTP POST is required.'), 'error');
    drupal_set_title(t('Error'));
    return '';
  }
  $batch =& batch_get();
  $id = $batch['id'];
  drupal_save_session(FALSE);

  // Get progress
  $handle = 'background_batch:' . $id;
  $process = BackgroundProcess::loadByHandle($handle);
  if ($process) {

    // If process is locked and hasn't started for 10 seconds, then relaunch

    #$process->reDispatch();
  }
  elseif ($batch['sets'][$batch['current_set']]['count'] == 0) {

    // The background process has self-destructed, and the batch job is done.
    $percentage = 100;
    $message = '';
  }
  else {

    // Not running?
    _background_batch_initiate(FALSE);
    $percentage = t('N/A');
    $message = '';

    // Refetch progress information
    $process = BackgroundProcess::loadByHandle($handle);
  }
  if ($process) {
    $percentage = $process
      ->getProgress() * 100;
    $message = $process
      ->getProgressMessage();
    $eta = $process
      ->calculateETA();

    // Check wether ETA information should be shown.
    if (variable_get('background_batch_show_eta', BACKGROUND_BATCH_PROCESS_ETA)) {
      $message = "ETA: " . ($eta ? format_date((int) $eta, 'large') : t('N/A')) . "<br/>{$message}";
    }
    else {
      $js_setting['batch']['initMessage'] = $message;
    }

    // If process is locked and hasn't started for 10 seconds, then relaunch
    $process
      ->reDispatch();
  }
  drupal_json_output(array(
    'status' => TRUE,
    'percentage' => sprintf("%.02f", $percentage),
    'message' => $message,
  ));
}