0

I have a really slow-running action (30-45 seconds) that loops over each user in my DB and syncs the data with some off-site database. I want this to happen once or twice daily, so I have a WP-Cron job setup to trigger the action.

My problem is that the action isn't essential for rendering the page, so ideally it's something that would run in the background instead of blocking the page rendering.

Is there any built-in system in WordPress that'll do this kind of thing?

One really hacky solution I thought of was to trigger the action via AJAX and just let it run in the background as the user visits the page. It doesn't seem like a good option, so I wanted to hear if you guys have any inputs or thoughts about a cleaner way to achieve this.

  • 1
    There's no official threading system for PHP, a cron job would be what you want, but the solution would ideally be a job system, where syncing each user is a task. You'll need server level access though as this won't run on a shared host, and it'll timeout if you trigger it from a browser request – Tom J Nowell Apr 20 '17 at 14:42
  • If your server supports "real" Cron jobs you can do it that way without interfering with WP. – WebElaine Apr 20 '17 at 14:56
0

Techcrunch has released a library to spawn an asynchronous task within WordPress.

https://github.com/techcrunch/wp-async-task

Essentially, you can take any process that is triggered by an action hook, and you can defer the processing on that hook to run asynchronously. You extend the class to define which action you are triggering and a couple of functions to prepare_data to pass, and the action to perform.

  • Perfect! This is exactly what I needed. – Ahrengot Apr 20 '17 at 16:10
  • link only answers are not useful answers. Answers should at least give enough details as to what is on the other end of the link to let people decide it is not what they are looking for. – Mark Kaplun Apr 20 '17 at 16:31
  • Fair point. I've updated to provide a few more details on the class, but without knowing the details of what @Ahrengot is trying to accomplish, I can't provide sample code. – joetek Apr 20 '17 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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