Sometimes you need to add the ability to change cron expression for separate tasks in the admin panel through module settings. It is important to note that native Magento does not offer such possibility by default, however, it has this mechanism included. Let’s have a look at a default task catalog_product_index_price_reindex_all in the module Mage_Catalog:
File <magento_root>/app/code/core/Mage/Catalog/etc/config.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<global> ... <crontab> <jobs> <catalog_product_index_price_reindex_all> <schedule> <cron_expr>0 2 * * *</cron_expr> </schedule> <run> <model>catalog/observer::reindexProductPrices</model> </run> </catalog_product_index_price_reindex_all> </jobs> </crontab> ... </global> |
As we can see the cron expression is statically defined through xml. The same implementation is typical for all standard cron jobs in Magento. Let’s check app/code/core/Mage/Cron/Model/Observer.php:
File <magento_root>/app/code/core/Mage/Cron/Model/Observer.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class Mage_Cron_Model_Observer { ... /* line 163 in magento 1.9.0.1 */ protected function _generateJobs($jobs, $exists) { $scheduleAheadFor = Mage::getStoreConfig(self::XML_PATH_SCHEDULE_AHEAD_FOR)*60; $schedule = Mage::getModel('cron/schedule'); foreach ($jobs as $jobCode => $jobConfig) { $cronExpr = null; if ($jobConfig->schedule->config_path) { $cronExpr = Mage::getStoreConfig((string)$jobConfig->schedule->config_path); } if (empty($cronExpr) && $jobConfig->schedule->cron_expr) { $cronExpr = (string)$jobConfig->schedule->cron_expr; } if (!$cronExpr || $cronExpr == 'always') { continue; } $now = time(); $timeAhead = $now + $scheduleAheadFor; $schedule->setJobCode($jobCode) ->setCronExpr($cronExpr) ->setStatus(Mage_Cron_Model_Schedule::STATUS_PENDING); for ($time = $now; $time < $timeAhead; $time += 60) { $ts = strftime('%Y-%m-%d %H:%M:00', $time); if (!empty($exists[$jobCode.'/'.$ts])) { // already scheduled continue; } if (!$schedule->trySchedule($time)) { // time does not match cron expression continue; } $schedule->unsScheduleId()->save(); } } return $this; } ... } |
As we can see before using the node <сrоn_expr>, we first check for the node <соnfig_pаth> and in case it is verified then we attempt to get the value for the cron expression through Mage::getStoreConfig() by using the values of that very node as the path. As you can see it is pretty easy to set a cron task for using some settings in the admin panel. Let’s take a simple example:
In the system.xml file of the module we create a cron expression setting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<config> <sections> <mymodule translate="label" module="mymodule"> <label>My Module</label> <tab>belvg</tab> <frontend_type>text</frontend_type> <sort_order>10</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <groups> <settings translate="label" module="mymodule"> <label>Settings</label> <frontend_type>text</frontend_type> <sort_order>2</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <fields> <cron_expr translate="label" module="mymodule"> <label>Cron Expression</label> <frontend_type>text</frontend_type> <sort_order>10</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </cron_expr> </fields> </settings> </groups> </mymodule> </sections> </config> |
In the config.xml file we register the cron task:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<global> ... <crontab> <jobs> <mymodule_cron_task> <schedule> <config_path>mymodule/settings/cron_expr</config_path> </schedule> <run> <model>catalog/observer::reindexProductPrices</model> </run> </mymodule_cron_task> </jobs> </crontab> ... </global> |
Now cron expression can be set in the admin settings by using standard cron syntax.
Hi! Thanks for the question. Unfortunately, we need more data to be able to help you. Please, provide us with more details or write to our support department: [email protected]. We’ll be happy to assist.
This is not working for me. Can you help me?