Index: modules/system/system.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.admin.inc,v retrieving revision 1.180 diff -u -r1.180 system.admin.inc --- modules/system/system.admin.inc 17 Aug 2009 19:14:41 -0000 1.180 +++ modules/system/system.admin.inc 19 Aug 2009 08:21:51 -0000 @@ -1298,6 +1298,14 @@ '#description' => t('Render all blocks on the default 404 (not found) page. Disabling blocks can help with performance but might leave users with a less functional site.'), '#default_value' => variable_get('site_404_blocks', 0) ); + $form['cron_safe_threshold'] = array( + '#type' => 'select', + '#title' => t('Automatically run cron'), + '#default_value' => variable_get('cron_safe_threshold', DRUPAL_DEFAULT_CRON_THRESHOLD), + '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 43200, 86400, 604800), 'format_interval'), + '#description' => t('When enabled, the site will check whether cron has been run in the configured interval and automatically run it upon the next page request. For more information visit the status report page.', array('@status-report-url' => url('admin/reports/status'))), + ); + $form['#validate'][] = 'system_site_information_settings_validate'; return system_settings_form($form); @@ -1316,6 +1324,11 @@ if (!menu_valid_path($item)) { form_set_error('site_frontpage', t("The path '@path' is either invalid or you do not have access to it.", array('@path' => $item['link_path']))); } + // Clear cache when enabling or disabling the cron threshold. + $cron_threshold = variable_get('cron_safe_threshold', DRUPAL_DEFAULT_CRON_THRESHOLD); + if (($cron_threshold > 0 && $form_state['input']['cron_safe_threshold'] == 0) || ($cron_threshold == 0 && $form_state['input']['cron_safe_threshold'] > 0)) { + cache_clear_all(); + } } /** Index: modules/system/system.module =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.module,v retrieving revision 1.750 diff -u -r1.750 system.module --- modules/system/system.module 17 Aug 2009 19:14:41 -0000 1.750 +++ modules/system/system.module 19 Aug 2009 08:36:40 -0000 @@ -42,6 +42,11 @@ define('DRUPAL_MAXIMUM_TEMP_FILE_AGE', 21600); /** + * Default interval between automatic cron executions in seconds. + */ +define('DRUPAL_DEFAULT_CRON_THRESHOLD', 10800); + +/** * New users will be set to the default time zone at registration. */ define('DRUPAL_USER_TIMEZONE_DEFAULT', 0); @@ -496,6 +501,12 @@ 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); + $items['system/run-cron-image'] = array( + 'title' => 'Execute cron', + 'page callback' => 'system_run_cron_image', + 'access callback' => '_system_run_cron_image_access', + 'type' => MENU_CALLBACK, + ); $items['admin'] = array( 'title' => 'Administer', 'access arguments' => array('access administration pages'), @@ -3053,3 +3064,55 @@ return $local; } + +/** + * Implement hook_page_alter(). + */ +function system_page_alter(&$page) { + // We always add triggers to run cron automatically when this feature is + // enabled; it avoids troubles with cached pages. + if (_system_run_cron_image_access()) { + $page['page_bottom']['run_cron'] = array( + // GET the image callback with XMLHttpRequest, it will work only + // when Javascript is available. + '#attached_js' => array( + '(function($){ $.get("'.url('system/run-cron-image').'"); })(jQuery);' => array('type' => 'inline', 'scope' => 'header'), + ), + // Image callback as HTML code for a graceful degradation when Javascript + // is not available. + '#markup' => '', + ); + } +} + +/** + * Menu callback; executes cron via an image callback. + */ +function system_run_cron_image() { + drupal_page_is_cacheable(FALSE); + + // Output a transparent 1x1 image to the browser. + drupal_set_header('Content-Type', 'image/gif'); + echo "\x47\x49\x46\x38\x39\x61\x1\x0\x1\x0\x80\xff\x0\xc0\xc0\xc0\x0\x0\x0\x21\xf9\x4\x1\x0\x0\x0\x0\x2c\x0\x0\x0\x0\x1\x0\x1\x0\x0\x2\x2\x44\x1\x0\x3b"; + + // Run cron automatically if it has never ran or threshold was crossed. + $cron_last = variable_get('cron_last', NULL); + $cron_threshold = variable_get('cron_safe_threshold', DRUPAL_DEFAULT_CRON_THRESHOLD); + if (!isset($cron_last) || (REQUEST_TIME - $cron_last > $cron_threshold)) { + drupal_cron_run(); + } + + exit; +} + +/** + * Checks if the feature to run cron automatically is enabled. + * + * It is also used as a menu access callback for this feature. + * + * @return + * TRUE if the cron threshold is enabled, FALSE otherwise. + */ +function _system_run_cron_image_access() { + return variable_get('cron_safe_threshold', DRUPAL_DEFAULT_CRON_THRESHOLD) > 0; +} Index: modules/system/system.test =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.test,v retrieving revision 1.64 diff -u -r1.64 system.test --- modules/system/system.test 17 Aug 2009 19:14:41 -0000 1.64 +++ modules/system/system.test 19 Aug 2009 08:35:51 -0000 @@ -391,6 +391,25 @@ } /** + * Ensure that the triggers to run cron automatically are added only when + * this functionality is enabled. + */ + function testAutomaticCronTriggers() { + global $base_url; + // Render a page with cron threshold enabled + variable_set('cron_safe_threshold', 10800); + $this->drupalGet($base_url); + $this->assertRaw('
', t('Image trigger for cron is present when enabled.')); + $this->assertRaw('(function($){ $.get("' . url('system/run-cron-image') . '"); })(jQuery);', t('Javascript trigger for cron is present when enabled.')); + + // Render a page with cron threshold disabled + variable_set('cron_safe_threshold', 0); + $this->drupalGet($base_url); + $this->assertNoRaw('
', t('Image trigger for cron is not present when disabled.')); + $this->assertNoRaw('(function($){ $.get("' . url('system/run-cron-image') . '"); })(jQuery);', t('Javascript trigger for cron is not present when disabled.')); + } + + /** * Ensure that temporary files are removed. */ function testTempFileCleanup() {