Class: BigWig::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/bigwig/job.rb

Overview

Dispatches jobs as they are received from the queue to the relevant plugin.

Each message on the queue is expected to take a form similar to:

{
  :method => 'my_method_name', 
  :id => 'optional arbitrary task identifier', 
  :data => { :hash => :of, :relevant => :data }
}

The dispatch message asks for the Plugin that knows how to deal with the given method name and invokes it with the relevant id and data

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg) ⇒ Job

Returns a new instance of Job.



18
19
20
21
22
# File 'lib/bigwig/job.rb', line 18

def initialize(msg)
  @method = msg[:method]
  @task_id = msg[:id]
  @data = msg[:data] || {}
end

Class Method Details

.dispatch(msg) ⇒ Object

Dispatch the given message to the correct plugin



14
15
16
# File 'lib/bigwig/job.rb', line 14

def self.dispatch(msg)
  new(msg).run
end

Instance Method Details

#runObject

Invoke the plugin with the given data and task id



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bigwig/job.rb', line 25

def run
  jobid = @task_id || rand(0xfffffff).to_s(16).upcase
  BigWig::logger.info("Received #{@method} (job #{jobid}) with #{@data.inspect}")
  
  result = nil
  benchmark = begin
    Benchmark.measure do
      result = plugin_for(@method).call(@task_id, @data) 
    end
  rescue StandardError => e
    BigWig::logger.error("Bigwig Job id #{jobid} failed with exception #{e}: #{e.message}")
    raise e
  end

  BigWig::logger.info("Bigwig Job id #{jobid} completed in #{benchmark.real.round} seconds")
  return result
end