grailsquartz-schedulergspflash-messagegrails-services

Showing Status Message on the Top of Grails Views whenever Some Service went Down


I am Developing grails application with multiple services and Quartz Jobs. Within Grails Quartz Jobs, I inject some services which make requests to server and perform some operation based upon the result, returned from server.

Now, Sometimes that server goes down due to some reasons and the service which communicates with that server gets connectionException. As all this is happening at back end and user doesn't know about it. I want to show message to user (No matter in which GSP page currently user is when server went down) at the top of the GSP whenever my service encounters that server is Down.

And that message will be disappeared when my service started communicating server (when server is up). As far I know, FLASH can be used here but that persists within single request but I want to show this message until server becomes accessible.

What are the different options for me to achieve this in Grails ? What will be best option ?

Thanks in Advance :)


Solution

  • Create a status service that keeps a volatile stats property, set it to reflect the status when it changes and use a tag library to read the status and include it in your layout/GSPs.

    Here is a very quick example of that

    First the Service:

    // MyStatusService
    package com.example
    
    class MyStatusService {
      boolean isServerDown = false
      ...
    }
    

    Then within your code:

    // From within your code, setting the status
    def myStatusService // assumes you can inject it
    ...
    myStatusService.isServerDown = true // or false depending on your code
    ...
    

    A tag library:

    // MyStatus TagLibrary
    package com.example
    
    class MyStatusTagLib {
      def myStatusService
      static namespace = "myStatus"
    
      def checkStatus = { attrs ->
        if (myStatusService.isServerDown) {
          out << "Server is down message here."
        }
      }
    }
    

    Then finally in your GSP or even your layout:

    <myStatus:checkStatus />
    

    Hope that helps.