6

Does Magento 2's crontab area load a scoped di.xml file? Or is this no longer used? Or should it be used, but its not being loaded as part of the crontab application is a bug? Or do I fundamentally misunderstand something?

In Magento 2, the "areas" feature allows you to load extra configuration information based on the request context (that's a little hand wavy, but accurate from a certain point of view).

For example, Magento will always load the following di.xml

./vendor/magento/module-tax/etc/di.xml

but will only load the following di.xml when in the frontend area

./vendor/magento/module-tax/etc/frontend/di.xml

It's not clear if this is true for the di.xml files for the crontab area.

The cron runner in Magento is a separate Magento System Application from the Magento System Application that handles HTTP requests. When the cron application launches, its Magento System Application has a very minimal launch

#File: vendor/magento/framework/App/Cron.php
public function launch()
{
    $this->_state->setAreaCode('crontab');
    $this->_eventManager->dispatch('default');
    $this->_response->setCode(0);
    return $this->_response;
}

You can see the call to setAreaCode where the crontab area code is set. This ensures that requests for configuration trees will merge in any configuration located in a module's etc/crontab folder

app/etc/crontab/*.xml

However -- di.xml is different/special. Because Magento needs access to the object manager earlier than the launch of the Magento system application, the object manager initially loads all the etc/di.xml files before the call to setAreaCode.

During an HTTP request, the Magento System Application (Magento\Framework\App\Http) loads the area specific di.xml files after setting the area code

#File: vendor/magento/framework/App/Http.php
public function launch()
{
    $areaCode = $this->_areaList->getCodeByFrontName($this->_request->getFrontName());
    $this->_state->setAreaCode($areaCode);
    $this->_objectManager->configure($this->_configLoader->load($areaCode));
    //...
}

Since this call to configure is missing from the cron Application's launch method, it seems like the crontab doesn't load any etc/crontab/di.xml files. However, there are a few etc/crontab/di.xml files present.

$ find . -wholename '*crontab/di.xml'
./vendor/magento/module-captcha/etc/crontab/di.xml
./vendor/magento/module-catalog-rule-configurable/etc/crontab/di.xml

This creates some confusion about what the correct behavior of the system should be. Has anyone here been following Magento 2 development closely enough to know what the correct system behavior is?

1
  • 1
    basing on all you've described, it should load crontab area. We'll look deeper into it and fix, if necessary. Thanks for the observation.
    – BuskaMuza
    Dec 1, 2015 at 16:02

1 Answer 1

3

Based on feedback from the core team, it sounds like the current behavior is not the intended behavior, and in future versions the crontab application will load in a crontab area's di.xml.

3
  • 1
    Aaaaaaaand...? This still seems broken as of 2.1.5 Apr 9, 2017 at 14:30
  • Also it doesn't work in 2.2.7...
    – sergei.sss
    Feb 25, 2019 at 20:58
  • crontab/di.xml seems to be correctly read in 2.4.3-p2 at least :)
    – Simon
    Aug 2, 2022 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.