Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle long AJAX requests - send a response but continue to work

We are sending in instruction sets from a browser into a multi-tier web app using jquery-ajax. The first component (component A, written in C#) validates and persists the instructions into a database, and then calls the second component (component B, written in Java and also called via HTTP) which operates on the instructions after retrieving them from the same database. The browser client just polls the rdbms table via component A after submitting the job, so it is effectively disconnected after sending in the request and doesn't wait for a response from component A.

What is the best way for the middle-tier (component A) to return a success message to the client acknowledging a successful submission of the task, but then still make the request to the task handler (component B) and releasing all of its resources? Returning a response is a final action for the page so we'd have to do something in another thread before sending this back to the browser.

Another option we have considered is for this to happen in component B where the task handler sends back an immediate response to the middle tier acknowledging the request, but then continues on working in the background. The only difference would be where we spawn the additional thread to do the work.

Any good ideas on how to handle this?

like image 700
Graham Avatar asked Nov 15 '25 19:11

Graham


1 Answers

What we have done in situations like this is:

  • Browser send instruction set to component A
  • Component A validates instruction set
  • Component A save instruction set to DB under new "tracking" ID STATUS=PENDING
  • Component make request to component B to do the work
  • "Threading" is in component B:
    • Component B starts background thread to do the work, first thing background thread does is update status of "tracking ID" to STATUS=RUNNING
    • Component B main thread returns to success to component A
  • Component A gets "started successfully" message from component B, so returns its own "started successfully" message back to the browser along with the "tracking ID". At this stage component A is done with the request, all component A resources are free.
  • Browser can make different call to component A to check status of "tracking ID" in the DB
  • Meanwhile the background "thread" of component B is still doing the work, possibly recording progress against the "tracking ID"
  • Component B completes work, updates status of "tracking ID" in DB, STATUS=OK or STATUS=ERROR and gives up all resources.

The advantage of all that is there is no waiting for the work to finish from you application's point of view. The actual work is done by a "background" thread, just as if you kicked it off with nohup dothework & in a shell. The key is using the DB to monitor the status of the "tracking ID".

like image 67
Sodved Avatar answered Nov 17 '25 12:11

Sodved



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!