You are here

public function BackgroundProcess::queue in Background Process 7

Same name and namespace in other branches
  1. 8 background_process.class.php \BackgroundProcess::queue()
  2. 6 BackgroundProcess.class.php \BackgroundProcess::queue()

File

./BackgroundProcess.class.php, line 88
Class for handling background processes.

Class

BackgroundProcess
BackgroundProcess class.

Code

public function queue($callback, $args = array()) {
  if (!$this
    ->lock(BACKGROUND_PROCESS_STATUS_QUEUED)) {
    return FALSE;
  }
  $this->callback = $callback;
  $this->args = $args;
  if (!background_process_set_process($this->handle, $this->callback, $this->uid, $this->args, $this->token)) {

    // Could not update process
    return NULL;
  }
  module_invoke_all('background_process_pre_execute', $this->handle, $this->callback, $this->args, $this->token);

  // Initialize progress stats
  $old_db = db_set_active('background_process');
  progress_remove_progress($this->handle);
  db_set_active($old_db);
  $queues = variable_get('background_process_queues', array());
  $queue_name = isset($queues[$this->callback]) ? 'bgp:' . $queues[$this->callback] : 'background_process';
  $queue = DrupalQueue::get($queue_name);
  $queue
    ->createItem(array(
    rawurlencode($this->handle),
    rawurlencode($this->token),
  ));
  _background_process_ensure_cleanup($this->handle, TRUE);
}