Best practice to check that a service (e.g. a database) is started?

When you have some IntegrationTests that required is started before, you need to create a task that creates another thread.

What is the easiest way to detect when the service is started?

In the sample below I wait ‘startupBlockMillis’ millis. But waiting is fragile and sucks:)

Is it useful to great another thread that runs a request against the above service until a valid response is identified. How do you solve such issues?

Regards, Leif

Here is a sample to detail what I mean:

ext.springBootModuleHttpPort = 8000
ext.springBootModuleStartupBlockMillis = 5000
ext.springBootModuleJarPath = jar.archivePath
  task startSpringBootModule(type: SpringBootModuleStarter) {
  group = 'spring-boot-module'
  description = 'Starting spring boot module in a separate process.'
    httpPort = springBootModuleHttpPort
  startupBlockMillis = springBootModuleStartupBlockMillis
  jarPath = springBootModuleJarPath
    task stopSpringBootModule {
  group = 'spring-boot-module'
  description = 'Shutting down spring boot module'
    doLast {
    "curl -X POST http://localhost:${springBootModuleHttpPort}/shutdown".execute()
  class SpringBootModuleStarter extends DefaultTask {
  Integer httpPort
  Integer startupBlockMillis
  String jarPath
  void start() {
    Thread.start {
      "java -jar ${jarPath} --server.port=${httpPort}".execute()
      // this is what I want to get rid off

I don’t think there is a best practice. You’ll have to use whatever the service provides to determine when it’s ready. For example, there might be a way to start up the service that blocks until the service is ready. Or you could poll until a HTTP request succeeds.

Hi Peter.

What do you mean by “start up the service that blocks until the service is ready” like this should work:

startServiceInForkedProcess checkIfStarted.dependsOn startServiceInForkedProcess integrationTest.dependsOn checkIfStarted integrationTest.finalizedBy stopService

checkIfStarted could be one “isStarted” call with a huge execution timeout or a poll as you mentioned.

Thank you, Leif

What do you mean by “start up the service that blocks until the service is ready”

For example, the service might come with a shell script or API that blocks until the service is ready. like this should work:

I’d probably combine ‘startServiceInForkedProcess’ and ‘checkIfStarted’ into one and the same task.