class JobDispatch::Worker::Item

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

Attributes

job_id[RW]
method[R]
params[R]
result[R]
status[R]
target[R]

Public Class Methods

new(target, method, *params) click to toggle source
# File lib/job_dispatch/worker/item.rb, line 19
def initialize(target, method, *params)
  @target, @method, @params = target, method, params
end

Public Instance Methods

execute() click to toggle source

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

# 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) click to toggle source
# File lib/job_dispatch/worker/item.rb, line 49
def notify_error(exception)
  # subclass this to send error notifications
end