Delayed Job provide send_later and send_at as instance methods along-with handle_asynchronously as class method
1 module Delayed 2 module MessageSending 3 def send_later(method, *args) 4 Delayed::Job.enqueue Delayed::PerformableMethod.new(self, method.to_sy m, args) 5 end 6 7 def send_at(time, method, *args) 8 Delayed::Job.enqueue(Delayed::PerformableMethod.new(self, method.to_sy m, args), 0, time) 9 end 10 11 module ClassMethods 12 def handle_asynchronously(method) 13 aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1 14 with_method, without_method = "#{aliased_method}_with_send_later#{pu nctuation}", "#{aliased_method}_without_send_later#{punctuation}" 15 define_method(with_method) do |*args| 16 send_later(without_method, *args) 17 end 18 alias_method_chain method, :send_later 19 end 20 end</code><strong>Usage of send_later, send_at and handle_asynchronously</strong> <code>@user.send_later(:deliver_welcome)Notifier.send_later(:deliver_welcome, user)Notifier.send_at(15.minutes.from_now, :deliver_welcome, user)#Inside User classhandle_asynchronously :deliver_welcome