构建一个集成了我的rails环境的ruby守护进程

jjm*_*res 8 ruby daemon ruby-on-rails

我需要构建一个ruby守护进程,它将使用freeswitcher eventmachine库进行freeswitch.

因为几天我在网上寻找最佳解决方案来构建一个可以整合我的rails环境的ruby守护进程,特别是我的活跃记录模型.我看看优秀的Ryan Bates截屏视频(第129集自定义守护进程),但我不确定这仍然是一个真正的解决方案.

有没有人知道这样做的好方法?

感谢你的帮助.

Log*_*ter 8

我一直为我的rails环境构建守护进程.守护进程宝石真正完成了它的所有工作.这是从我最新的rails应用程序(script/yourdaemon)中提取的一个小模板,作为示例.我使用eventmachine gem,但想法是一样的:

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

class YourDaemon

  def initialize
  end

  def dostuff
    logger.info "About to do stuff..."
    EventMachine::run {
      # Your code here
    }
  end

  def logger
    @@logger ||= ActiveSupport::BufferedLogger.new("#{RAILS_ROOT}/log/your_daemon.log")
  end
end

dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))

daemon_options = {
  :multiple   => false,
  :dir_mode   => :normal,
  :dir        => File.join(dir, 'tmp', 'pids'),
  :backtrace  => true
}

Daemons.run_proc('your_daemon', daemon_options) do
  if ARGV.include?('--')
    ARGV.slice! 0..ARGV.index('--')
  else
    ARGV.clear
  end

  Dir.chdir dir

  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
  YourDaemon.new.dostuff
end
Run Code Online (Sandbox Code Playgroud)

这给了你所有常用的脚本/你的守护进程[run | start | stop | restart],你可以在" - "之后将参数传递给守护进程.在生产中你将需要使用god或monit来确保守护进程在它死亡时重新启动.玩得开心!