How to Set Expressions for Separate Cron Tasks Via Admin Settings in Magento

July 2, 2014 Andrei Danilchyk Magento
How to Set Expressions for Separate Cron Tasks Via Admin Settings in Magento

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:

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:

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:

In the config.xml file we register the cron task:

Now cron expression can be set in the admin settings by using standard cron syntax.



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *