Class: JobDispatch::Worker::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/job_dispatch/worker/item.rb

Overview

This represents a unit of work to be done. It will be serialised to Mongo database

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, method, *params) ⇒ Item

Returns a new instance of Item.



19
20
21
# File 'lib/job_dispatch/worker/item.rb', line 19

def initialize(target, method, *params)
  @target, @method, @params = target, method, params
end

Instance Attribute Details

#job_idObject

Returns the value of attribute job_id.



12
13
14
# File 'lib/job_dispatch/worker/item.rb', line 12

def job_id
  @job_id
end

#methodObject (readonly)

Returns the value of attribute method.



14
15
16
# File 'lib/job_dispatch/worker/item.rb', line 14

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



15
16
17
# File 'lib/job_dispatch/worker/item.rb', line 15

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



16
17
18
# File 'lib/job_dispatch/worker/item.rb', line 16

def result
  @result
end

#statusObject (readonly)

Returns the value of attribute status.



17
18
19
# File 'lib/job_dispatch/worker/item.rb', line 17

def status
  @status
end

#targetObject (readonly)

Returns the value of attribute target.



13
14
15
# File 'lib/job_dispatch/worker/item.rb', line 13

def target
  @target
end

Instance Method Details

#executeObject

execute the method on the target with the given parameters This will capture standard exceptions for return over network.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/job_dispatch/worker/item.rb', line 25

def execute
  begin
    JobDispatch.logger.info "Worker executing job #{job_id}: #{target}.#{method}"
    Thread.current["JobDispatch::Worker.job_id"] = job_id
    @klass = target.constantize
    @result = @klass.__send__(method.to_sym, *params)
    @status = :success
  rescue StandardError => ex

    notify_error(ex) rescue nil

    @result = {
        class: ex.class.to_s,
        message: ex.to_s,
        backtrace: ex.backtrace,
    }
    @status = :error
    JobDispatch.logger.debug ex
  ensure
    Thread.current["JobDispatch::Worker.job_id"] = nil
    JobDispatch.logger.info "Worker completed job #{job_id}: #{target}.#{method}, status: #{@status}"
  end
end

#notify_error(exception) ⇒ Object



49
50
51
# File 'lib/job_dispatch/worker/item.rb', line 49

def notify_error(exception)
  # subclass this to send error notifications
end