Question

I am trying to automate a task with Heroku Scheduler. I tested the task in local and it works. I created a temporary route for testing, and it works everytime I hit the route:

get 'create_expired_credit_lines' to: 'credits#create_expired_lines'

credits.controller.rb

class CreditsController < ApplicationController
...

 def create_expired_lines
 ...do something...
 end

I then created a file

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

And when heroku executes the scheduler.rake I get the following error:

2014-03-20T20:33:58.972574+00:00 app[scheduler.9175]: undefined method `create_expired_lines' for #<Class:0x000000030dee10>

This is the full log

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
Was it helpful?

Solution

create_expired_lines is an instance method and you are calling it as a class method. Hence, the error.

You can either call it as

Credit.new.create_expired_lines

or

declare it as a class method

def self.create_expired_lines
 ...do something...
 end

and call it as Credit.create_expired_lines

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top