photo credit: What time is it? via photopin (license)

Solving ‘Too late for the schedule’ with Magento crons

Tags: ,

You’ve probably all encountered the ‘Too late for the schedule’-error in Magento at some point in your career as a Magento Developer: A cron is not working, and when you check the cron_schedule  table in your database, you see numerous records with this message.
This error occurs when the Magento cron is trying to run a job that should have already ran a while ago. This can have a couple of reasons (with their accompanied solutions) which I will outline in this article.

Reason 1: UTC timezone

Timezones can always be a bit of a headache when it comes to scheduled tasks and server locations. Magento stores it’s cron tasks in UTC, but checks the locale of your (admin) store to see if the time has already passed. Therefore it is advised to set the time of your store to UTC, but the time of your separate websites/store views respectively to their locale.

Reason 2: The ‘missed if not run within’-parameter

If you want to know how Magento determines if a task is ‘too late for the schedule’ take a look at Mage_Cron_Model_Observer::_processJob() . This is the part of the code that is responsible for the message:

The self::XML_PATH_SCHEDULE_LIFETIME -parameter is the ‘missed if not run within’-value which is set in the configuration in ‘System > Config > Advanced > System > Cron’:

Screen Shot 2015-09-15 at 08.33.06
The code itself is pretty self-explanatory:

  • It takes the time of the scheduled tasks.
  • If the time of the scheduled task is in the future it does nothing.
  • If the time of the scheduled task is in the past (=older than the current timestamp minus the ‘missed if not run within’-value) it throws the error.
  • Otherwise it just runs the task as required.

Effectively, when the ‘missed if not run within’-parameter is set to 15 minutes, this means that if the scheduled task is older than now – 15 minutes, it will not run, but instead throw this error. Needless to say, when this value is lower than the time your cron runs (for example: if your cron runs every 5 minutes, but this value is set to 2 minutes), this will obviously throw this error.
You probably didn’t do this, but it is good to know this inner mechanic of how Magento handles crons. Especially the part that Magento checks the schedule prior before running any tasks. And in the next reason I’ll explain to you why:

Reason #3: External plugins

This is the part where I was pulling my hairs. My timezone was correct, my configuration was correct, but my cron schedule was still showing the ‘too late for the schedule’-message. So what was happening?
On further inspection, I noticed that there were some tasks from external plugins who where starting (the executed_at -column in the database revealed this), but were never finishing (the finished_at -column stayed NULL  for these tasks). So something was preventing these tasks from being finished (probably causing a crash in PHP or something) and therefore stopping the entire chain of tasks!
The next logical thing that happened of course, was that after 3 tries (15 minutes later), the time for all the scheduled tasks that where further on in the chain all became too late. Too late for the schedule to be exactly. Up until the task that was causing the problems itself also became too late.
So how did I solve this?
Well, in my case I had to debug and fix the tasks of 2 external plugins that were throwing notices and warnings. For some reason even a notice or a warning (so not just an exception) caused my cron to fail silently. Perhaps the fact that I was on HHVM instead of PHP had something to do with this, but I’m not sure (yet).

In conclusion

I hope that this article, and especially the findings in reason #3 helped someone who stumbled on this page when he was encountering the same problem. Magento sure has some quirky ways sometimes to get things done, but in the end, when you know the pitfalls and where too look when something goes wrong, it’s actually quite workable 😉

Visitors give this article an average rating of 3.8 out of 5.

How would you rate this article?

4 thoughts on “Solving ‘Too late for the schedule’ with Magento crons”

  1. fabien says:

    Great article.
    I had the same problem and after searching for hours, digging in Apache/PHP logs, it was indeed a module that set PHP’s memory_limit to a low value throwing an error of Allowed memory size too small.

  2. Thank you for me changing Admin local Time to GMT Saved me !

  3. Alex says:

    Thanks for sharing this post, i was looking for help in fixing cron job for weekly email setup, https://www.cloudways.com/blog/setup-magento-cron-job/, this post also helped me to configure it.

Leave a Reply