Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would test cron with Magento?

Tags:

First I need to understand how Magento cron works.

I know how cron works on linux, using crontab -e.
I know I need to set up Magento's cron.php to run periodically

But when I define cron within magento config file, how does they match that they should be run?

Because if set my cron.php to run every 15 min (0,15,30,45 * * * *) and I have a Magento cron <schedule><cron_expr>10 * * * *</cron_expr></schedule> for example.

How will it match/work?

By the way, how could I test that my cron works without waiting a long time?

like image 607
ÉricP Avatar asked Mar 13 '11 16:03

ÉricP


People also ask

How do I test a cron job in Magento 2?

Setup Cron Job from Magento 2 Admin Panel From the left panel, go to Stores > Settings > Configuration. Navigate to Advanced > System and expand the Cron (Scheduled Tasks) section and perform further on the Cron configuration options for group: index and Cron configuration options for group: default sections.

How can I tell if cron job is running Magento?

To check the configured cron jobs you can use the command crontab -l in your terminal and you will see the cron jobs configured and the time they will run.


2 Answers

Magento's cron entry point is the cron.php script in your Magento root. You'll need to setup an OS crontab entry hit this script, which you appear to have done already.

How the cron works

Every time the cron.php script is hit three things happen:

  • Schedule: Magento parses the merged config.xml files for <config><crontab>...</crontab></config> jobs entries, reading their cron_expr elements for detail on how often they should be run. Magento then populates the cron schedule table with jobs that should be executed in the future, along with timestamps for when they should be run. The extent into the future that Magento does this is configurable in the admin.
  • Execute: Magento reads the cron schedule table for jobs that need to be executed this very second and jobs that should have already been executed, i.e. with timestamps in the past, that haven't expired. The expiry limit is also a parameter configurable in the admin.
  • Cleanup: Magento goes through the schedule table deleting jobs that have been completed, or missed (due to the deadline).

Duplicate runs?

You raise an interesting point. What happens if you have a 10 minute cron scheduled in Magento, but cron.php is only guaranteed to be hit every 15 minutes. It appears that Magento would run your Magento jobs twice, in some cases:

  • HH:00 -> HH:50 and HH:00
  • HH:15 -> HH:10
  • HH:30 -> HH:20 and HH:30
  • HH:45 -> HH:40

I've only read the code, and that's what appears to happen. The best way to find out would be, of course, to run it and find out. If you want to avoid the issue, in most cases, then increase the cron.php execution frequency to every 5 minutes, which is what we do.

Testing your code

If you want to test if your cron is actually working without waiting an age, set the cron_expr to execute every minute, or clear out your cron schedule table then hit cron.php twice (once to generate the schedule, and again to run the job).

If you want to just test that your model is working, you could setup a test script (outlined in https://www.nicksays.co.uk/testing-magento-modules), and run that.

Your question prompted a blog post: https://www.nicksays.co.uk/dissecting-the-magento-cron-system :)

like image 128
Nick Avatar answered Sep 22 '22 14:09

Nick


On the second half of your question... how to tell if its running.

Here is something that I did. I added the following code at the end of the cron.php file. This updates a simple log called "cronlog.txt" everytime the cron.php file gets called.

So, if I have doubts about cron running I can just glance at that file and see the last date and time the cron.php file ran.

try {    $myFile = "cronlog.txt";    $fh = fopen($myFile, 'w');    $stringData = date('l jS \of F Y h:i:s A');    fwrite($fh, $stringData);    fclose($fh); } catch (Exception $e) {    Mage::printException($e); } 
like image 34
RThomas Avatar answered Sep 18 '22 14:09

RThomas