Icode4food
Icode4food

Reputation: 8694

Drupal hook_cron execution order

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!

Upvotes: 5

Views: 2984

Answers (4)

Vladislav
Vladislav

Reputation: 1995

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;
  }
}

Upvotes: 1

Kevin
Kevin

Reputation: 13226

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

Upvotes: 2

avpaderno
avpaderno

Reputation: 29689

Hooks execution is determinate from the weight of the module implementing them; the weightier module will be executed for last.

Upvotes: 0

jhedstrom
jhedstrom

Reputation: 3388

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

Upvotes: 10

Related Questions