1

This is a question about WordPress architecture, that I wasn't able to fully understand by reading WordPress documentation.

I have a scheduled event that can take more than 1 minute to finish because it's a heavy stored procedure that create a few caches on my database.

As far as I know, scheduled events in WordPress (wp_schedule_event) are triggered only when a request is made on the website. So, I imagine that if I schedule this event to run every day at 3:00 AM (which is the less crowded hour of my website) it'll trigger after 3:00 AM on the first request and it can happen a few minutes after this time if nobody is visiting it. Am I right?

Still, if I'm right, let's say a user opens my website at 3:01 AM and it triggers my scheduled event... will this user have to wait 1 minute until it ends so the website is loaded? Or this scheduled event happens in a thread?

1 Answer 1

1

Yes, WP Cron won't run if nobody visits your site. You can also run into the PHP execution time limits

There are ways to mitigate this however:

  • Manually ping the cron URLs via a real cron job using curl
  • Run cron via WP CLI on a real cron job, letting cron jobs run arbitrarily
  • Use a job manager plugin such as cavalcade to manage cron tasks
3
  • Thanks a lot, Tom! Your answer opened a lot of ways to help us out here! Only one thing wasn't clear for me... the first user to access the website after 3 AM will have to wait until the job finishes or does WP execute the event in a thread? Nov 6, 2017 at 19:11
  • 1
    Cron events are thrown in the background then users of the site don't need to wait.
    – mmm
    Nov 6, 2017 at 20:29
  • 1
    there are no threads in PHP, only additional non-blocking requests, WP Cron is called in a non-blocking request right at the very end of a page load
    – Tom J Nowell
    Nov 6, 2017 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.