#331611 by Damien Tournoud: Add poorman's cron-like feature to core. From: damz --- system/system.admin.inc | 8 ++++++++ system/system.module | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 0 deletions(-) diff --git modules/system/system.admin.inc modules/system/system.admin.inc index 3aa003c..b612cdc 100644 --- modules/system/system.admin.inc +++ modules/system/system.admin.inc @@ -1241,6 +1241,14 @@ function system_site_information_settings() { '#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', 10800), + '#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); diff --git modules/system/system.module modules/system/system.module index 7389341..1bb2f9b 100644 --- modules/system/system.module +++ modules/system/system.module @@ -476,6 +476,13 @@ function system_menu() { 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); + $items['system/cron/%'] = array( + 'title' => 'Execute cron', + 'page callback' => 'system_execute_cron', + 'page arguments' => array(2), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); $items['admin'] = array( 'title' => 'Administer', 'access arguments' => array('access administration pages'), @@ -2569,3 +2576,35 @@ function system_retrieve_file($path) { } return $local; } + +/** + * Implement hook_footer(). + * + * Add a transparent 1x1 image in the closure of the page when the cron needs + * to run. + */ +function system_footer() { + if ($cron_treshold = variable_get('cron_safe_threshold', 10800)) { + $cron_last = variable_get('cron_last', NULL); + if (!isset($cron_last) || (REQUEST_TIME - $cron_last > $cron_treshold)) { + return ''; + } + } +} + +/** + * Menu callback; execute the cron via an image callback. + */ +function system_execute_cron($token) { + $cron_last = variable_get('cron_last', NULL); + if (drupal_valid_token($token, $cron_last)) { + // 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"; + + // Execute the code. + drupal_cron_run(); + + exit; + } +}