1

In order to start delayed_job's on a schedule you need to have helper classes with a perform method that delayed_job can call. These need to be defined before any of the classes that use them to create scheduled delayed_jobs are called. All very short, and many of them in my case. For example:

class AccountUpdateJob < Struct.new(:account_id)
  def perform
    acct = Account.find(account_id)
    acct.api_update
  end
end   

I'm doing this in a file called "dj_helper_classes" in the initializers folder. Is that the right thing to do?

Rourke
  • 107
  • 1
  • 9

1 Answers1

0

I keep mine in lib/jobs, one file per class. So, your example would be in lib/jobs/account_update_job.rb

module Jobs
    class AccountUpdateJob < Struct.new(:account_id)
        def perform
          acct = Account.find(account_id)
          acct.api_update
        end
    end
end   
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
cailinanne
  • 8,332
  • 5
  • 41
  • 49
  • How do you make sure they all get loaded in time for use? – Rourke Jun 04 '11 at 05:30
  • By default Rails3 does not autoload everything in the lib directory. You can make it do so by adding the following to application.rb `config.autoload_paths += %W(#{config.root}/lib)` `config.autoload_paths += Dir["#{config.root}/lib/**/"]` – cailinanne Jun 04 '11 at 17:37
  • It's probably worth noting that `application.rb` already contains the relevant lib autoload line, it's just commented out by default. – coreyward Jun 04 '11 at 17:39
  • Here's some more discussion on autoloading the lib directory: http://stackoverflow.com/questions/3356742/best-way-to-load-module-class-from-lib-folder-in-rails-3 – cailinanne Jun 04 '11 at 17:40