Posted on

Cloud Backup Case study: Detecting if Magento cron is running in your extensions

We released the latest version of our Magento Cloud Backup extension, for sending a full site and database backup offsite to Amazon’s cloud storage service S3. You can read about the latest release over on the World Wide Access site. In this post I wanted to quickly cover the method I used for detecting if cron is running on a Magento store. This was one of the new features added to help new users identify possible problems.

I think it’s important for many extensions that rely on cron to check if it is running, because otherwise it’s possible for users to have a false sense of security (particularly around automated backups). This technique I mentioned briefly back in October at the Magento Developers Paradise. This more fully worked example will hopefully be useful to others.

The logic is this: If we have no pending jobs, or no successful jobs in the Magento cron table, then cron is probably not running (either it never has, or only has once).

$schedules_pending = Mage::getModel('cron/schedule')->getCollection()
        ->addFieldToFilter('status', Mage_Cron_Model_Schedule::STATUS_PENDING)
$schedules_complete = Mage::getModel('cron/schedule')->getCollection()
        ->addFieldToFilter('status', Mage_Cron_Model_Schedule::STATUS_SUCCESS)
 if (sizeof($schedules_pending) == 0 || 
        sizeof($schedules_complete) == 0) {
                // cron probbaly isn't running

With this code run from within a frontend model, or a controller, you can effectively inform your users that cron is probably not running. It would be quite a nice service actually that just automatically calls cron for Magento stores via the web.

It would also be possible to look at when the last successful job completed, if it was days ago then it may also be a clue that cron has been running, but has since stopped. If anyone has an alternative approach, please let me know I’d like to hear it.

7 thoughts on “Cloud Backup Case study: Detecting if Magento cron is running in your extensions

  1. I just look in the DB. It’s all there in one table. But would be nice to wrap this in an admin module and if it hasn’t been running, you can click a “run now” button to go and manually trigger it asap.

    Cron is one of those principally simple things which has a knack of going wrong silently, with grave consequences. Often, things start faltering cumulatively, like with stock control etc.

    Any tools for early diagnosis are helpful. I sense a new Ashley-module coming… 🙂

  2. Hey J.T! I think instead of a module, I should add some sort of cron-calling functionality to – so you can request a call to /cron.php every 5,10,15,30,60 minutes for each store. Then you can get a daily summary if it misses any – though you would not get to see errors if they happen when it is called.

    It can be a ‘premium’ subscription feature! (Along with uptime and response time monitoring from 4 data centres globally).
    What do you think?

  3. really nice way to check it. Should maybe check that your specific task is running by filtering by the string of your job_code. Like in cron_schedule table “export_apply_all” job_code have at least one “status” set as “success”.

    It’s like an egocentric cronjob checker :p

  4. I just tried to install the Magento Cloud Backup extension and Magento Connect Manager returned an error – “community/Aschroder_CloudBackup: Version for ‘Aschroder_CloudBackup’ was not detected” ????

    Any thoughts?

  5. Is there a way to send this to a S3 bucket in my aws account. Currently, it is sending to a bucket which is not controlled by us. So technically, I cannot physically view the contents of the bucket from my aws control panel.

  6. Hi, SBR
    At the moment Amazon keeps the Devpay S3 buckets separate/isolated from the user’s own buckets – I’m not 100% sure why they do that, probably security. In any case what it means is we cannot access your personal buckets, and you cannot access the backup buckets. It causes a lot of confusion so I will add a note to the admin area in the next release, but until Amazon changes the way their S3 service works, we can’t do much about it I’m afraid. Hope you’re still keen to try it out despite that limitation.

  7. Hey Ash, Thanks for the heads up. I was playing around with this for a while on my test store and now moving it to a full production environment. You should probably get a signup via the PureHerbalCure store sometime today. 🙂

Comments are closed.