class Workers::Task

Attributes

exception[R]
input[R]
max_tries[R]
result[R]
state[R]
tries[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/workers/task.rb, line 12
def initialize(options = {})
  @logger = Workers::LogProxy.new(options[:logger])
  @input = options[:input] || []
  @on_perform = options[:on_perform] || raise(Workers::MissingCallbackError, 'on_perform callback is required.')
  @on_finished = options[:on_finished]
  @max_tries = options[:max_tries] || 1
  @state = :initialized
  @tries = 0

  raise Workers::MaxTriesError, 'max_tries must be >= 1' unless @max_tries >= 1

  nil
end

Public Instance Methods

failed?() click to toggle source
# File lib/workers/task.rb, line 53
def failed?
  @state == :failed
end
run() click to toggle source
# File lib/workers/task.rb, line 26
def run
  raise Workers::InvalidStateError, "Invalid state (#{@state})." unless @state == :initialized

  @state = :running

  while(@tries < @max_tries && @state != :succeeded)
    @tries += 1

    begin
      @result = @on_perform.call(@input)
      @state = :succeeded
      @exception = nil
    rescue Exception => e
      @state = :failed
      @exception = e
    end
  end

  @on_finished.call(self)

  nil
end
succeeded?() click to toggle source
# File lib/workers/task.rb, line 49
def succeeded?
  @state == :succeeded
end