You are here

function _background_batch_process in Background Process 7.2

Same name and namespace in other branches
  1. 8 background_batch/background_batch.module \_background_batch_process()
  2. 6 background_batch/background_batch.module \_background_batch_process()
  3. 7 background_batch/background_batch.module \_background_batch_process()

Process a batch step

Parameters

type $id:

Return value

type

File

background_batch/background_batch.module, line 161
This module adds background processing to Drupals batch API

Code

function _background_batch_process($id = NULL) {
  $process = BackgroundProcess::currentProcess();
  if (!$process) {
    return;
  }
  if (!$id) {
    $process
      ->keepAlive(FALSE);
    return;
  }

  // Retrieve the current state of batch from db.
  $data = db_query("SELECT batch FROM {batch} WHERE bid = :bid", array(
    ':bid' => $id,
  ), array(
    'target' => 'background_process',
  ))
    ->fetchColumn();
  if (!$data) {
    $process
      ->keepAlive(FALSE);
    return;
  }
  require_once 'includes/batch.inc';
  $batch =& batch_get();
  $batch = unserialize($data);

  // Check if the current user owns (has access to) this batch.
  global $user;
  if ($batch['uid'] != $user->uid) {
    $process
      ->keepAlive(FALSE);
    return drupal_access_denied();
  }
  timer_start('background_batch_processing');
  $percentage = 0;
  $lifespan = $process
    ->getOption('batch_lifespan') * 1000;
  while ($percentage < 100) {
    $current_set =& _batch_current_set();
    $queue = _batch_queue($current_set);
    if ($queue
      ->numberOfItems() <= 0) {

      // There are no items in the queue ... so the batch is invalid!
      $current_set['count'] = 0;
      $percentage = 100;
      break;
    }
    list($percentage, $message) = _batch_process();

    // Restart background process after X miliseconds
    if (timer_read('background_batch_processing') > $lifespan) {
      break;
    }
  }
  if ($percentage >= 100) {
    $batch['finish_time'] = microtime(TRUE);
    $process
      ->setProgress(1);
    $process
      ->keepAlive(FALSE);
  }
}