用多个dynos玩2 Heroku启动 [英] Play 2 Heroku startup with multiple dynos

查看:88
本文介绍了用多个dynos玩2 Heroku启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在启动时,会触发一个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进程时,您正在运行两个进程。您有几个选项:


  • 使用不同的进程(也称为单例进程)运行全局进程。关于Play的好处是你可以有多个 GlobalSettings 实现。当你开始你的过程时,你可以用 -Dapplication.global = YourSecondGlobal 来指定你要使用的全局。然后,在您的proc文件中,您将拥有 singleton:target / start -Dhttp.port = $ {PORT} $ {JAVA_OPTS} -Dapplication.global = YourSecondGlobal 。启动您的Web进程并 singleton 进程并确保 singleton 被缩放为1。

  • 使用分布式信号获取锁。每个进程然后将争夺锁定 - 获胜的进程将继续进行,其他进程将失败。如果您使用Postgres(与Heroku上的许多人一样),可以使用顾问锁是一个不错的选择。


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 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.

这篇关于用多个dynos玩2 Heroku启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆