| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
/** |
/** |
| 10 |
* Implementation of hook_help(). |
* Implementation of hook_menu(). |
| 11 |
*/ |
*/ |
| 12 |
function poormanscron_help($path, $arg) { |
function poormanscron_menu() { |
| 13 |
switch ($path) { |
$items['poormanscron/run-cron-check'] = array( |
| 14 |
case 'admin/help#poormanscron': |
'title' => 'Execute cron', |
| 15 |
return '<p>'. t('The Poormanscron module runs cron jobs without the need of the cron application.') .'</p>'; |
'page callback' => 'poormanscron_run_cron_check', |
| 16 |
case 'admin/settings/poormanscron': |
'access callback' => 'poormanscron_run_cron_check_access', |
| 17 |
return '<p>'. t('The settings provided here allow you to administer Poormancron.') .'</p>'; |
'type' => MENU_CALLBACK, |
| 18 |
|
); |
| 19 |
|
return $items; |
| 20 |
|
} |
| 21 |
|
|
| 22 |
|
/** |
| 23 |
|
* Implementation of hook_init(). |
| 24 |
|
*/ |
| 25 |
|
function poormanscron_init() { |
| 26 |
|
if (poormanscron_run_cron_check_access()) { |
| 27 |
|
drupal_add_js(drupal_get_path('module', 'poormanscron') . '/poormanscron.js'); |
| 28 |
|
drupal_add_js(array('cronCheck' => variable_get('cron_last', 0) + variable_get('cron_safe_threshold', 10800)), 'setting'); |
| 29 |
} |
} |
| 30 |
} |
} |
| 31 |
|
|
| 32 |
/** |
/** |
| 33 |
* Implementation of hook_exit(). |
* Checks if the feature to automatically run cron is enabled. |
| 34 |
* |
* |
| 35 |
* Checks if poormanscron needs to be run. If this is the case, it invokes |
* Also used as a menu access callback for this feature. |
| 36 |
* the cron hooks of all enabled modules, which are executed after |
* |
| 37 |
* all HTML is returned to the browser. So the user who kicks off the cron |
* @return |
| 38 |
* jobs should not notice any delay. |
* TRUE if cron threshold is enabled, FALSE otherwise. |
| 39 |
*/ |
* |
| 40 |
function poormanscron_exit() { |
* @see poormanscron_run_cron_check() |
| 41 |
|
*/ |
| 42 |
// Calculate when the next poormanscron run is due. |
function poormanscron_run_cron_check_access() { |
| 43 |
$lastrun = variable_get('poormanscron_lastrun', 0); |
return variable_get('cron_safe_threshold', 10800) > 0; |
| 44 |
$nextrun = $lastrun + 60 * variable_get('poormanscron_interval', 60); |
} |
|
|
|
|
// If the configured time has passed, start the next poormanscron run. |
|
|
if (time() > $nextrun) { |
|
|
|
|
|
// If this cron run fails to complete, wait a few minutes before retrying. |
|
|
variable_set('poormanscron_lastrun', |
|
|
$lastrun + 60 * variable_get('poormanscron_retry_interval', 10)); |
|
|
|
|
|
// Get the current Drupal messages. Use drupal_set_message() so that |
|
|
// the messages aren't deleted in case the cron run fails and |
|
|
// we don't get a chance to restore them below. |
|
|
$saved_messages = drupal_set_message(); |
|
|
|
|
|
// Invoke the cron hooks of all enabled modules. |
|
|
if (drupal_cron_run()) { |
|
|
$message = 'Cron run completed (via poormanscron).'; |
|
|
} |
|
|
else { |
|
|
$message = 'Cron run unsuccessful (via poormanscron).'; |
|
|
} |
|
|
|
|
|
// Write a message to the logs if the user wants us to do so. |
|
|
if (variable_get('poormanscron_log_cron_runs', 1) == 1) { |
|
|
watchdog('cron', $message); |
|
|
} |
|
|
|
|
|
$t = time(); |
|
| 45 |
|
|
| 46 |
// Update the time of the last poormanscron run (this one). |
/** |
| 47 |
variable_set('poormanscron_lastrun', $t); |
* Menu callback; executes cron via an image callback. |
| 48 |
|
* |
| 49 |
|
* This callback runs cron in a separate HTTP request to prevent "mysterious" |
| 50 |
|
* slow-downs of regular HTTP requests. It is invoked via an AJAX request |
| 51 |
|
* (if the client's browser supports JavaScript). |
| 52 |
|
* |
| 53 |
|
* @see poormanscron_run_cron_check_access() |
| 54 |
|
*/ |
| 55 |
|
function poormanscron_run_cron_check() { |
| 56 |
|
// Because we don't have a good way to disable a page from being cached |
| 57 |
|
// (like using page_is_cacheable in Drupal 7), we have to use this ugly hack. |
| 58 |
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
| 59 |
|
|
| 60 |
|
$cron_run = FALSE; |
| 61 |
|
$time = time(); |
| 62 |
|
|
| 63 |
|
// Cron threshold semaphore is used to avoid errors every time the image |
| 64 |
|
// callback is displayed when a previous cron is still running. |
| 65 |
|
$threshold_semaphore = variable_get('cron_threshold_semaphore', FALSE); |
| 66 |
|
if ($threshold_semaphore) { |
| 67 |
|
if ($time - $threshold_semaphore > 3600) { |
| 68 |
|
// Either cron has been running for more than an hour or the semaphore |
| 69 |
|
// was not reset due to a database error. |
| 70 |
|
watchdog('cron', 'Cron has been running for more than an hour and is most likely stuck.', array(), WATCHDOG_ERROR); |
| 71 |
|
|
| 72 |
// Delete any messages added during the cron run (and existing prior |
// Release the cron threshold semaphore. |
| 73 |
// messages). |
variable_del('cron_threshold_semaphore'); |
| 74 |
drupal_get_messages(); |
} |
| 75 |
|
} |
| 76 |
// Restore any prior messages. |
else { |
| 77 |
if (isset($saved_messages)) { |
// Run cron automatically if it has never run or threshold was crossed. |
| 78 |
foreach ($saved_messages as $type => $types_messages) { |
$cron_last = variable_get('cron_last', NULL); |
| 79 |
foreach ($types_messages as $message) { |
$cron_threshold = variable_get('cron_safe_threshold', 10800); |
| 80 |
drupal_set_message($message, $type); |
if (!isset($cron_last) || ($time - $cron_last > $cron_threshold)) { |
| 81 |
} |
// Lock cron threshold semaphore. |
| 82 |
} |
variable_set('cron_threshold_semaphore', $time); |
| 83 |
|
$cron_run = drupal_cron_run(); |
| 84 |
|
// Release the cron threshold semaphore. |
| 85 |
|
variable_del('cron_threshold_semaphore'); |
| 86 |
} |
} |
| 87 |
} |
} |
| 88 |
|
|
| 89 |
|
drupal_json(array('cron_run' => $cron_run)); |
| 90 |
|
exit; |
| 91 |
} |
} |
| 92 |
|
|
| 93 |
/** |
/** |
| 94 |
* Implmentation of hook_menu(). |
* Implementation of hook_form_FORM_ID_alter(). |
| 95 |
*/ |
*/ |
| 96 |
function poormanscron_menu() { |
function poormanscron_form_system_site_information_settings_alter(&$form, &$form_state) { |
| 97 |
$items = array(); |
$form['cron_safe_threshold'] = array( |
| 98 |
$items['admin/settings/poormanscron'] = array( |
'#type' => 'select', |
| 99 |
'title' => 'Poormanscron', |
'#title' => t('Automatically run cron'), |
| 100 |
'description' => 'A module which runs Drupal cron jobs without the cron application.', |
'#default_value' => variable_get('cron_safe_threshold', 10800), |
| 101 |
'page callback' => 'drupal_get_form', |
'#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 43200, 86400, 604800), 'format_interval'), |
| 102 |
'page arguments' => array('poormanscron_admin_settings'), |
'#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 <a href="@status-report-url">status report page</a>.', array('@status-report-url' => url('admin/reports/status'))), |
|
'access arguments' => array('administer site configuration'), |
|
|
'file' => 'poormanscron.admin.inc', |
|
| 103 |
); |
); |
| 104 |
return $items; |
$form['buttons'] += array('#weight' => 30); |
| 105 |
} |
} |