You are here

background_process.dispatchers.inc in Background Process 7.2

File

background_process.dispatchers.inc
View source
<?php

/**
 * Dispatcher for HTTP
 */
function background_process_dispatcher_http($process) {
  $process
    ->logDebug(__FUNCTION__);
  list($url, $headers) = background_process_build_request('bgp-start/' . $process
    ->getPID(), $process
    ->getServiceHost(), array(
    'query' => array(
      'handle' => $process
        ->getHandle(),
    ),
  ));
  $headers['Content-Type'] = 'application/x-www-form-urlencoded';
  $data = 'token=' . $process
    ->getToken();
  if ($process
    ->getPhase()) {
    $data .= '&process=' . base64_encode(serialize($process));
    $data .= '&module=' . base64_encode(drupal_get_path('module', 'background_process'));
    $data .= '&DRUPAL_ROOT=' . base64_encode(DRUPAL_ROOT);
    $url = url(drupal_get_path('module', 'background_process') . '/background_process.bootstrap.php', array(
      'query' => array(
        'handle' => $process
          ->getHandle(),
      ),
      'absolute' => TRUE,
    ));
  }
  $options = array(
    'method' => 'POST',
    'data' => $data,
    'headers' => $headers,
    'blocking' => FALSE,
  );
  $result = background_process_http_request($url, $options);
  $process->connection = $result->fp;
}

/**
 * Title callback for HTTP service hosts.
 */
function background_process_dispatcher_http_title($service_host) {
  return sprintf("http: %s - %s", $service_host['base_url'], $service_host['http_host']);
}

/**
 * Options callback for HTTP service hosts.
 */
function background_process_dispatcher_http_options($service_host) {
  global $base_url;
  $options = array(
    'base_url' => $base_url,
    'http_host' => isset($service_host['http_host']) ? $service_host['http_host'] : parse_url($base_url, PHP_URL_HOST),
  );
  return $options;
}

/**
 * Dispatcher for Drush
 */
function background_process_dispatcher_drush($process) {
  $service_hosts = background_process_get_service_hosts();
  $service_host = $service_hosts[$process
    ->getServiceHost()];
  $cmd = !empty($service_host['drush_cmd']) ? $service_host['drush_cmd'] : variable_get('background_process_drush_cmd', BACKGROUND_PROCESS_DRUSH_CMD);
  if (!empty($service_host['site_alias'])) {
    $cmd .= ' @' . $service_host['site_alias'];
  }
  $logfile = '/dev/null';
  $logfile = '/tmp/drupal-drush.log';
  $realcmd = "{$cmd} bgp-execute " . escapeshellarg($process
    ->getPID()) . " >> " . escapeshellarg($logfile) . " 2>&1 &";

  #error_log($realcmd);

  #$start = microtime(TRUE);
  exec($realcmd);

  #$end = microtime(TRUE);

  #error_log(sprintf("exec() took %.08f seconds", $end - $start));
  $process->connection = TRUE;
}

/**
 * Title callback for Drush service hosts.
 */
function background_process_dispatcher_drush_title($service_host) {
  return sprintf("drush: %s", $service_host['site_alias']);
}

/**
 * Options callback for Drush service hosts.
 */
function background_process_dispatcher_drush_options($service_host) {
  global $base_url;
  $options = array(
    'drush_cmd' => !empty($service_host['drush_cmd']) ? $service_host['drush_cmd'] : variable_get('background_process_drush_cmd', BACKGROUND_PROCESS_DRUSH_CMD),
    'site_alias' => !empty($service_host['site_alias']) ? $service_host['site_alias'] : '',
  );
  return $options;
}

/**
 * Dispatcher for queue
 */
function background_process_dispatcher_queue($process) {
  $service_hosts = background_process_get_service_hosts();
  $service_host = $service_hosts[$process
    ->getServiceHost()];
  $queue = DrupalQueue::get($service_host['queue']);

  // What to do about transactions here?
  // If queue processing is requested before THIS request is over,
  // the item might not be available to the cron worker.
  // 1.) Use another db connection instead of target, just like in 1.x?
  // 2.) Use a specific queue class?
  // 3.) Keep it simple. Queues are used for "whenever possible processing".
  $queue
    ->createItem($process
    ->getPID());
}

/**
 * Title callback for queue service hosts.
 */
function background_process_dispatcher_queue_title($service_host) {
  return sprintf("queue: %s", $service_host['queue']);
}

/**
 * Options callback for queue service hosts.
 */
function background_process_dispatcher_queue_options($service_host) {
  $options = array();
  $options['queue'] = isset($service_host['queue']) ? $service_host['queue'] : 'background_process';
  return $options;
}

/**
 * Dispatcher for running process in foreground
 */
function background_process_dispatcher_foreground($process) {
  $process
    ->execute();
}

/**
 * Title callback for foreground service hosts.
 */
function background_process_dispatcher_foreground_title($service_host) {
  return sprintf("foreground");
}

Functions

Namesort descending Description
background_process_dispatcher_drush Dispatcher for Drush
background_process_dispatcher_drush_options Options callback for Drush service hosts.
background_process_dispatcher_drush_title Title callback for Drush service hosts.
background_process_dispatcher_foreground Dispatcher for running process in foreground
background_process_dispatcher_foreground_title Title callback for foreground service hosts.
background_process_dispatcher_http Dispatcher for HTTP
background_process_dispatcher_http_options Options callback for HTTP service hosts.
background_process_dispatcher_http_title Title callback for HTTP service hosts.
background_process_dispatcher_queue Dispatcher for queue
background_process_dispatcher_queue_options Options callback for queue service hosts.
background_process_dispatcher_queue_title Title callback for queue service hosts.