kctang
kctang

Reputation: 11202

Gradle task to do processing when external process is "ready"

I would like my gradle task to:

  1. Execute a command (external process to "start my server").
  2. Wait for a certain output from my external process say "Server Is Ready" in the stdout.
  3. Do something in my gradle task (basically to talk to my external task).
  4. When I am done doing my stuff, end the external process by launching another "stop my server" command.
  5. When both commands complete, end the task.

This is like starting a server to run integration tests, but this is not a tomcat/jetty type server, so it needs to be launch from command line and wait for server to be "ready" via the server's stdout.

I am currently able to use the exec task to run my task and get the output. So I think I can probably code in Groovy/Java to achieve what I want.

However, it seems tedious to code this seemingly common pattern. So I am wondering if there's an easier way to solve such problems without too much coding - i.e. is this a common pattern that gradle has a DSL for?

Note that I am a newbie at Gradle, so any advise around this topic would also be appreciated.

Upvotes: 12

Views: 2165

Answers (2)

Shorn
Shorn

Reputation: 21446

The overall pattern: probably not - too many different possibilities/combinations for there to be any kind of standard template.

But it's simple to build your own logic with a set of tasks. The most complicated part is the waiting for the external process, which Gradle does have a DSL for (Ant actually has the core logic/DSL, Gradle is just wrapping it).

This is my task to wait for Postgres to come up inside a local docker container:

task waitForLocalDb(){
  group = 'docker.db'
  mustRunAfter localApiSvcDbRunCleanContainer
  doFirst{
    println "waiting for Postgres to come up"
    ant.waitfor(
      maxwait:"10", maxwaitunit:"second", 
      checkevery:"500", checkeveryunit: "millisecond"
    ){
      socket(server:"192.168.99.100", port:"5544")
    }
  }
}

This waits for Postgres by checking for it to be listening on the specified port.

You can use the Ant waitfor task to do various things - like check for the server to say "server is ready" in a file somewhere ("in stdout" would very much depend on how you were launching your process).

Or you could check for "server is ready" or something like that in a specific URL, see https://stackoverflow.com/a/2548226/924597

Upvotes: 1

Nikita Skvortsov
Nikita Skvortsov

Reputation: 4923

As far as I know, current version of Gradle (1.4) has no dedicated DSL support for starting up/shutting down external processes. You can implement it in groovy right inside build script, it should not be too difficult.

Upvotes: 1

Related Questions