Rails 3:仅在服务器启动时执行初始化程序 [英] Rails 3: Execute initializer only for server start

查看:50
本文介绍了Rails 3:仅在服务器启动时执行初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Rails 3.1 应用程序,它需要在应用程序服务器启动时启动几个后台进程:(1) 使用 FSSM 监视某个目录中的文件的文件系统监视进程,以及 (2)从 Resque 队列执行作业的 Resque worker"进程.

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.

在我最初的尝试中,我通过在 config/initializers 目录中的初始化程序中生成进程来完成此操作.这在进程与rails server"一起正确启动和停止的意义上工作正常.然而,问题是这些初始化程序也会在我运行rails 控制台"或任何 Rake 任务时执行.有什么方法可以让初始化程序仅作为rails 服务器"命令的一部分运行,而不是在rails 控制台"或 Rake 任务期间运行?

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.

谢谢!

推荐答案

您可以通过多种不同的方式启动后台任务和进程.你应该使用更成熟的方法...

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

  • 监控
  • 新贵
  • init.d 脚本

但是,如果您想简化流程,我建议您使用 Foreman...

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

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

这篇关于Rails 3:仅在服务器启动时执行初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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