Bartosz Bilicki
Bartosz Bilicki

Reputation: 13235

How to wait until web application is up with Gradle?

I use Gradle to trigger deployment of my web app to dev environment. Deployment is done asynchronously- deploy task returns at once, but it takes some time until new application starts.

I would like to wait in my Gradle script until new version of application is up and healthly.

I use Spring Boot 1.3.5 and I have standard monitoring endpoints /info and /health http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

/info returns

{
  "app" : {
    "name" : "app name",
    "branch" : "origin/develop",
    "id" : "153",
    "tag" : "build info",
    "version" : "1.0-SNAPSHOT"
  }
}

I am interested in waiting until /info' returns specificid` - id of new version ,not old one that still may be running.

Upvotes: 1

Views: 1986

Answers (1)

Stanislav
Stanislav

Reputation: 28106

Since Gradle let you use a Groovy within the script, you are free to make your build waiting for some condition, after the task's main job is executed. All you need is to provide a doLast closure, where you have to check the health-status response within the loop with some sleep time.

That will look like:

//assume, this is your deploy task
task deploy {
    doLast {
        def id = %make a http-request and parse response for id%
        while(id!=%specificid%) {
            //sleep for 5 seconds
            sleep(5*1000)
            //one more http-request and parsing
            id = %make a http-request and parse response for id%
        }
    }
}

doLast is executed after the task job is done and it will not let next task execute until it's running.

You can add any conditions you need, for example to prevent the infinite loop or something else. Sure, that is not a ready solution, but you can use it to build your own. All you need is to make http-request and parse response (httpbuilder lib may help you with that).

I used almost the same logic once to check whether is remote service up and running, but with the ssh-session, not http-requests, and it was working fine.

Upvotes: 1

Related Questions