How to run Lagom in development mode and in the background

(, en)

To start a Lagom or Play project in development mode for testing, you start everything with sbt runAll. You can stop everything by hitting Enter (or any other key).

Now you might think: cool, Lagom is starting everything for me, so why not use the same approach in my CI pipeline to run integration tests in a clean environment. Well, good luck!

One way or another, no matter what, the server is stopped immediately after startup. This happens

If you trace down the issue in the Lagom source, you’ll end up in ConsoleHelper.scala, which uses System.in.read() to wait for “Enter”:

def blockUntilExit() = {
  // blocks until user presses enter
  System.in.read()
}

This piece of code also triggers on other input, even if you use /dev/null as standard input. In case of /dev/null, read() returns an end of file and will not block.

Luckily there is a solution for it. To figure out what to do, you have to return to the basics. The question is: “how to deal with System.in.read()” and not “how to run Lagom in the background”. For the first question StackOverflow knows the answer.

Put into action, you have to create a bash script, e.g. run_in_bg.sh with the following contents:

#!/usr/bin/env bash

(while true; do sleep 10000; done) | sbt runAll >lagom.log &

This will allow you to run Lagom (or Play) in the background.