用多个dynos玩2 Heroku启动 [英] Play 2 Heroku startup with multiple dynos
问题描述
在启动时,会触发一个Akka actor,它自己安排未来的工作(例如发送推送通知)。
object Global扩展GlobalSettings {
覆盖def onStart(app:应用程序){
val actor = Akka.system.actorOf(Props [SomeActor])
Akka.system.scheduler.scheduleOnce(0 seconds,actor,None)
}
}
这可以正常工作,但我很想知道如果打开数字网络dynos。
将使用两个Web dynos执行两次onStart?
如果全球在全球范围内真正起作用,并且 onStart 仅执行一次, DYNOS。如果不是这样,多个dynos必须以某种方式同意负责做这项工作的一个dyno。
有没有人遇到类似的问题?
如果你运行两个web dynos,你的全局将被执行两次。全球化在这个过程中是全球化的扩展Web进程时,您正在运行两个进程。您有几个选项:
GlobalSettings
实现。当你开始你的过程时,你可以用 -Dapplication.global = YourSecondGlobal
来指定你要使用的全局。然后,在您的proc文件中,您将拥有 singleton:target / start -Dhttp.port = $ {PORT} $ {JAVA_OPTS} -Dapplication.global = YourSecondGlobal
。启动您的Web进程并 singleton
被缩放为1。
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?
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 havesingleton: target/start -Dhttp.port=${PORT} ${JAVA_OPTS} -Dapplication.global=YourSecondGlobal
. Start your web processes andsingleton
process and make suresingleton
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.
这篇关于用多个dynos玩2 Heroku启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!