You are here

public function BackgroundProcess::queue in Background Process 8

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

Implements Queues.

File

./background_process.class.php, line 74

Class

BackgroundProcess
BackgroundProcess class.

Code

public function queue($callback, $args = []) {
  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)) {
    return NULL;
  }
  \Drupal::moduleHandler()
    ->invokeAll('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 = \Drupal::config('background_process.settings')
    ->get('background_process_queues');
  $queue_name = isset($queues[$this->callback]) ? 'bgp:' . $queues[$this->callback] : 'background_process';
  $queue = DrupalQueue::get($queue_name);
  $queue
    ->createItem([
    rawurlencode($this->handle),
    rawurlencode($this->token),
  ]);
  _background_process_ensure_cleanup($this->handle, TRUE);
}