DelayedJOb’s (DJ) fancy methods

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, method.to_sy    m, args)  5     end  6   7     def send_at(time, method, *args)  8       Delayed::Job.enqueue(, 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

About sandipransing

Web Developer #ruby #rails #JS
This entry was posted in Ruby on Rails and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s