3

I have a grails-application which uses ajax on several edges.

I trigger large calculations (client -> controller -> service -> controller -> client) via ajax and sometimes call abort() on all current running requests. (eg. if the user changed some parameters)

The problem now is that when I call abort() on my ajax-requests, these requests really stop on client side (perfect!), but the server keeps calculating those aborted requests - it does not know that the user aborted and further processing is useless.

I searched for a way/plugin etc. to tell the server "hey, stop this request, it's not needed anymore", but didn't find a solution.

Any ideas on how I might tackle this or how others have dealt with this?

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
Martin L.
  • 3,006
  • 6
  • 36
  • 60

2 Answers2

0

I doubt there is more simple way than sending another ajax request to the server and have the controller "stop" the previews processing in service for example.

Check this question: How to cancel ajax request that has run (on server side)

Community
  • 1
  • 1
Dopele
  • 547
  • 3
  • 16
  • 1
    Hm, I already thought so. In my case, sending an additional request would be no problem. But how to tell grails/tomcat then to cancel the previous requests from this client? – Martin L. Jul 09 '12 at 10:47
0

As others have said, there isn't much you can do to get the server to stop processing a request. The only place you can exert some control would be in your service that is handling the long-running calculation.

One solution that comes to mind would be for the calculation service to check a flag of some sort that tells it to abort, and if it finds it to be set, then it simply returns right away or returns some small junk value, since the client isn't listening anymore anyways.

Since services are transaction-safe by default (I think), you would have to be careful to set the flag in a transaction-independent way, which means that the Session and the Database (assuming you are using a transactional relational database) are off limits, since your service won't see changes made by a subsequent "stop" request. Instead, something like Redis might be ideal, since it's an external, non-transactional, very fast key-value data store.

As an example "architecture", you essentially would have a "stop" action that sets a redis key for that particular user or session telling the calculation to stop. Then, you have your calculation service check that key as it runs, and stops processing if it finds it set.

There are other (perhaps better) architectures that would let you do this sort of thing, but given your current system, I think this would be one of the simplest.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173