Skip to content

Instantly share code, notes, and snippets.

@berkes
Created August 5, 2010 09:11
Show Gist options
  • Select an option

  • Save berkes/509463 to your computer and use it in GitHub Desktop.

Select an option

Save berkes/509463 to your computer and use it in GitHub Desktop.
Drupal Hook_cron design pattern
/**
* Implementation of hook_cron().
*/
function example_cron() {
static $is_running;
$time = time();
//Call is_active. IF so, stor in variable.
if (!$is_running && _example_cron_may_run($time, variable_get('example_alive_last_ran', 0), variable_get('example_refresh_interval', 648000))) {
$is_running = TRUE;
example_do_complex_updates();
$is_running = FALSE;
variable_set('example_alive_last_ran', $time);
}
}
/**
* Determine if we must run another cron run, or if not enough time has passed yet.
*
* @param $now
* timestamp of now.
* @param $last_ran
* Timestamp of last ran date
* @param $interval
* Seconds to wait before next run.
*
* @return
* Description of return value
*/
function _example_cron_may_run($now, $last_ran, $interval) {
//how much time has passed between the last run and now?
$passed = $now - $last_ran;
//is that passed time larger then the amount of seconds to wait before another run?
return $passed > $interval ? TRUE : FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment