You are here

class BackgroundBatchContext in Background Process 7.2

Same name and namespace in other branches
  1. 6 background_batch/background_batch.module \BackgroundBatchContext
  2. 7 background_batch/background_batch.module \BackgroundBatchContext

Class batch context. Automatically updates progress when 'finished' index is changed.

Hierarchy

Expanded class hierarchy of BackgroundBatchContext

File

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

View source
class BackgroundBatchContext extends ArrayObject {
  private $batch = NULL;
  private $process = NULL;
  public function __construct() {
    $args = func_get_args();
    return call_user_func_array(array(
      'parent',
      '__construct',
    ), $args);
  }
  public function getBatch() {
    if (!$this->batch) {
      $this->batch =& batch_get();
    }
    return $this->batch;
  }
  public function getProcess() {
    if (!$this->process) {
      $this->process = BackgroundProcess::loadByHandle('background_batch:' . $this->batch['id']);
      $this->process
        ->setProgressInterval(variable_get('background_batch_delay', BACKGROUND_BATCH_DELAY));
    }
    return $this->process;
  }

  /*
    public function __destruct() {
      $this->offsetSet('finished', 0);
    }
  */
  public static function processMessage($current_set, $finished = 0, $eta = 0) {
    $total = $current_set['total'];
    $count = $current_set['count'];
    $elapsed = $current_set['elapsed'];
    $start_time = $current_set['start'];
    $progress_message = $current_set['progress_message'];
    $current = $total - $count;
    $step = 1 / $total;
    $base = $current * $step;
    $progress = $base + $finished * $step;
    $time = microtime(TRUE);
    $elapsed = floor($time - $start_time);
    $values = array(
      '@remaining' => $count,
      '@total' => $total,
      '@current' => $current,
      '@percentage' => $progress * 100,
      '@elapsed' => format_interval($elapsed),
      // If possible, estimate remaining processing time.
      '@estimate' => format_interval(floor($eta) - floor($time)),
    );
    $message = strtr($progress_message, $values);
    return array(
      $progress,
      $message,
    );
  }

  /**
   * Override offsetSet().
   * Update progress if needed.
   */
  public function offsetSet($name, $value) {
    if ($name == 'finished' || ($name = 'message')) {
      if ($this
        ->getBatch() && $this
        ->getProcess()) {
        list($progress, $message) = self::processMessage($this->batch['sets'][$this->batch['current_set']], $value, $this->process
          ->calculateETA());
        $message .= $message && $this['message'] ? '<br/>' : '';
        $message .= $this['message'];
        if ($progress >= $this->process
          ->getProgress()) {
          $this->process
            ->setProgress($progress, $message ? $message : NULL);
        }
      }
    }
    return parent::offsetSet($name, $value);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackgroundBatchContext::$batch private property
BackgroundBatchContext::$process private property
BackgroundBatchContext::getBatch public function
BackgroundBatchContext::getProcess public function
BackgroundBatchContext::offsetSet public function Override offsetSet(). Update progress if needed.
BackgroundBatchContext::processMessage public static function
BackgroundBatchContext::__construct public function