Search code examples
scalaakkadaemonsystemdjsvc

Running akka as a systemd job


I would like to run an Akka application that runs and restarts automatically on a system with systemd when the machine boots. I have been trying to do this with apache daemon/jscv, but I'm running in to some problems. right now I'm just trying to run the thing (step one), but that's already failing. I the following code

object Launcher extends Daemon {

  val system = ActorSystem("testsystem")
  var schedule: Cancellable = new Cancellable{
    def cancel(): Unit = ()
    def isCancelled: Boolean = true
  }

  def roll(rand: Random) = () => {
    rand.nextInt(6)
  }

  val selection = system.actorSelection("mytestactor")


  def init(context: DaemonContext) = {
    val ref = system.actorOf(Props[TestActor], "mytestactor")
  }

  def start = {
    import scala.concurrent.ExecutionContext.Implicits.global
    val jrand = new Random()
    val roller = roll(jrand)
    schedule = system.scheduler.schedule(Duration(0, SECONDS), Duration(2, SECONDS))( () => selection ! roller() )
  }



  def stop = {
    if (!schedule.isCancelled) {
      schedule.cancel()
    }
  }

  def destroy = {
    system.shutdown()
  }

}

class TestActor extends Actor {

  def receive = {
    case 0 => println("rolled zero")
    case n: Int if n < 6 => println("rolling")
  }
}

I tried to package that up with sbt assembly and then tried to launch the thing with

when I try to launch that with

sudo jsvc -outfile ~/out.txt -errfile ~/err.txt -cp commons-daemon.jar:akkadaemon.jar Launcher

but doesn't seem to be working; err.txt contains

java.lang.ClassNotFoundException: Launcher
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.apache.commons.daemon.support.DaemonLoader.load(DaemonLoader.java:151)
Cannot load daemon
Service exit with a return value of 3

What should I do to get this approach to work? Is this at least a viable direction (for when I want to write the systemd script once this gets to work), or am I looking at it all wrong?


Solution

  • To use jsvc, use the fully qualified class name, and use a normal class, not an object. The launcher invocation will than look like

    sudo jsvc -outfile ~/out.txt -errfile ~/err.txt -cp commons-daemon.jar:akkadaemon.jar Fully.Qualified.Name.Of.Launcher