简体   繁体   中英

Returning HTTP response without waiting for the process to be completed

I am planning to create a rest API that receives a HTTP request from UI. this rest api needs to trigger a task which may takes more than 1 hour and we don't want UI waiting for this response. if the request is reached to server, we just want to send the response back with status=ok and let the task on server side goes on. once the task is completed, it will update DB with status=success. when fails, will update DB with status=fail. so that a user can check the status of task on UI that pulled from this table later.

I am thinking of adding the request into DB table and check that table with cron scheduler(1 min interval) to see if there is any request in and process. However, if there is other better solution, I would like to try. Please, let me know if there is other options(some hints). I will google further!

Thanks!

From the REST API standpoint you have the option of asynchronous calls with polling, or pushing updates to the UI.

For Polling these are the steps:

  1. Client sends HTTP request and gets an HTTP 202 response plus a location URL for checking statuses.

  2. Client can continue sending requests to the check status URL which will return 202 as long as the result is pending.

  3. Once the work is done, a client request to the check status URL will then return HTTP 302 along with the URL of the final resource for the client to access.

Here's more info on the asynchronous REST API pattern with polling: https://docs.microsoft.com/en-us/azure/architecture/patterns/async-request-reply

If you prefer pushing results to the client then you can look into WebSockets as was recommended by another user. WebSockets has obvious advantages but would require support for the websockets framework in both UI and Server (it's a fairly well established framework so this shouldn't be a problem). Polling on the other hand would just be plain old HTTP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM