diff --git a/httprl.admin.inc b/httprl.admin.inc index 6561f81..e9f6777 100644 --- a/httprl.admin.inc +++ b/httprl.admin.inc @@ -15,6 +15,12 @@ function httprl_admin_settings_form() { '#default_value' => variable_get('httprl_server_addr', FALSE), '#description' => t('If left blank it will use the same server as the request. If set to -1 it will use the host name instead of an IP address. This controls the output of httprl_build_url_self()'), ); + $form['httprl_background_callback'] = array( + '#type' => 'checkbox', + '#title' => t('Enable background callbacks'), + '#default_value' => variable_get('httprl_background_callback', HTTPRL_BACKGROUND_CALLBACK), + '#description' => t('If disabled all background_callback keys will be turned into callback & httprl_queue_background_callback will return NULL and not queue up the request. Note that background callbacks will automatically be disabled if the site is in maintenance mode.'), + ); $form['timeouts'] = array( '#type' => 'fieldset', '#title' => t('Default timeouts'), @@ -104,7 +110,7 @@ function httprl_admin_settings_form_validate($form, &$form_state) { continue; } if ($values[$name] <= 0) { - form_set_error($name, t('Must be a postive number.')); + form_set_error($name, t('Must be a positive number.')); continue; } } diff --git a/httprl.module b/httprl.module index a00cef3..8ff7813 100644 --- a/httprl.module +++ b/httprl.module @@ -5,6 +5,11 @@ */ /** + * Default value + */ +define('HTTPRL_BACKGROUND_CALLBACK', TRUE); + +/** * Default maximum number of seconds a single request call may take. */ define('HTTPRL_TIMEOUT', 30.0); @@ -1758,7 +1763,14 @@ function httprl_post_processing($id, &$responses, &$output, $time_left = NULL) { && is_array($result->options['background_callback'][0]) && !empty($result->options['background_callback'][0]['function']) ) { - httprl_queue_background_callback($result->options['background_callback'], $result); + $call_is_queued = httprl_queue_background_callback($result->options['background_callback'], $result); + if (is_null($call_is_queued)) { + watchdog('httprl', 'Background callback attempted but it is disabled. Going to use a normal callback'); + unset($result->options['callback']); + $result->options['callback'] = $result->options['background_callback']; + unset($result->options['background_callback']); + httprl_run_callback($result); + } } // Copy the result to the output array. @@ -1877,6 +1889,18 @@ function httprl_queue_background_callback(&$args, &$result = NULL) { } $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]; @@ -1943,13 +1967,6 @@ function httprl_queue_background_callback(&$args, &$result = 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']; - } // Create data array and options for request. $options = array( 'data' => array( @@ -2344,7 +2361,8 @@ function _httprl_use_proxy($host) { /** * Returns a persistent variable. * - * This version ignores the $conf global and reads directly from the database. + * This version will read directly from the database if value is not in global + * $conf variable. * * Case-sensitivity of the variable_* functions depends on the database * collation used. To avoid problems, always use lower case for persistent @@ -2710,6 +2728,31 @@ function httprl_drupal_full_bootstrap() { return $full_bootstrap; } + +/** + * Sees if httprl can run a background callback. + * + * @return Bool + * TRUE or FALSE. + */ +function httprl_is_background_callback_capable() { + // Check if site is offline. + if ((defined('VERSION') && substr(VERSION, 0, 1) >= 7 && httprl_variable_get('maintenance_mode', 0)) || httprl_variable_get('site_offline', 0)) { + return FALSE; + } + // Check that the httprl_background_callback variable is enabled. + if (!httprl_variable_get('httprl_background_callback', HTTPRL_BACKGROUND_CALLBACK)) { + return FALSE; + } + // Check that the callback in menu works. + if (!menu_get_item('httprl_async_function_callback')) { + return FALSE; + } + + // All checks passed. + return TRUE; +} + /** * Sets the global user to the given user ID. *