Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating view with async operation in MVC

I'm writing a small internal web app. I need to do a very long operation (which can take about an hour) at the press of a button and let the user know once the operation completes with its result.

In other words, I need the button to start the async operation and refresh the view with the results once it is finished (after about an hour run).

Edit: The operation could use some sort of a refresh mechanism in the View. The result can be sent to the view after a small lag (doesn't have to refresh realtime)

like image 513
Omri Avatar asked Mar 26 '14 12:03

Omri


1 Answers

You could use SignalR, but that might be a little bit overkill for this. Another option is to set up another controller action which checks if the task has been completed. Then, on the client side, you could use jQuery to make ajax requests to that controller action. When the action comes back as complete you can show an alert or otherwise update the page.

$.ajax({
    type: 'GET',
    url: http://mysite.info/tasks/checkComplete/5,
    success: function (response) {
        if (response == 'true') {
            alert('Task complete');
        }         
    }
});

As for what happens on the server side, I don't think this is a case where I would use async/await. If your task is really going to be running for an hour I have a feeling you're going to run into timeout issues, etc. I would have a controller action which is used to start the task, but all it does it put the request to start in the database. I would then have an external "worker" which checks for requests in that database and performs the tasks. Once the task is complete it would update that database entry to mark it as complete. Then the "CheckComplete" controller action from my example above could check the database to see if the task is in fact completed.

like image 104
jdehlin Avatar answered Nov 02 '22 23:11

jdehlin