Class: Msgr::Pool::Worker

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Logging
Defined in:
lib/msgr/pool.rb

Overview

Worker actor capsuling worker logic and dispatching tasks to custom runner object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #log_name

Constructor Details

#initialize(pool, index, runner_klass, runner_args) ⇒ Worker

Returns a new instance of Worker.



137
138
139
140
141
142
143
144
# File 'lib/msgr/pool.rb', line 137

def initialize(pool, index, runner_klass, runner_args)
  @pool     = pool
  @poolname = pool.to_s
  @index    = index
  @runner   = runner_klass.new *runner_args

  log(:debug) { 'Worker ready.' }
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



135
136
137
# File 'lib/msgr/pool.rb', line 135

def index
  @index
end

#poolObject (readonly)

Returns the value of attribute pool.



135
136
137
# File 'lib/msgr/pool.rb', line 135

def pool
  @pool
end

#runnerObject (readonly)

Returns the value of attribute runner.



135
136
137
# File 'lib/msgr/pool.rb', line 135

def runner
  @runner
end

Instance Method Details

#dispatch(method, args) ⇒ Object

Dispatch given method and argument to custom runner. Arguments are used to call #send on runner instance.



149
150
151
152
153
154
155
156
157
158
# File 'lib/msgr/pool.rb', line 149

def dispatch(method, args)
  log(:debug) { "Dispatch to runner: #{runner.class.name}##{method.to_s}" }

  # Send method to custom runner.
  runner.send method, *args
rescue => error
  log(:error) { "Received error from runner: #{error.message}\n#{error.backtrace.join("    \n")}" }
ensure
  pool.executed Actor.current
end

#to_sObject



160
161
162
# File 'lib/msgr/pool.rb', line 160

def to_s
  "#{@poolname}[##{index}]"
end