2015-02-11 108 views
0

錯誤,我有以下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 '[email protected]' -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 

爲了在我的生產服務器上部署該守護程序,我缺少了什麼。

在此先感謝您的幫助。

UPDATE


這裏是我的mongoid.yml文件的路徑:

[email protected]:~/apps/pre.blabloo.com/current$ cd config 
[email protected]:~/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 

回答

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 c配置文件的絕對路徑爲Mongoid.load!方法。要轉換mongoid.yml配置文件的相對路徑,請使用File.expand_path方法,並使用__FILE__作爲dir_string參數。