In Magento 1, instead of declaring the cron expression directly like this:
<crontab>
<jobs>
<vendor_module_cron>
<schedule>*/30 * * * *</schedule>
<run>
<model>module/observer::runCron</model>
</run>
</vendor_module_cron>
</jobs>
</crontab>
you could setup your crontab like this in the config.xml
:
<crontab>
<jobs>
<vendor_module_cron>
<schedule>
<config_path>vendor/module/cron_expr</config_path>
</schedule>
<run>
<model>module/observer::runCron</model>
</run>
</vendor_module_cron>
</jobs>
</crontab>
Where Magento would use Mage::getStoreConfig((string)$jobConfig->schedule->config_path)
to get the cron schedule expression.
This way you could dynamically change the schedule from the backend without having to modify the config.xml
file directly.
How can we achieve this in Magento 2 ? Is it still possible ?
Best Answer
Following ProductAlerts in M2:
You should be able to define something similarly:
You could leverage the way they interpret the expression through multiple fields but it shouldn't be required, such as:
This would make the input user-friendly (so-to-speak) but is not required, a simple text field that allows you to enter an expression like
7 7 * * *
would be fine as well, I would just put some validation on that field to make sure the expression is correct.