生产中的守护程序 - 部署时出错

时间:2015-02-11 09:34:19

标签: ruby-on-rails linux ruby-on-rails-3 mongodb mongoid

我有以下dameon:

#!/usr/bin/ruby
require 'rubygems' unless defined?(Gem)
require 'forever'
require 'mongoid'

ENV["RAILS_ENV"] ||= "production"
require File.expand_path("../../../config/environment", __FILE__)
Mongoid.load!("../../config/mongoid.yml")


Forever.run do
  every 1.minutes do
    @booking = Booking.where(booking_available: false, :created_at.lt => DateTime.now.to_datetime.in_time_zone("Madrid") - 10.minutes).to_a
    @booking.each do |book|
      if book.Ds_Response.nil?
        book.booking_available = true
        book.save
        puts "update #{book.id}"
      end
    end
    puts "#{@booking.count}"
  end
end

这项工作非常适合我的开发环境,但当我尝试在我的服务器上部署此守护程序时,我收到以下错误:

executing "cd /home/web/apps/pre.blabloo.com/current && RACK_ENV=pre bundle exec ruby script/user/booking_release.rb start
/home/web/apps/pre.blabloo.com/shared/bundle/ruby/1.9.1/gems/mongoid-2.8.1/lib/mongoid/config/environment.rb:39:in `initialize': No such file or directory - ../../config/mongoid.yml (Errno::ENOENT)
*** [err :: 0.0.0.0] from /home/web/apps/pre.blabloo.com/shared/bundle/ruby/1.9.1/gems/mongoid-2.8.1/lib/mongoid/config/environment.rb:39:in `new'
*** [err :: 0.0.0.0] from /home/web/apps/pre.blabloo.com/shared/bundle/ruby/1.9.1/gems/mongoid-2.8.1/lib/mongoid/config/environment.rb:39:in `load_yaml'
*** [err :: 0.0.0.0] from /home/web/apps/pre.blabloo.com/shared/bundle/ruby/1.9.1/gems/mongoid-2.8.1/lib/mongoid/config.rb:125:in `load!'
*** [err :: 0.0.0.0] from /home/web/apps/pre.blabloo.com/shared/bundle/ruby/1.9.1/gems/mongoid-2.8.1/lib/mongoid.rb:148:in `load!'
*** [err :: 0.0.0.0] from script/user/booking_release.rb:8:in `<main>'
    command finished in 13495ms
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell '1.9.3@blabloo' -c 'cd /home/web/apps/pre.blabloo.com/current && RACK_ENV=pre bundle exec ruby script/user/booking_release.rb start'" on 0.0.0.0

我在生产服务器上部署此守护程序时缺少的是什么。

提前感谢您的帮助。

更新


这是我的mongoid.yml文件的路径:

web@app0:~/apps/pre.blabloo.com/current$ cd config
web@app0:~/apps/pre.blabloo.com/current/config$ ls
application.rb  database.yml  deploy_tasks    environments  initializers  mongoid.yml   routes.rb
boot.rb     deploy.rb     environment.rb  facebook.yml  locales   newrelic.yml  unicorn.rb

它上面的守护进程

blabloo/script/user/booking_release.rb

1 个答案:

答案 0 :(得分:0)

主要问题是配置文件mongoid.yml是用相对路径指定的。

如果查看Mongoid.load!方法的实现,您将看到该文件最终由File.new方法打开。 File.new方法尝试使用进程的当前工作目录来查找配置文件。

示例:

/Users/xxxx/Workspace/sandbox_projects中的文件树:

tmp_20150211
  |-- config.rb
  |-- root
  |    |-- tree
  |    |    |-- leaf.rb

档案leaf.rb

# Returns the actual absolute path of the config.rb file
File.expand_path('../../../config.rb', __FILE__)

# Returns the path that is used to locate the config.rb file
File.expand_path('../../config.rb')
File.exists?('../../config.rb')

当我在leaf.rb目录(tmp_20150211)中运行$ ruby root/tree/leaf.rb文件时,无法找到config.rb文件:

File.expand_path('../../../config.rb', __FILE__)  #=> /Users/xxxx/Workspace/sandbox_projects/tmp_20150211/config.rb

File.expand_path('../../config.rb')               #=> /Users/xxxx/Workspace/config.rb
File.exists?('../../config.rb')                   #=> false

但是,当我在leaf.rb目录(tmp_20150211/root/tree)中运行$ ruby leaf.rb文件时,可以找到config.rb文件:

File.expand_path('../../../config.rb', __FILE__)  #=> /Users/xxxx/Workspace/sandbox_projects/tmp_20150211/config.rb

File.expand_path('../../config.rb')               #=> /Users/xxxx/Workspace/sandbox_projects/tmp_20150211/config.rb
File.exists?('../../config.rb')                   #=> true

<强>结论

要解决您的问题,请指定mongoid.yml配置文件,其中包含Mongoid.load!方法的绝对路径。要隐藏mongoid.yml配置文件的相对路径,请使用File.expand_path方法,并使用__FILE__作为dir_string参数。