Module: Resque::Plugins::Later::Method::ClassMethods

Defined in:
lib/resque/plugins/later/method.rb

Instance Method Summary collapse

Instance Method Details

#later(method_name, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/resque/plugins/later/method.rb', line 5

def later(method_name, opts={})
  alias_method "now_#{method_name}", method_name
  return unless PerformLater.config.enabled?
  
  define_method "#{method_name}" do |*args|
    loner          = opts.fetch(:loner, false)
    queue          = opts.fetch(:queue, :generic)
    klass          = PerformLater::Workers::ActiveRecord::Worker
    klass          = PerformLater::Workers::ActiveRecord::LoneWorker if loner
    args           = PerformLater::ArgsParser.args_to_resque(args)
    digest         = PerformLater::PayloadHelper.get_digest(klass, method_name, args)

    if loner
      return "AR EXISTS!" unless Resque.redis.get(digest).blank?
      Resque.redis.set(digest, 'EXISTS')
    end
    
    Resque::Job.create(queue, klass, send(:class).name, send(:id), "now_#{method_name}", *args)
  end
end