2012-02-17 15:54:11 -08:00
|
|
|
require 'sidekiq/extensions/generic_proxy'
|
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
module Extensions
|
|
|
|
##
|
2012-08-16 16:43:01 -07:00
|
|
|
# Adds a 'delay' method to ActiveRecords to offload instance method
|
2012-02-17 15:54:11 -08:00
|
|
|
# execution to Sidekiq. Examples:
|
|
|
|
#
|
|
|
|
# User.recent_signups.each { |user| user.delay.mark_as_awesome }
|
2012-08-16 16:43:01 -07:00
|
|
|
#
|
|
|
|
# Please note, this is not recommended as this will serialize the entire
|
|
|
|
# object to Redis. Your Sidekiq jobs should pass IDs, not entire instances.
|
|
|
|
# This is here for backwards compatibility with Delayed::Job only.
|
2012-02-17 15:54:11 -08:00
|
|
|
class DelayedModel
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
def perform(yml)
|
|
|
|
(target, method_name, args) = YAML.load(yml)
|
|
|
|
target.send(method_name, *args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
def delay
|
|
|
|
Proxy.new(DelayedModel, self)
|
|
|
|
end
|
2012-05-25 20:21:42 -07:00
|
|
|
def delay_for(interval)
|
2012-05-25 20:28:48 -07:00
|
|
|
Proxy.new(DelayedModel, self, Time.now.to_f + interval.to_f)
|
2012-05-25 20:21:42 -07:00
|
|
|
end
|
2012-02-17 15:54:11 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2012-03-10 12:30:15 -08:00
|
|
|
end
|