我正在尝试使用Heroku Scheduler自动执行任务。我在本地测试了这个任务并且它有效。我创建了一个临时路线进行测试,每当我点击路线时它都会起作用:
get 'create_expired_credit_lines' to: 'credits#create_expired_lines'
credits.controller.rb
class CreditsController < ApplicationController
...
def create_expired_lines
...do something...
end
然后我创建了一个文件
app/lib/tasks/scheduler.rake
desc "This task is called by the Heroku scheduler add-on"
task :create_expired_credit_lines => :environment do
puts "Creating expired credit lines..."
Credit.create_expired_lines
puts "done."
end
当heroku执行scheduler.rake时,我收到以下错误:
2014-03-20T20:33:58.972574+00:00 app[scheduler.9175]: undefined method `create_expired_lines' for #<Class:0x000000030dee10>
这是完整日志
2014-03-20T20:33:49.798874+00:00 heroku[api]: Starting process with command `bundle exec rake create_expired_credit_lines` by scheduler@addons.heroku.com
2014-03-20T20:33:53.823769+00:00 heroku[scheduler.9175]: Starting process with command `bundle exec rake create_expired_credit_lines`
2014-03-20T20:33:54.477084+00:00 heroku[scheduler.9175]: State changed from starting to up
2014-03-20T20:33:57.189982+00:00 app[scheduler.9175]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7)
2014-03-20T20:33:57.190348+00:00 app[scheduler.9175]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7)
2014-03-20T20:33:58.193568+00:00 app[scheduler.9175]: Connecting to database specified by DATABASE_URL
2014-03-20T20:33:58.527454+00:00 app[scheduler.9175]: [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
2014-03-20T20:33:58.889846+00:00 app[scheduler.9175]: Creating expired credit lines...
2014-03-20T20:33:58.893837+00:00 app[scheduler.9175]: rake aborted!
2014-03-20T20:33:58.974452+00:00 app[scheduler.9175]: /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/dynamic_matchers.rb:55:in `method_missing'
2014-03-20T20:33:58.974452+00:00 app[scheduler.9175]: /app/lib/tasks/scheduler.rake:4:in `block in <top (required)>'
2014-03-20T20:33:58.972574+00:00 app[scheduler.9175]: undefined method `create_expired_lines' for #<Class:0x000000030dee10>
2014-03-20T20:33:58.974452+00:00 app[scheduler.9175]: Tasks: TOP => create_expired_credit_lines
2014-03-20T20:33:58.974452+00:00 app[scheduler.9175]: (See full trace by running task with --trace)
2014-03-20T20:34:00.664220+00:00 heroku[scheduler.9175]: Process exited with status 1
2014-03-20T20:34:01.988621+00:00 heroku[scheduler.9175]: State changed from up to complete
答案 0 :(得分:1)
create_expired_lines
是一个实例方法,您将其称为类方法。因此,错误。
您可以将其称为
Credit.new.create_expired_lines
或
将其声明为类方法
def self.create_expired_lines
...do something...
end
并将其称为Credit.create_expired_lines