Search code examples
grailsgroovygrails-controller

How to prevent grails server sided code from running if it is already running


I have a question about a grails application. I have a controller that does a lot of work, on the server side, and can take several minutes to complete its task. I'm concerned that if the user hits refresh while the code is running, it will try to run the code again. How would i prevent this. below is some psuedo code

 def refreshDb = {      
    requeryDataBase()
}

  public void requeryDatabase(){

    ....some long process here
  }

so, how would i prevent requeryDataBase() from running if it is already running?

thanks for any help or insight! jason


Solution

  • If you want multiple users to be able to use the controler something like the following could work using the session object.

      public void requeryDatabase(){
    if (session['queryRunning']==false) {
       session['queryRunning']=true
        ....some long process here
    session['queryRunning']=false //set to false so the user can execute the controller again
    }
     else {
       //Put code to notify the user to be pacient here
    }
      }