2014-10-03 48 views
0

我在foreverb守護進程上訪問我的可用性模型時有以下代碼。但我總是得到以下錯誤:從foreverb守護進程訪問我的模型

uninitialized constant Availability 

這裏是我的守護:

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

Forever.run do 

    every 10.seconds do 
    @classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a 
    puts @classes.count 
    end 

end 

感謝您的幫助。

注:我正在使用rails3和mongoid。

更新的代碼


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

require File.expand_path('../config/environment', __FILE__) 

Forever.run do 

    every 10.seconds do 
     @classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a 
     puts @classes.count 
    end 

end 

現在我發現了以下錯誤:

用戶/ classes_notification.rb; tail -f -n 150 user/sample.log;

/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require': cannot load such file -- /Users/jeanosorio/repos/blabloo/script/user/config/environment (LoadError) 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require' 
    from user/classes_notification.rb:6:in `<main>' 

UPDATE


SCRIPT ENVIRONMENT FILE

UPDATE PATH


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

# Load Rails 
#ENV['RAILS_ENV'] = ARGV[0] || 'development' 

require File.expand_path('../../../config/environment', __FILE__) 

Forever.run do 

    every 10.seconds do 
     @classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a 
     puts @classes.count 
    end 

end 

現在我得到這個eror:

script/user/classes_notification.rb; tail -f -n 150 script/user/sample.log; 

WARN: Unresolved specs during Gem::Specification.reset: 
     thor (>= 0.15.0) 
     tzinfo (~> 0.3.22) 
WARN: Clearing out unresolved specs. 
Please report a bug if this causes problems. 
/Users/jeanosorio/.rvm/gems/[email protected]/gems/bundler-1.5.2/lib/bundler/runtime.rb:34:in `block in setup': You have already activated multi_json 1.10.1, but your Gemfile requires multi_json 1.8.2. Prepending `bundle exec` to your command may solve this. (Gem::LoadError) 
    from /Users/jeanosorio/.rvm/gems/[email protected]/gems/bundler-1.5.2/lib/bundler/runtime.rb:19:in `setup' 
    from /Users/jeanosorio/.rvm/gems/[email protected]/gems/bundler-1.5.2/lib/bundler.rb:119:in `setup' 
    from /Users/jeanosorio/.rvm/gems/[email protected]/gems/bundler-1.5.2/lib/bundler/setup.rb:7:in `<top (required)>' 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:135:in `require' 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:135:in `rescue in require' 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:144:in `require' 
    from /Users/jeanosorio/repos/blabloo/config/boot.rb:6:in `<top (required)>' 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require' 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require' 
    from /Users/jeanosorio/repos/blabloo/config/application.rb:1:in `<top (required)>' 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require' 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require' 
    from /Users/jeanosorio/repos/blabloo/config/environment.rb:2:in `<top (required)>' 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require' 
    from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require' 
    from script/user/classes_notification.rb:9:in `<main>' 

回答

0

這是很容易負載Rails ENV只需添加到您的腳本的頂部:

#!/usr/bin/env ruby 

require File.expand_path('../config/environment', __FILE__) 

Rails.root DIR工作這麼簡單的3行腳本。

Another examples

+0

感謝,更新我的代碼,但現在我得到另一個錯誤,你能幫我請 – Jean 2014-10-03 08:45:46

+0

@Jean您的'Rails'應用程序?看起來像'/用戶/ jeanosorio/repos/blabloo /腳本/用戶/'沒有這樣的Rails應用程序。 – 2014-10-03 08:49:00

+0

我只是上傳我的路徑 – Jean 2014-10-03 08:56:40