自定义Daemon与Rails 3 [英] Custom Daemon with Rails 3

查看:91
本文介绍了自定义Daemon与Rails 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义守护程序来加载Rails环境。
我的环境如下:
ruby​​-1.9.2-p180
rails 3.0.5

I'm trying to create a custom daemon that loads up the Rails environment. My environment is as follows: ruby-1.9.2-p180 rails 3.0.5

我做了以下事情:

-安装了daemons gem

-Installed the daemons gem

-安装了daemon_generator插件,位于:
https://github.com/dougal/daemon_generator

-Installed daemon_generator plugin found here: https://github.com/dougal/daemon_generator

-已生成守护程序:rails生成守护程序侦听器

-Generated a daemon: rails generate daemon listener

所有这些工作正常。

但是,一旦我尝试访问活动记录对象(如尝试检索用户),它就会崩溃。

However, as soon as I try to access an active record object like trying to retrieve a user, it blows up.

*** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***
#<NameError: method `recognize' not defined in Rack::Mount::RouteSet>
*** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***
#<NoMemoryError: failed to allocate memory>
#<SystemStackError: stack level too deep>
#<fatal: exception reentered>
#<NoMethodError: undefined method `eq' for nil:NilClass>
#<NameError: method `recognize' not defined in Rack::Mount::RouteSet>

关于如何创建一个加载Rails 3.0.5的守护进程的想法?

Any thoughts on how to create a Daemon that loads up Rails 3.0.5?

推荐答案

我更喜欢滚动自己的rails守护程序控制器。这是一个适用于大多数情况的简单示例:

I prefer to roll my own rails daemon controllers. Here is a simple example that works for most cases:

脚本/守护程序

#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'

ENV["APP_ROOT"] ||= File.expand_path("#{File.dirname(__FILE__)}/..")
ENV["RAILS_ENV_PATH"] ||= "#{ENV["APP_ROOT"]}/config/environment.rb"

script = "#{ENV["APP_ROOT"]}/daemons/#{ARGV[1]}"

Daemons.run(script, dir_mode: :normal, dir: "#{ENV["APP_ROOT"]}/tmp/pids") 

daemons / your_daemon_script.rb

daemons/your_daemon_script.rb

require ENV["RAILS_ENV_PATH"]
loop { 
  ... your code ...
}

您可以使用以下命令来控制恶魔:

You can control your deamons by using the following commands:

script/daemon run your_daemon_script.rb
script/daemon start your_daemon_script.rb
script/daemon stop your_daemon_script.rb

这使我可以轻松添加新的守护程序,并且可以轻松加载必要时在每个脚本中添加rails。

This enables me to easily add new daemons and I can easily load rails in each script if necessary.

这篇关于自定义Daemon与Rails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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