1

I have a RESTful API built with PHP. In POST request saving to DB is triggered. The problem is that now I have to support long running tasks. For example a user triggers POST request that is going to take a few minutes to be processed and having to wait is not a good option.

From what I've learnt from now: One of the best practices is something like this - user sends POST request with data, API returns status 202 Accepted and sends a location header with an URL where user will obtain the result (once it's ready).

But how can I achieve this if there is no asynchronous/multithreading stuff in PHP? My endpoint will have to return status 202 and trigger a process (start writing to DB).

I don't want to let the user waiting for his POST request until the result is obtained ( so it needs to happen like I've described ).

Anyone has any idea?

6

You need two applications.

Your website simply saves the job to a database or queue.

your worker application picks jobs up from the queue, does them and write the result back to the database.

| improve this answer | |
  • Yes – and you don't have to write it yourself. Some operating systems have built-in "job" and "workload" handling capability. Linux/Unix has a great many open-source systems. (You will find both back-end processing and front-end user interface stuff, already done. Clusters ... networks ... AWS ... it's all "a thing already done." – Mike Robinson Apr 7 at 17:47
  • Might want to elaborate on when async would be used (for example waiting a few seconds) versus waiting a few minutes (queue approach). – Jon Raynor Apr 7 at 17:57
  • hmmm those seconds add up fast and will crash your server under load – Ewan Apr 7 at 18:17
  • Okay, I think I'm going to do something similar. I started researching the topic. One thing that comes up my mind is how my "worker" application will be notified when a file or db record is created with the request, would RabbitMQ or something similar work?Or maybe I can set up my linux server to periodically fire a request to see if something new is inserted and if so, send the data for processing (maybe send POST request with it). Anyway, I still have a lot of research to do, thanks – stubborn Apr 7 at 21:12
  • @stubborn Just write the data somewhere, And from the writer apps point of view, it Is done. What happens to the data than, who picks them up And when or how often, Is no concern of that writer app. The Worker app Will check either periodically or whenever a Worker thread (from Its pool) Is free to process new set of data, or it can actively wait for new data like with rabbitmq. – slepic Apr 9 at 4:32

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.