I have own custom Rest API's. I would like to make one longer job there which can take a few minutes. It is course too long to wait for api response. I would like to run this job in background, return meantime 200 to the user. When job will finish, will save results to the DB.

How can i achieve this ?

  • Sounds like the REST API would be writing to a job queue where the first-in is first-out. The jobs are worked on automatically according to that order. If the user needs the background data, then it could be e.g. emailed to the user when it's ready with a given url to data. Implementing such a system in detail is probably out of scope here. – birgire Feb 26 at 16:20
  • I mean smth like in java/scala you have, threads, akka etc. you just run process in anotther thread. Async rest apis. – Baku Feb 26 at 18:57
  • only cron job is the solution ? – Baku Feb 27 at 7:47
  • or can be by hooks ? for example I will save_post, which is connected the some hook, hooks fires up after post saved. I am not sure how exactly it works in the background, I am new to WP :) – Baku Feb 27 at 7:51
  • 1
    Cron would most likely work well for periodically crunching through the jobs, then there are solutions like (I've not tested them though) WP-Minions, WP Background Processing Async Background Processing with an example that might be useful to you. – birgire Feb 27 at 8:57

Your Answer

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

Browse other questions tagged or ask your own question.