Same name and namespace in other branches
  1. 10 core/includes/batch.inc \_batch_process()
  2. 7.x includes/batch.inc \_batch_process()
  3. 8.9.x core/includes/batch.inc \_batch_process()
  4. 9 core/includes/batch.inc \_batch_process()

Advance batch processing for 1 second (or process the whole batch if it was not set for progressive execution - e.g forms submitted by drupal_execute).

1 call to _batch_process()
_batch_do in includes/batch.inc
Do one pass of execution and inform back the browser about progression (used for JavaScript-mode only).

File

includes/batch.inc, line 165
Batch processing API for processes to run in multiple HTTP requests.

Code

function _batch_process() {
  $batch =& batch_get();
  $current_set =& _batch_current_set();
  $set_changed = TRUE;
  if ($batch['progressive']) {
    timer_start('batch_processing');
  }
  while (!$current_set['success']) {

    // If this is the first time we iterate this batch set in the current
    // request, we check if it requires an additional file for functions
    // definitions.
    if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
      include_once $current_set['file'];
    }
    $finished = 1;
    $task_message = '';
    if ((list($function, $args) = reset($current_set['operations'])) && function_exists($function)) {

      // Build the 'context' array, execute the function call,
      // and retrieve the user message.
      $batch_context = array(
        'sandbox' => &$current_set['sandbox'],
        'results' => &$current_set['results'],
        'finished' => &$finished,
        'message' => &$task_message,
      );

      // Process the current operation.
      call_user_func_array($function, array_merge($args, array(
        &$batch_context,
      )));
    }
    if ($finished >= 1) {

      // Make sure this step isn't counted double when computing $current.
      $finished = 0;

      // Remove the operation and clear the sandbox.
      array_shift($current_set['operations']);
      $current_set['sandbox'] = array();
    }

    // If the batch set is completed, browse through the remaining sets,
    // executing 'control sets' (stored form submit handlers) along the way -
    // this might in turn insert new batch sets.
    // Stop when we find a set that actually has operations.
    $set_changed = FALSE;
    $old_set = $current_set;
    while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
      $current_set =& _batch_current_set();
      $set_changed = TRUE;
    }

    // At this point, either $current_set is a 'real' batch set (has operations),
    // or all sets have been completed.
    // If we're in progressive mode, stop after 1 second.
    if ($batch['progressive'] && timer_read('batch_processing') > 1000) {
      break;
    }
  }
  if ($batch['progressive']) {

    // Gather progress information.
    // Reporting 100% progress will cause the whole batch to be considered
    // processed. If processing was paused right after moving to a new set,
    // we have to use the info from the new (unprocessed) one.
    if ($set_changed && isset($current_set['operations'])) {

      // Processing will continue with a fresh batch set.
      $remaining = count($current_set['operations']);
      $total = $current_set['total'];
      $progress_message = $current_set['init_message'];
      $task_message = '';
    }
    else {
      $remaining = count($old_set['operations']);
      $total = $old_set['total'];
      $progress_message = $old_set['progress_message'];
    }
    $current = $total - $remaining + $finished;
    $percentage = $total ? floor($current / $total * 100) : 100;
    $values = array(
      '@remaining' => $remaining,
      '@total' => $total,
      '@current' => floor($current),
      '@percentage' => $percentage,
    );
    $message = strtr($progress_message, $values) . '<br/>';
    $message .= $task_message ? $task_message : '&nbsp;';
    return array(
      $percentage,
      $message,
    );
  }
  else {

    // If we're not in progressive mode, the whole batch has been processed by now.
    return _batch_finished();
  }
}