0

I have a question about performing a very long and resource intensive task within WordPress. Here is the scenario.

I have a plugin that allows users to run a task that takes a very long time and once completed will return the result back to the user.

The first thing I did was run the task via WordPress AJAX functions. This did not work since the site locked up to that user. Meaning that if the task was started then no other pages could be loaded from that user. I was able to get it to work where other viewers of the site would not be locked out but the original person who started the task was still locked out. Also, this created a dependency since the user was not able to close that window while the AJAX task was running since that would close the connection and stop the task.

The second solution I tried to come up with was to just create a single event via the wp_schedule_single_event() function. I was able to develop this and everything works fine but now the task that is being run via wp-cron is taking a lot longer, like 10 times as long, than the AJAX method. Also, I break up this large task into many smaller tasks and when the first task is run via wp-cron it stales for a while. Then once the first task finally completes it is able to run smoothly again until something happens and it stales out. This process repeats until all the sub-tasks are completed. I am not sure if there is throttling built into the wp-cron function, but I do not see the same speed as I did with the AJAX function.

Overall my main question is how can I create a wp-cron task that runs as fast an AJAX task?

Here a simile question that I asked on Stack Overflow for reference.

https://stackoverflow.com/questions/33508639/is-it-possible-to-perform-a-background-php-server-task-from-an-ajax-call-that-wi/33508941?noredirect=1#comment54802107_33508941

3
  • how are you triggering a request that would cause the cron task to run? wp-cron is really pseudo-cron, a request has to be made after the event is scheduled for the process to start.
    – Milo
    Nov 18, 2015 at 23:45
  • I am using the wp_schedule_single_event function codex.wordpress.org/Function_Reference/wp_schedule_single_event
    – Kyle
    Nov 19, 2015 at 12:29
  • 1
    I understand that, but after the event is scheduled it doesn't just happen on its own, another request has to be made to the server to trigger the event. If that request happens 10 minutes after the scheduled event, it may appear that your process took 10 minutes + the time the process actually took to run.
    – Milo
    Nov 19, 2015 at 16:28

1 Answer 1

0

As @Milo said, WP Cron will only really get triggered when someone requests something from WP, that is visit any WordPress page (I haven't looked up the exact details so I don't know if, for example, an AJAX request would trigger the scheduled event).

You said that you have broken up the main task into smaller tasks - I think that you should take that approach and implement it into the AJAX method. You should break the main task into as many smaller subtasks as possible and chain them. Once the first subtask is completed - send another AJAX request to perform the next subtask. And so on, until everything is done. You don't even have to rely on JavaScript to correctly chain the events - you can just pass the action name for the next-in-line subtask in your AJAX response and instruct your script to just send AJAX requests with action names that it got back from the server, until the server says that everything's completed. This MVC-like approach will save you some time on maintaining your code - JS doesn't have to be aware of the structure of how your subtasks are divided, it will just request data from the server until the server says that everything's done.

This is the best approach when it comes to UX too, because you can continuously display task updates to the user after each subtask response arrives. Updating your user's of the progress in time-intensive tasks is crucial. You can even store the progress/result of your subtasks in the database or cookies so that the user can continue the processing even after accidentally navigating off of the page!

3
  • I want to use this method but I also don't want the task to be dependent on having that page open, meaning that if the user closes out of the page before the AJAX task is completed, then the task will not complete. If I use wp_schedule_single_event instead then I am able to perform the task without any dependency on an open connection to the server from the user's browser.
    – Kyle
    Nov 19, 2015 at 12:31
  • @Kyle Then you should run some checks and log the times for when the event is scheduled, when the task starts and when it ends. My guess is that you will see a huge difference in the time that the task was scheduled and when it starts running. If that's not the case, and the task actually takes that long to complete (vs doing it via AJAX), then it will mean that there's some kind of throttling happening on your server. Unfortunately I haven't ever delved that deep into WP Cron to know if the tasks have some kind of low priority. Nov 19, 2015 at 18:34
  • Worth mentioning - if you've taken a copy of the live DB a while back to use for local then it will have a whole bunch of missed schedules to take care of before you even get onto your task.
    – Bysander
    May 30, 2022 at 13:23

Your Answer

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

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