Class: Woodhouse::Layout::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/woodhouse/layout.rb

Overview

A Worker describes a single job that is performed on a Server. One or more Runner actors are created for every Worker in a Node.

Any Worker has three parameters used to route jobs to it:

worker_class_name

This is generally a class name. It’s looked up in a Registry and used to instantiate a job object.

job_method

This is a method on the object called up with worker_class_name.

criteria

A hash of values (actually, a QueueCriteria object) used to filter only specific jobs to this worker. When a job is dispatched, its arguments are compared with a worker’s criteria. This is done via an AMQP headers exchange (TODO: need to have a central document to reference on how Woodhouse uses AMQP and jobs are dispatched)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_class_name, job_method, opts = {}) ⇒ Worker

Returns a new instance of Worker.



193
194
195
196
197
198
199
200
201
# File 'lib/woodhouse/layout.rb', line 193

def initialize(worker_class_name, job_method, opts = {})
  opts = opts.clone
  self.worker_class_name = worker_class_name
  self.job_method = job_method
  self.threads   = opts.delete(:threads) || 1
  criteria  = opts.delete(:only)
  self.flags     = opts
  self.criteria  = criteria
end

Instance Attribute Details

#criteriaObject

Returns the value of attribute criteria.



190
191
192
# File 'lib/woodhouse/layout.rb', line 190

def criteria
  @criteria
end

#flagsObject

Returns the value of attribute flags.



191
192
193
# File 'lib/woodhouse/layout.rb', line 191

def flags
  @flags
end

#job_methodObject

Returns the value of attribute job_method.



190
191
192
# File 'lib/woodhouse/layout.rb', line 190

def job_method
  @job_method
end

#threadsObject

Returns the value of attribute threads.



190
191
192
# File 'lib/woodhouse/layout.rb', line 190

def threads
  @threads
end

#worker_class_nameObject

Returns the value of attribute worker_class_name.



190
191
192
# File 'lib/woodhouse/layout.rb', line 190

def worker_class_name
  @worker_class_name
end

Instance Method Details

#==(other) ⇒ Object

TODO: want to recognize increases and decreases in numbers of threads and make minimal changes



241
242
243
244
245
246
# File 'lib/woodhouse/layout.rb', line 241

def ==(other)
  [worker_class_name, job_method, 
    threads, criteria] ==
  [other.worker_class_name, other.job_method,
    other.threads, other.criteria]
end

#accepts_job?(job) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/woodhouse/layout.rb', line 235

def accepts_job?(job)
  criteria.matches?(job.arguments)
end

#describeObject



231
232
233
# File 'lib/woodhouse/layout.rb', line 231

def describe
  "#@worker_class_name##@job_method(#{@criteria.describe})"
end

#exchange_nameObject



203
204
205
# File 'lib/woodhouse/layout.rb', line 203

def exchange_name
  "#{worker_class_name}_#{job_method}".downcase
end

#frozen_cloneObject



227
228
229
# File 'lib/woodhouse/layout.rb', line 227

def frozen_clone
  clone.freeze
end

#queue_nameObject



207
208
209
# File 'lib/woodhouse/layout.rb', line 207

def queue_name
  exchange_name + criteria.queue_key
end