2

Does anybody have any experience with Magento module level cron jobs across multiple instances of the application?

I know Magento adds the cron records to the cron_schedule table - would each instance of the Magento application add it's own call to the module level cron?

All application instances are looking at the same database.

Thanks for any insights.

dbcn
  • 641
  • 1
  • 9
  • 20

1 Answers1

3

If you have several copies of Magento with a common database then you have a multi-site install. Only add a cron job for one server, there is no need to have crons for any other.

Magento does not call the cron for you, you still need to edit the server's crontab. From the Magento Wiki:

To execute all these configured tasks, the cron.php file located in the Magento root will need to be run periodically, for example every 15 minutes. Basically, this script will check if it needs to run any tasks, and if it needs to schedule any future tasks.

In UNIX/BSD/linux systems you will need to add this line (or a similar line) to your crontab:

 # for debugging purposes only:
 MAILTO=your.user@your.server.com

 */5 * * * *  /bin/sh /absolute/path/to/magento/cron.sh

 # /absolute/path/to/bin/php - replace with path to your PHP5 CLI executable
 # example: /usr/local/php5/bin/php-cli

 # in order to find what is the PHP CLI executable you can try to the following command in shell:
 # which php

 # /absolute/path/to/magento/cron.php - replace with path to Magento installation
 # example: /home/user/public_html/magento/cron.php
clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • This is in fact the set up we ended up using. The multiple application instances are all using the same db, so we're just running the system cron on a single admin box that populates magento's cron_schedule table. – dbcn Mar 04 '11 at 21:57
  • what if one of the two instances is only for failover? If the primary server goes down, than no cronjobs will be executed at all if they are not configured on the secondary server. What's the best solution to handle this case? @clockworkgeek – bpoiss Jun 01 '18 at 08:58
  • @bpoiss There is no definite rule I know of. Perhaps set the cron on one server to run at 10,30,50 minutes and the other at 0,20,40 minutes. That way neither will run at the same time as the other but there's always a backup. – clockworkgeek Jun 02 '18 at 10:18
  • @clockworkgeek would it be a problem if the cronjobs run every minute on both servers? I am testing this for a week on a staging system and haven't seen any problems for now. – bpoiss Jun 04 '18 at 06:30
  • I wouldn't recommend it as that's a race condition. Imagine there is a job to send newsletters (actually there is every 5 minutes) and both servers start at _exactly_ the same time, they'll both mark the job as running then both send thousands of emails out to the same list of recipients, effectively spamming those people. It's rare because that's how race conditions work but it's unwanted and hard to predict, so don't tempt fate. – clockworkgeek Jun 05 '18 at 10:00