You are here

class BackgroundBatchContext in Background Process 7

Same name and namespace in other branches
  1. 6 background_batch/background_batch.module \BackgroundBatchContext
  2. 7.2 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 317
This module adds background processing to Drupals batch API

View source
class BackgroundBatchContext extends ArrayObject {
  private $batch = NULL;
  private $interval = NULL;
  private $progress = NULL;
  public function __construct() {
    $this->interval = variable_get('background_batch_delay', BACKGROUND_BATCH_DELAY) / 1000000;
    $args = func_get_args();
    return call_user_func_array(array(
      'parent',
      '__construct',
    ), $args);
  }

  /**
   * Set progress update interval in seconds (float).
   */
  public function setInterval($interval) {
    $this->interval = $interval;
  }

  /**
   * Override offsetSet().
   * Update progress if needed.
   */
  public function offsetSet($name, $value) {
    if ($name == 'finished') {
      if (!isset($this->batch)) {
        $this->batch =& batch_get();
        $this->progress = progress_get_progress('_background_batch:' . $this->batch['id']);
      }
      if ($this->batch) {
        $total = $this->batch['sets'][$this->batch['current_set']]['total'];
        $count = $this->batch['sets'][$this->batch['current_set']]['count'];
        $elapsed = $this->batch['sets'][$this->batch['current_set']]['elapsed'];
        $progress_message = $this->batch['sets'][$this->batch['current_set']]['progress_message'];
        $current = $total - $count;
        $step = 1 / $total;
        $base = $current * $step;
        $progress = $base + $value * $step;
        progress_estimate_completion($this->progress);
        $elapsed = floor($this->progress->current - $this->progress->start);
        $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($this->progress->estimate) - floor($this->progress->current)),
        );
        $message = strtr($progress_message, $values);
        $message .= $message && $this['message'] ? '<br/>' : '';
        $message .= $this['message'];
        progress_set_intervalled_progress('_background_batch:' . $this->batch['id'], $message ? $message : $this->progress->message, $progress, $this->interval);
      }
    }
    return parent::offsetSet($name, $value);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackgroundBatchContext::$batch private property
BackgroundBatchContext::$interval private property
BackgroundBatchContext::$progress private property
BackgroundBatchContext::offsetSet public function Override offsetSet(). Update progress if needed.
BackgroundBatchContext::setInterval public function Set progress update interval in seconds (float).
BackgroundBatchContext::__construct public function