Drupal hook_cron execution order
Asked Answered
G

4

5

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!

Gabi answered 14/6, 2010 at 19:16 Comment(0)
A
10

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

Ariose answered 14/6, 2010 at 21:7 Comment(1)
UPDATE system SET weight='N' WHERE name='my_module'. It's a common practice for modules that need later execution to place that in their hook_install(), but you can manually do that just as easily.Aegean
S
2

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
Shifrah answered 14/6, 2010 at 19:41 Comment(0)
P
1

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;
  }
}
Poisson answered 6/4, 2018 at 7:15 Comment(0)
F
0

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

Flew answered 15/6, 2010 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.