Module: Resque::Plugins::Async::Method::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#async_method(method_name, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/resque/plugins/async/method.rb', line 10

def async_method method_name, options = {}
  
  puts "options => #{options}"
  alias_method :"sync_#{method_name}", method_name # Allow tests to call sync_ methods ...

  return if Rails.env.test? # ... but don't actually make them asynchronous

  define_method method_name do |*args|
    raise NotPersistedError, "Methods can only be async'ed on persisted records (currently: #{inspect})" unless self.persisted?

    my_klass              = Resque::Plugins::Async::Worker
    my_klass.queue        = options[:queue] || __send__(:class).name.underscore.pluralize.to_sym
    my_klass.loner        = options[:loner] || false
    my_klass.lock_timeout = options[:lock_timeout] || 0
    my_klass.loner = true if my_klass.lock_timeout > 0 # Magick config

    Resque.enqueue(
      my_klass,
      __send__(:class).name,
      __send__(:id),
      "sync_#{method_name}".to_sym,
      *args
    )
  end
end