| 1 |
<?php |
<?php |
| 2 |
/** |
/** |
| 3 |
* $Id: cronplus.module,v 1.7 2006/05/03 21:12:40 syscrusher Exp $ |
* $Id: cronplus.module,v 1.8 2007/03/22 23:41:18 syscrusher Exp $ |
| 4 |
* |
* |
| 5 |
* CronPlus enhances the Drupal cron feature to provide specific calls to |
* CronPlus enhances the Drupal cron feature to provide specific calls to |
| 6 |
* other modules at hourly, daily, weekly, monthly, and yearly intervals. |
* other modules at hourly, daily, weekly, monthly, and yearly intervals. |
| 189 |
*/ |
*/ |
| 190 |
function cronplus_cron() { |
function cronplus_cron() { |
| 191 |
// Gather information... |
// Gather information... |
| 192 |
|
$last_cron = intval(variable_get('cronplus_last_cron', 0)); |
| 193 |
$now = time(); |
$now = time(); |
| 194 |
$now_wkdy = intval(gmdate('w'), $now); // 0=Sunday |
$now_wkdy = intval(gmdate('w'), $now); // 0=Sunday |
| 195 |
// For the $now_week, weeks start on Monday, not Sunday. This is a mismatch |
// For the $now_week, weeks start on Monday, not Sunday. This is a mismatch |
| 200 |
$now_year = intval(gmdate('Y', $now)); |
$now_year = intval(gmdate('Y', $now)); |
| 201 |
$now_date = gmdate('Y-m-d', $now); |
$now_date = gmdate('Y-m-d', $now); |
| 202 |
$now_hour = intval(gmdate('H', $now)); |
$now_hour = intval(gmdate('H', $now)); |
| 203 |
$last_cron = intval(variable_get('cronplus_last_cron', 0)); |
|
| 204 |
$last_cron_fmt = gmdate('Y-m-d H:i:s', $last_cron); |
// Gather the last time each cronplus hook was invoked |
| 205 |
$last_cron_hour = intval(gmdate('H', $last_cron)); |
$cronplus_hourly_last = intval(variable_get('cronplus_hourly_last', 0)); |
| 206 |
$last_cron_week = intval(gmdate('W', $last_cron)); |
$cronplus_daily_last = intval(variable_get('cronplus_daily_last', 0)); |
| 207 |
$last_cron_year = intval(gmdate('Y', $last_cron)); |
$cronplus_weekly_last = intval(variable_get('cronplus_weekly_last', 0)); |
| 208 |
|
$cronplus_monthly_last = intval(variable_get('cronplus_monthly_last', 0)); |
| 209 |
|
$cronplus_yearly_last = intval(variable_get('cronplus_yearly_last', 0)); |
| 210 |
|
|
| 211 |
|
$last_cron_hour = intval(gmdate('H', $cronplus_hourly_last)); |
| 212 |
|
$last_cron_day = gmdate('Y-m-d', $cronplus_daily_last); |
| 213 |
|
$last_cron_week = intval(gmdate('W', $cronplus_weekly_last)); |
| 214 |
|
$last_cron_month = gmdate('Y-m', $cronplus_monthly_last); |
| 215 |
|
$last_cron_year = intval(gmdate('Y', $cronplus_yearly_last)); |
| 216 |
|
|
| 217 |
// Preferred hour affects daily, weekly, monthly, and yearly runs |
// Preferred hour affects daily, weekly, monthly, and yearly runs |
| 218 |
$prefer_hour = intval(variable_get('cronplus_preferred_hour', 0)); |
$prefer_hour = intval(variable_get('cronplus_preferred_hour', 0)); |
| 219 |
// Preferred wkdy affects weekly, monthly, and yearly runs |
// Preferred wkdy affects weekly, monthly, and yearly runs |
| 232 |
cronplus_invoke_hook('hourly', $now, $last_cron); |
cronplus_invoke_hook('hourly', $now, $last_cron); |
| 233 |
} |
} |
| 234 |
// Daily |
// Daily |
| 235 |
if (substr($last_cron_fmt, 0, 10) != $now_date && ($now_hour >= $prefer_hour || $interval > 96000)) { |
if (substr($last_cron_day, 0, 10) != $now_date && ($now_hour >= $prefer_hour || $interval > 96000)) { |
| 236 |
// The sleep() calls in the following logic ensure that calls to cronplus_invoke_hook() |
// The sleep() calls in the following logic ensure that calls to cronplus_invoke_hook() |
| 237 |
// cannot occur multiple times in the same second, no matter how fast the system is. |
// cannot occur multiple times in the same second, no matter how fast the system is. |
| 238 |
// This is needed because log entries have a precision of only seconds, and we want |
// This is needed because log entries have a precision of only seconds, and we want |
| 250 |
cronplus_invoke_hook('weekly', $now, $last_cron); |
cronplus_invoke_hook('weekly', $now, $last_cron); |
| 251 |
} |
} |
| 252 |
// Monthly |
// Monthly |
| 253 |
if (substr($last_cron_fmt, 0, 7) != $now_month && (($now_wkdy >= $prefer_wkdy && $now_hour >= $prefer_hour) || $interval > 86400*35)) { |
if ($now_month != $last_cron_month && (($now_wkdy >= $prefer_wkdy && $now_hour >= $prefer_hour) || $interval > 86400*35)) { |
| 254 |
sleep(1); |
sleep(1); |
| 255 |
cronplus_invoke_hook('monthly', $now, $last_cron); |
cronplus_invoke_hook('monthly', $now, $last_cron); |
| 256 |
} |
} |