Rails 3: Execute initializer only for server start

家住魔仙堡 提交于 2019-12-12 18:07:54

问题


I'm working on a Rails 3.1 application that needs to start up a couple of background processes when the application server starts: (1) A filesystem monitor process using FSSM to monitor files in a certain directory, and (2) a Resque "worker" process that executes jobs from a Resque queue.

In my initial attempt at this, I have done this by spawning the processes in an initializer in the config/initializers directory. This works fine in the sense that the processes are correctly started and stopped together with "rails server". However, the problem is that these initializers are also executed whenever I run "rails console" or any Rake tasks. Is there any way to have the initializers run only as part of a "rails server" command, and not run during "rails console" or Rake tasks?

Alternatively, is there a better approach to starting these background processes that does not use the initializers mechanism? I'd still like to be able to start up everything together with a single command.

Thanks!


回答1:


There are lots of different ways you can start background tasks and processes. You should use more established methods...

  • Monit
  • Upstart
  • init.d scripts

However if you want to streamline the process I suggest you use Foreman...

http://blog.daviddollar.org/2011/05/06/introducing-foreman.html



来源:https://stackoverflow.com/questions/7811555/rails-3-execute-initializer-only-for-server-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!