Search code examples
ruby-on-railsrubyruby-on-rails-3delayed-job

rails - Delayed jobs without perform method


After make a big class with many method, I want all of these method be called in a delayed jobs.

But the practice of the Delayed::job, is that you have to create a class with a perform method, like that :

class Me < Struct.new(:something)
   def perform
     puts "GO"
   end
end

and call it like :

 Delayed::Job.enqueue Me.new(1)

But the problem is that my class as already many method like this type

class NameHandler
  def self.init
    ap "TODO : Delayed jobs "
  end

  def self.action_one
    ...
  end

  def self.action_two
    ...
  end

etc.

end

and I want to call it like :

 Delayed::Job.enqueue NameHandler.action_one params...

Theres is an best practice for that ? Or I have to follow the classic Delayed::job way and lose many times ?


Solution

  • In the README it has a number of ways:

    Me.new.delay.action_one
    

    or

    class NameHandler
      handle_asynchronously :action_one  
    
      def action_one      
      end
    
      def self.action_one
        new.action_one
      end
    end
    
    NameHandler.action_one