Magento Cron System

In this blog post we will see in detail how magento cron system works.

Basics

Magento Cron System is used to run tasks on recurring basis as with any cron based system. “cron.php” located in magento base folder is the main entry for magento cron system. To start magento cron you need to put “http://domainname.com/cron.php” in your web server cron job and set it to run every 15min.

There are various default cron job setup already in magento, so its recommend that you setup cron in magento. The default cron jobs in magento are related to log cleaning, newsletter, product stock notifications, currency updates.

Adding Cron To Your Custom Module

To add cron job to your custom module add below xml to the config.xml

  <default>
    <crontab>
        <jobs>
            <job_unique_name>
                <schedule>
                    <cron_expr>0 2 * * *</cron_expr>
                </schedule>
                <run>
                    <model>test/observer::cronMethod</model>
                </run>
            </job_unique_name>
        </jobs>
    </crontab>
  </default>

“job_unique_name” can be any unique name which you want to give.
“cron_expr” can take any valid cron expression
“test/observer” should be a valid model in your module
“cronMethod” is a function inside that model

TIP: You can also specify “always” in cron_expr, to execute cron job always.

Magento also uses a table “cron_schedule” where is stores the status of all cron jobs. Details stored are job_code, scheduled_at, executed_at, finished_at, status. This is also a useful table which you can use debug your cron jobs.

There are various settings options for cron in admin which you can configure located at “Sytem -> Configuration -> Advanced -> System -> Cron”. These settings are

Generate Schedules Every: After how many minutes should we generate a new cron entry in database
Schedule Ahead for: How many minutes ahead should the cron entry be done in database.
Missed if Not Run Within: Mark status as missed if cron has not run with these many minutes
History Cleanup Every: Delete cron entry after how many minutes
Success History Lifetime: After how many minutes to delete a cron entry with status success
Failure History Lifetime: After how many minutes to delete a cron entry with status failure

TIP: Magento has a very good design relating to cron by having a single entry point for various cron functions. The major drawback for this is, if any of the cron jobs have an error all other cron jobs will also not execute since all are executed serially.

Magento Cron Job In Detail

Now lets dig into magento source code and see how cron is implemented.

Files to Refer
1. “cron.php”
2. “Mage_Cron_Model_Observer”
3. “app/code/core/Mage/Cron/etc/config.xml”

The main entry point from cron is “cron.php”
There we see

Mage::getConfig()->init()->loadEventObservers('crontab');
Mage::app()->addEventArea('crontab');
Mage::dispatchEvent('always');
Mage::dispatchEvent('default');

So magento first loads all event observers in the crontab area, and then dispatches “always” and “default” event.
Both these events are defined in “app/code/core/Mage/Cron/etc/config.xml” file

<crontab>
        <events>
            <default>
                <observers>
                    <cron_observer>
                        <class>cron/observer</class>
                        <method>dispatch</method>
                    </cron_observer>
                </observers>
            </default>
            <always>
                <observers>
                    <cron_observer>
                        <class>cron/observer</class>
                        <method>dispatchAlways</method>
                    </cron_observer>
                </observers>
            </always>
        </events>
    </crontab>

Lets look at the “dispatch()” function “Mage_Cron_Model_Observer” class. As we can see here it reads jobs from
‘crontab/jobs’, ‘default/crontab/jobs’ xpaths and processes these jobs.