Cron jobs taking too long

Recommended Posts

Magento 2.2.7

Ess M2ePro 1.3.5

I've been noticing that my cron jobs on Magento are taking a long time to complete. I run the job using an HTTP get request to https://<server>/pub/cron.php every minute.

See the screenshot for timings for the M2ePro module.

My store only has 80 items listed and only perhaps 10-20 sales per day, so can't see why it should be taking so long. Anyone seeing anything similar before I dive into a deep debug.



Screenshot (23).png

Share this post

Link to post
Share on other sites

OK, tracked this down eventually, and it is by design.

The M2ePro cron module tries to be clever and applies a random delay to the start of the cron jobs in an attempt to distribute load on a server.

It only does this load distribution under certain conditions.

If you are in developer mode, it does not distribute load.

If the php max_execution_time setting is under 300 seconds it does not distribute load.

To distribute load, each cron job is delayed by a random delay anywhere between 0 and 60 seconds.

This is pretty poor in my opinion. What happens if the cron job is deleyed by 59 seconds. It will most likely still be running when the next cron initiation is attempted resulting in skipped crons.

Time for a module override me thinks to get rid of this pesky load distribution. This should really be a setting that we can turn on/off.


Share this post

Link to post
Share on other sites

Hi Graham

You will avoid this random delay caused by load balancing if you switch to m2e cron service. Their service manages all of the calls following a strict schedule. We actually did not have to do anything to enable it, I believe it was activated automatically once we obtained the license. 


Share this post

Link to post
Share on other sites

Yes I noticed that the m2e cron servers were constantly prodding my server to trigger the m2e cron job every minute. I've currently got them banned by my firewall to stop them from triggering the cron. If you allow the m2epro cron service to trigger your cron jobs, are they triggering only the cron jobs for m2epro, or are they triggering all Magento cron jobs? i.e do you still need a method of triggering the rest of Magento's crons. What url do they use to trigger? Looking at the m2epro code though, if your php max execution time is >= 300s and the cron is triggered by the front end and not the cli/crontab (because max execution time = 0 for cli/crontab), then no matter the method of triggering (and I would imagine also included in this is the m2epro cron service) then the random delay of 0-60 seconds is added to every cron execution.

I'd still much rather trigger the cron jobs myself though. My system is in the Google cloud, and I use the Cloud Scheduler to ping off a request to /cron.php through my front end load balancer, so a random frontend will be chosen to run the cron every minute. That works really well for me.

I've dropped my php max execution time to 299 seconds for the cron.php script and that gets rid of the random delay, so I'm happy. (although max execution time should really be a lot lower than that for the cron!)

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.