You are here

function BackgroundProcess::dispatch in Background Process 6

Same name and namespace in other branches
  1. 8 background_process.class.php \BackgroundProcess::dispatch()
  2. 7.2 background_process.inc \BackgroundProcess::dispatch()
  3. 7 BackgroundProcess.class.php \BackgroundProcess::dispatch()
1 call to BackgroundProcess::dispatch()
BackgroundProcess::execute in ./BackgroundProcess.class.php

File

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

Class

BackgroundProcess
BackgroundProcess class.

Code

function dispatch() {
  if (variable_get('clean_url', 0) && variable_get('background_process_use_double_encoding', BACKGROUND_PROCESS_USE_DOUBLE_ENCODING)) {
    $handle = rawurlencode(rawurlencode($this->handle));
    $token = rawurlencode(rawurlencode($this->token));
  }
  else {
    $handle = rawurlencode($this->handle);
    $token = rawurlencode($this->token);
  }
  list($url, $headers) = background_process_build_request('bgp-start/' . $handle . '/' . $token, $this->service_host);
  background_process_set_service_host($this->handle, $this->service_host);
  $options = array(
    'method' => 'POST',
    'headers' => $headers,
  );
  if (variable_get('background_process_enable_diagnostics', FALSE)) {
    $options['blocking'] = TRUE;
    watchdog('bg_process', 'Calling: @url using options: !options', array(
      '@url' => $url,
      '!options' => str_replace("\n", '<br>', htmlspecialchars(print_r($options, TRUE))),
    ), WATCHDOG_DEBUG);
  }
  $result = background_process_http_request($url, $options);
  if (variable_get('background_process_enable_diagnostics', FALSE)) {
    watchdog('bp_process', 'Response from @url: !response', array(
      '@url' => $url,
      '!response' => str_replace("\n", '<br>', htmlspecialchars(print_r($result, TRUE))),
    ), WATCHDOG_DEBUG);
  }
  if (empty($result->error)) {
    $this->connection = $result->fp;
    _background_process_ensure_cleanup($this->handle, TRUE);
    return TRUE;
  }
  else {
    background_process_remove_process($this->handle);
    watchdog('bg_process', 'Could not call service %handle for callback %callback: !error', array(
      '%handle' => $this->handle,
      '%callback' => $this->callback,
      '!error' => print_r($result, TRUE),
    ), WATCHDOG_ERROR);

    // Throw exception here instead?
    return NULL;
  }
  return FALSE;
}