Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal hook_cron execution order

Tags:

cron

hook

drupal

What order does Drupal execute it's _cron hooks? It is important for a certain custom module I am developing and can't seem to find any documentation on it on the web. Maybe I'm searching for the wrong thing!

like image 443
Icode4food Avatar asked Jun 14 '10 19:06

Icode4food


3 Answers

Drupal executes all of its hooks in the order based off of module weight. Module weight defaults to 0, and the secondary ordering is alphabetical by module name:

http://api.drupal.org/api/function/module_list/6

like image 124
jhedstrom Avatar answered Oct 14 '22 04:10

jhedstrom


You can inspect and adjust the cron execution orders with the Supercron module. Some more details about this module (from its project page):

SuperCron is a complete replacement for Drupal's built-in Cron functionality. It allows you to:

  • See the list of all Cron hooks found in the enabled modules
  • Change the order in which cron hooks are called
  • Disable certain hooks
  • Run the tasks you choose in parallel, so that cron tasks will be executed all at once rather than one after the other
  • Identify the exceptions raised by individual hooks
  • Call hooks individually on demand (great for identifying problems)
  • Keep executing cron hooks that follow an exception, limiting the damage to only one module
  • Measure the time it takes for a cron hook to execute (we display the last call timings and the average timings)
  • Capture any output generated by the hooks
  • Change the way Cron behaves when the site is under load (this optional feature requires Throttle to be enabled)
  • Limit the IP addresses that may be allowed to call your cron scripts
like image 45
Kevin Avatar answered Oct 14 '22 02:10

Kevin


For Drupal 8 you have to rearrange modules' implementation order in hook_module_implements_alter:

function YOUR_MODULE_module_implements_alter(&$implementations, $hook) {

  // Move our hook_cron() implementation to the end of the list.
  if ($hook == 'cron') {
    $group = $implementations['YOUR_MODULE'];
    unset($implementations['YOUR_MODULE']);
    $implementations['YOUR_MODULE'] = $group;
  }
}

If you'd like to call your hook_cron first:

function YOUR_MODULE_module_implements_alter(&$implementations, $hook) {

  // Move our hook_cron() implementation to the top of the list.
  if ($hook == 'cron') {
    $group = $implementations['YOUR_MODULE'];
    $implementations = [
      'YOUR_MODULE' => $group,
    ] + $implementations;
  }
}
like image 33
Vladislav Avatar answered Oct 14 '22 04:10

Vladislav