Class: MicroQ::Worker::Standard

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/micro_q/worker/standard.rb

Overview

The default worker implementation. this worker can call any method on an class instance and pass an arbitrary argument list. By default it calls the ‘class’.constantize#‘perform’ method. It returns the result of the method call if possible (for debugging).

The middleware chain can stop this message from executing by not yielding to the given block.

A minimal message: (Calls the perform method with zero arguments) { :class => ‘MyWorker’ }

A more complex message: (Calls the update_data with a single parameter as a list of ids) { :class => ‘MyUpdater’, ‘method’ => ‘update_data’, :args => [[2, 6,74, 198]]}

Instance Method Summary collapse

Constructor Details

#initialize(manager) ⇒ Standard

Returns a new instance of Standard.



21
22
23
# File 'lib/micro_q/worker/standard.rb', line 21

def initialize(manager)
  @manager = manager
end

Instance Method Details

#fetch_klass(message) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/micro_q/worker/standard.rb', line 40

def fetch_klass(message)
  klass = MicroQ::Util.constantize(message['class'].to_s)

  loader = message['loader'] ||= { 'method' => 'new' }
  klass = klass.send(loader['method'], *loader['args']) if loader['method']

  klass
end

#perform(message) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/micro_q/worker/standard.rb', line 25

def perform(message)
  klass = fetch_klass(message)

  method = message['method'] || 'perform'
  args = message['args']

  defer do
    MicroQ.middleware.server.call(klass, message) do
      klass.send(method, *args)
    end
  end

  @manager.work_done!(current_actor)
end