Rails 3:仅在服务器启动时执行初始化程序

编程入门 行业动态 更新时间:2024-10-27 10:31:05
本文介绍了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...

blog.daviddollar/2011/05/06/introducing-foreman.html

更多推荐

Rails 3:仅在服务器启动时执行初始化程序

本文发布于:2023-08-07 14:24:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319610.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   启动时   服务器   程序   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!