Set Up Scheduled Tasks in Magento

Cron is an important utility which allows you to execute scripts at certain regular intervals. It has become an important aspect for web­-based applications as well. There are lots of ways in which cron is useful to websites, from sending regular newsletter mails to synchronizing the database with third-party systems. You can also use cron to clean up the back­-end storage to improve the overall performance of an application.

Magento supports cron in the core itself, as it does with several other utilities! It allows you to set up scheduled tasks in the module, so that they can run at regular intervals. Magento runs all the cron tasks using the "cron.sh" and "cron.php" files located in the root of the site. So you'll need to make sure that you've set up the system-level cron to run the "cron.sh" file at regular intervals, which eventually triggers the Magento cron system. And finally, Magento gathers all the cron jobs located in the modules, and runs them if needed in that particular cron run.

Although Magento has already provided lots of cron jobs in the core modules itself, you can create a custom cron task in your module as well. And creating a custom module is exactly what we'll be talking about in the upcoming sections.

A Glance at the File Setup

We'll create a simple custom module named "Customcron". Here's the list of files required for the desired setup:

  • app/etc/modules/Envato_All.xml: It's a file which is used to enable our custom module.
  • app/code/local/Envato/Customcron/etc/config.xml: It's a module configuration file in which we'll declare the custom cron job.
  • app/code/local/Envato/Customcron/Model/Customcron.php: It's a model file in which we'll define the cron job logic.

Custom Module: Set Up the Files and Folders

First, we need to create a module enabler file. Create a file "app/etc/modules/Envato_All.xml" and paste the following contents in that file. We've used "Envato" as our module namespace and "Customcron" as our module name. It'll enable our "Customcron" module by default.

Next, we need to create a module configuration file. Create "app/code/local/Envato/Customcron/etc/config.xml" and paste the following contents in that file.

The "config.xml" file looks fairly simple—it declares the version number and model classes as per the Magento conventions. However, the important tag for us is <crontab>, which is used to declare all the jobs. It's one of the "event observers" which is used by Magento to gather all the cron jobs in the modules.

Further, under the <jobs> tag, we've declared our custom crontab job using the <custom_cron_task> tag. It's a sort of unique identifier for the cron job. Although in the above file we've only created a single task, you can set up multiple cron jobs under the <jobs> tag. Next, under <custom_cron_task> we've defined <schedule> and <run> tags.

The <schedule> tag defines cron intervals inside the <cron_expr> tag at which the job will run regularly. In our case, the custom cron task will run every five minutes. But wait, what will it do every five minutes? That's exactly what the <run> tag stands for! It declares the "Model method" which will be called by Magento during the custom cron job run.

Next, we'll create a model "Cronjob.php" file. Create "app/code/local/Envato/Customcron/Model/Customcron.php" with the following contents.

So as we declared earlier, we've defined the "customcrontask" model method. In this method, we're simply sending an email using the Magento email class utility. But more importantly, this method will be called regularly, at every cron job run, of course every five minutes.

And finally, you should make sure that you've created a cronjob entry in your system. For Linux, you simply need to add the following line to your crontab file.

You just need to replace "/path/to/magento/site" with the actual path of the Magento installation. And for Windows, you can do the same by using scheduled tasks. However, in Windows, you need to use the "/path/to/magento/site/cron.php" file, as "cron.sh" is not supported.

So it's really straightforward to plug your custom cron jobs into the Magento cron system! That's it for today, and I hope you've learned something useful in Magento. Share your thoughts using the feed below!

Tags:

Comments

Related Articles