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.



132
133
134
135
136
137
138
139
# File 'lib/msgr/pool.rb', line 132

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.



130
131
132
# File 'lib/msgr/pool.rb', line 130

def index
  @index
end

#poolObject (readonly)

Returns the value of attribute pool.



130
131
132
# File 'lib/msgr/pool.rb', line 130

def pool
  @pool
end

#runnerObject (readonly)

Returns the value of attribute runner.



130
131
132
# File 'lib/msgr/pool.rb', line 130

def runner
  @runner
end

Instance Method Details

#dispatch(args) ⇒ Object

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



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

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

  # Send method to custom runner.
  runner.send :call, *args

rescue => error
  log(:error) { "Received error from runner: #{error.message}\n#{error.backtrace.join("    \n")}" }
ensure
  if pool.alive?
    pool.executed Actor.current
  else
    terminate
  end
end

#to_sObject



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

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