从foreverb守护进程访问我的模型

时间:2014-10-03 08:11:44

标签: ruby-on-rails ruby

我在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>'

更新


SCRIPT ENVIRONMENT FILE

更新路径


#!/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

现在我得到了这个错误:

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/ruby-1.9.3-p484@global/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/ruby-1.9.3-p484@global/gems/bundler-1.5.2/lib/bundler/runtime.rb:19:in `setup'
    from /Users/jeanosorio/.rvm/gems/ruby-1.9.3-p484@global/gems/bundler-1.5.2/lib/bundler.rb:119:in `setup'
    from /Users/jeanosorio/.rvm/gems/ruby-1.9.3-p484@global/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>'

1 个答案:

答案 0 :(得分:0)

这非常容易加载Rails env只需添加到脚本顶部:

#!/usr/bin/env ruby

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

Rails.root目录中使用这个简单的3行脚本。

Another examples