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!
Drupal hook_cron execution order
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:
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
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
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;
}
}
Hooks execution is determinate from the weight of the module implementing them; the weightier module will be executed for last.
© 2022 - 2024 — McMap. All rights reserved.