Launching a long-running process asynchronously

2019-02-25 12:52发布

问题:

In our web application, a user can make a change that requires a lot of database tables to update. The load time for all that can be up to 30 seconds. I don't want the user to wait for that to complete before navigating to another page.

I've put the long-running code on its own page (say, "updateinfo.aspx") and tried a few solutions, including jQuery AJAX calls to "updateinfo.aspx" or loading an image file that calls "updateinfo.aspx". In all cases, I cannot navigate from the original HTML page that kicked off the AJAX call to another HTML page while "updateinfo.aspx" is executing. Chrome says that the request to "updateinfo.aspx" is pending. When I click on a link to navigate away from original HTML page, we're "Waiting for example.org..." until the AJAX page is finished, then the request to navigate to the next HTML page follows through and the new page loads.

So, this defeats the purpose of putting the long-running code into an AJAX page. The user's page renders quickly, but they cannot continue about their day by navigating to another page until the AJAX page is finished. I don't care about the output of the AJAX page.

Any thoughts?

回答1:

You shouldn't really execute a long-running process in a web page context; the HTTP Request/Response model is not favourable to that concept when the client application is a web browser. This is a scenario I have had to address a number of times; you could: -

  1. use MSMQ; submit a message to a queue containing details of the operation to be performed, or
  2. write the details of the operation to be performed to a "Jobs" table

You can then create a Windows Service to read messages from the queue/pull un-processed items one at a time from the table, and perform the long-running operation.

In the most recent project I had to do this, from memory, I created a usercontrol that sat in the header (i.e. in the masterpage) which polled the database table via jQuery Ajax once every 15 seconds to detect when the job was completed, and show a popup to the user indicating the job was finished.

I can try and dig out some examples somewhere but those are the main moving parts, does that help at all ?