diff --git a/README.txt b/README.txt index f574095..061eee0 100644 --- a/README.txt +++ b/README.txt @@ -265,3 +265,22 @@ Use 2 threads to load up 4 different nodes. echo httprl_pr($nodes); ?> + +Run a function in the background. Notice that there is no return or printed key +in the callback options. + + '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(); + ?> + diff --git a/httprl.async.inc b/httprl.async.inc index 1a1a991..bcf3493 100644 --- a/httprl.async.inc +++ b/httprl.async.inc @@ -14,16 +14,25 @@ function httprl_async_page() { // The temp_key is not set. // The temp_key does not start with httprl_. // 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']) || empty($_POST['temp_key']) || strpos($_POST['temp_key'], 'httprl_') !== 0 || $_POST['master_key'] != hash('sha512', drupal_get_private_key()) - || lock_may_be_available($_POST['temp_key']) ) { 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 (empty($_POST['mode'])) { httprl_background_processing('httprl async function callback running', FALSE); diff --git a/httprl.module b/httprl.module index 29c4649..b962eff 100644 --- a/httprl.module +++ b/httprl.module @@ -1183,8 +1183,18 @@ function httprl_queue_background_callback(&$args, &$result = NULL) { } // Acquire lock for this run. - $id = 'httprl_' . hash('sha512', mt_rand() . time()); - lock_acquire($id); + $locked = FALSE; + $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. if (empty($callback_options['url'])) { @@ -1200,7 +1210,6 @@ function httprl_queue_background_callback(&$args, &$result = NULL) { 'temp_key' => $id, 'mode' => $mode, 'function' => $callback_options['function'], - 'parent' => FALSE, 'args' => serialize($args), ), 'internal_states' => array(