问题
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