Play 2 Heroku startup with multiple dynos

heroku, playframework-2.0

Solution

If you run two web dynos, your global will be executed twice. Global is global to the process. When you scale your web process, you are running two processes. You have a couple options:

- Use a different process (aka a singleton process) to run your global. The nice thing about Play is that you can have multiple `GlobalSettings` implementations. When you start your process, you specify the global you want to use with `-Dapplication.global=YourSecondGlobal`. In your procfile, then, you would have `singleton: target/start -Dhttp.port=${PORT} ${JAVA_OPTS} -Dapplication.global=YourSecondGlobal`. Start your web processes and `singleton` process and make sure `singleton` is scaled to 1.

- Use a distributed semaphor to obtain a lock. Each process will then race to obtain a lock -- the one that wins will proceed and the others will fail. If you're using Postgres (as many people do on Heroku), an advisory lock is a good choice.

Problem

I have a Play 2.x app up and running on Heroku with a single web dyno. On startup, an Akka actor is triggered which itself schedules future jobs (e.g. sending push notifications). ``` object Global extends GlobalSettings { override def onStart(app:Application) { val actor = Akka.system.actorOf(Props[SomeActor]) Akka.system.scheduler.scheduleOnce(0 seconds, actor, None) } } ``` This works fine with one web dyno but I am curious to know what happens if I turn up the number of web dynos. Will onStart be executed twice with two web dynos? Would be great if Global really works globally and onStart is only executed once, independently of the number of web dynos. If not, multiple dynos have to somehow agree on one dyno responsible for doing the job. Did anybody run into a similar issue?

Original source