Commit 02c1649b authored by mikeytown2's avatar mikeytown2

Issue #1427958 by mikeytown2: add in new example and fix some locking issues.

parent 4ef702aa
...@@ -265,3 +265,22 @@ Use 2 threads to load up 4 different nodes. ...@@ -265,3 +265,22 @@ Use 2 threads to load up 4 different nodes.
echo httprl_pr($nodes); echo httprl_pr($nodes);
?> ?>
Run a function in the background. Notice that there is no return or printed key
in the callback options.
<?php
// Setup callback options array; call watchdog in the background.
$callback_options = array(
array(
'function' => 'watchdog',
),
'httprl-test', 'background watchdog call done', array(), WATCHDOG_DEBUG,
);
// Queue up the request.
httprl_queue_background_callback($callback_options);
// Execute request.
httprl_send_request();
?>
...@@ -14,16 +14,25 @@ function httprl_async_page() { ...@@ -14,16 +14,25 @@ function httprl_async_page() {
// The temp_key is not set. // The temp_key is not set.
// The temp_key does not start with httprl_. // The temp_key does not start with httprl_.
// The master_key does not match the md5 of drupal_get_private_key(). // The master_key does not match the md5 of drupal_get_private_key().
// The temp_key does not match a lock that has been taken.
if ( empty($_POST['master_key']) if ( empty($_POST['master_key'])
|| empty($_POST['temp_key']) || empty($_POST['temp_key'])
|| strpos($_POST['temp_key'], 'httprl_') !== 0 || strpos($_POST['temp_key'], 'httprl_') !== 0
|| $_POST['master_key'] != hash('sha512', drupal_get_private_key()) || $_POST['master_key'] != hash('sha512', drupal_get_private_key())
|| lock_may_be_available($_POST['temp_key'])
) { ) {
httprl_fast403(); httprl_fast403();
} }
// Exit if the temp_key does not match a lock that has been taken.
// Wait up to 5 seconds for the lock to propagate out.
$tries = 0;
while (lock_may_be_available($_POST['temp_key'])) {
$tries++;
if ($tries > 5) {
httprl_fast403();
}
sleep(1);
}
// If request was a non blocking one, cut the connection right here. // If request was a non blocking one, cut the connection right here.
if (empty($_POST['mode'])) { if (empty($_POST['mode'])) {
httprl_background_processing('httprl async function callback running', FALSE); httprl_background_processing('httprl async function callback running', FALSE);
......
...@@ -1183,8 +1183,18 @@ function httprl_queue_background_callback(&$args, &$result = NULL) { ...@@ -1183,8 +1183,18 @@ function httprl_queue_background_callback(&$args, &$result = NULL) {
} }
// Acquire lock for this run. // Acquire lock for this run.
$id = 'httprl_' . hash('sha512', mt_rand() . time()); $locked = FALSE;
lock_acquire($id); $counter = 0;
while (!$locked && $counter < 20) {
$id = 'httprl_' . hash('sha512', mt_rand() . time());
$locked = lock_acquire($id);
}
// Make sure lock exists after this process is dead.
if (empty($mode)) {
global $locks;
unset($locks[$id]);
}
// Get URL to call function in background. // Get URL to call function in background.
if (empty($callback_options['url'])) { if (empty($callback_options['url'])) {
...@@ -1200,7 +1210,6 @@ function httprl_queue_background_callback(&$args, &$result = NULL) { ...@@ -1200,7 +1210,6 @@ function httprl_queue_background_callback(&$args, &$result = NULL) {
'temp_key' => $id, 'temp_key' => $id,
'mode' => $mode, 'mode' => $mode,
'function' => $callback_options['function'], 'function' => $callback_options['function'],
'parent' => FALSE,
'args' => serialize($args), 'args' => serialize($args),
), ),
'internal_states' => array( 'internal_states' => array(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment