Our thoughts, knowledge, insights and opinions

Running subprocesses from SBT console

TL;DR

Preparing environment for integration tests is not easy. Usually you need to run one or more external services - database, rabbitmq, web server, etc. What’s more, you have to be sure that they are up and running. After performing the tests you have to be able to turn them off. My plugin makes it possible to start all required applications directly from SBT console with minimal effort.

Check it on Github: https://github.com/whysoserious/sbt-process-runner

or continue reading.

Process definition

You ‘ll have to create your own process definition. Create an object extending ProcessInfo trait. There are two important pieces here:

def processBuilder: ProcessBuilder

defines how process should be started. For that you can use a very convenient scala.sys.process API.

def isStarted: Boolean

Many processes need a lot of time to start. The method above is periodically run by a plugin to check whether the process is up and running. The easiest implementation is just:

override def isStarted: Boolean = true

but you can also check for the existence of a PID file

override def isStarted: Boolean = {
  import java.nio.file._
  Files.exists(Paths.get("/tmp", "webappp"))
}

or test whether a certain port is open:

override def isStarted: Boolean = {
  try {
    new Socket("127.0.0.1", port).getInputStream.close()
    true
  } catch {
    case _: Exception => false
  }
}

You can see more examples in a test-project.

Installation

Check manual on Github

New commands with TAB COMPLETION

For each process you defined you ‘ll have three new commands in SBT console:

  • process-runner:status your-process-id: check status of the process - Idle, Starting or Running.
  • process-runner:start your-process-id: start process
  • process-runner:stop your-process-id: stop process

All commands support Tab completion.

If you have any more questions just contact me.

You like this post? Want to stay updated? Follow us on Twitter or subscribe to our Feed.



comments powered by Disqus