function httprl_queue_background_callback in HTTP Parallel Request & Threading Library 6
Same name and namespace in other branches
- 7 httprl.module \httprl_queue_background_callback()
Run callback in the background.
Will run the given callback returning values and what might have been printed by that function, as well as respecting any pass by reference values.
Parameters
array $args: An array of arguments, first key value pair is used to control the callback function. The rest of the key value pairs will be arguments for the callback function.
object $result: (optional) An object from httprl_send_request. If this is set, this will be the first argument of the function.
5 calls to httprl_queue_background_callback()
- httprl.examples.php in examples/
httprl.examples.php - HTTP Parallel Request Library code examples.
- httprl_call_user_func_array_async in ./
httprl.module - Run the callback with the given params in the background.
- httprl_install_http_test in ./
httprl.install - Issue a HTTP request to admin/httprl-test, verifying that the server got it.
- httprl_post_processing in ./
httprl.module - Run post processing on the request if we are done reading.
- httprl_qcinp in ./
httprl.module - Queue Callback to run In a New Process.
File
- ./
httprl.module, line 2210 - HTTP Parallel Request Library module.
Code
function httprl_queue_background_callback(&$args, &$result = NULL) {
// Use a counter to prevent key collisions in httprl_send_request.
static $counter;
if (!isset($counter)) {
$counter = 0;
}
$counter++;
if (!httprl_is_background_callback_capable()) {
return NULL;
}
// Get URL to call function in background.
if (empty($callback_options['url'])) {
$url = httprl_build_url_self('httprl_async_function_callback?count=' . $counter);
}
else {
$url = $callback_options['url'];
}
// Get options.
$callback_options = $args[0];
if (is_null($result)) {
array_shift($args);
}
else {
// Merge in this request by reference.
$args[0] =& $result;
}
// Set blocking mode.
if (isset($callback_options['return']) || isset($callback_options['printed'])) {
$mode = TRUE;
}
else {
$mode = FALSE;
}
// Make sure some array keys exist.
if (!isset($callback_options['return'])) {
$callback_options['return'] = '';
}
if (!isset($callback_options['function'])) {
$callback_options['function'] = '';
}
// Get the maximum amount of time this could take.
$times = array(
httprl_variable_get('httprl_timeout', HTTPRL_TIMEOUT),
httprl_variable_get('httprl_global_timeout', HTTPRL_GLOBAL_TIMEOUT),
);
if (isset($callback_options['options']['timeout'])) {
$times[] = $callback_options['options']['timeout'];
}
if (isset($callback_options['options']['global_timeout'])) {
$times[] = $callback_options['options']['global_timeout'];
}
// Create lock name for this run.
$available = FALSE;
$lock_counter = 0;
while (!$available && $lock_counter < 20) {
// 512 bits = 64 bytes.
if (function_exists('drupal_random_bytes')) {
$name = 'httprl_' . hash('sha512', drupal_random_bytes(64));
}
elseif (function_exists('openssl_random_pseudo_bytes')) {
$name = 'httprl_' . hash('sha512', openssl_random_pseudo_bytes(64));
}
else {
$name = 'httprl_' . hash('sha512', mt_rand() . microtime(TRUE) . serialize($_SERVER));
}
$available = lock_may_be_available($name);
$lock_counter++;
}
$callback_options['options']['lock_name'] = $name;
// Create data array and options for request.
$options = array(
'data' => array(
'master_key' => hash('sha512', httprl_drupal_get_private_key()),
'temp_key' => $name,
'mode' => $mode,
'php_timeout' => max($times),
'function' => $callback_options['function'],
'context' => isset($callback_options['context']) ? $callback_options['context'] : array(),
// Follow rfc4648 for base64url
// @see http://tools.ietf.org/html/rfc4648#page-7
'args' => strtr(base64_encode(serialize($args)), array(
'+' => '-',
'/' => '_',
)),
),
'internal_states' => array(
'background_function_return' => &$callback_options['return'],
'background_function_args' => &$args,
),
'blocking' => $mode,
'method' => 'POST',
);
if (isset($callback_options['printed'])) {
$options['internal_states']['background_function_printed'] =& $callback_options['printed'];
}
if (isset($callback_options['options']) && is_array($callback_options['options'])) {
$options += $callback_options['options'];
}
// Set Host header.
if (empty($options['headers']['Host'])) {
$hostname = httprl_get_hostname();
if (!empty($hostname)) {
$options['headers']['Host'] = $hostname;
}
}
// Set Session header if requested to.
if (!empty($callback_options['context']['session']) && !empty($_COOKIE[session_name()])) {
if (!isset($options['headers']['Cookie'])) {
$options['headers']['Cookie'] = '';
}
$options['headers']['Cookie'] = session_name() . '=' . $_COOKIE[session_name()] . ';';
}
// Send Request.
return httprl_request($url, $options);
}