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, opts = {}) ⇒ Object



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

def async_method(method_name, opts={})
  # Allow tests to call sync_ methods ...
  alias_method :"sync_#{method_name}", method_name

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

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

    my_klass       = Resque::Plugins::Async::Worker
    my_klass.queue = opts[:queue] ||
                     send(:class).name.underscore.pluralize

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