Search code examples
rubyruby-on-rails-3classmodeldelayed-job

Adding some custom methods to delayed job in rails 3?


I want to add some custom methods to the delayed jobs model.

I want to have some extra such as...

def status
  return "errored" unless self.last_error.blank?
  return "waiting" if self.locked_at.blank?
  return "running" unless self.locked_at.blank?
  return "blerg" if some.other.things...
end

...on the the delayed job class.

I was just wondering how I extend it in such a way.

Thanks!


Solution

  • Is the class Delayed::Job? You can add methods to classes in a few ways in ruby, but probably the easiest way is:

    config/initializers/delayed_job.rb

    class Delayed::Job
      def status
        return "errored" unless self.last_error.blank?
        return "waiting" if self.locked_at.blank?
        return "running" unless self.locked_at.blank?
        return "blerg" if some.other.things...
      end
    end
    

    Although this might look like it overwrites the class, it actually just adds the method if the class is already loaded..