2015-02-11 106 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参数。