Sha256: c766d1c684ecf4d43b7e4f906000353574f4a4982b96b111f1f295e415c06ce5

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

module Workers
  class Task
    include Workers::Helpers

    attr_reader :input
    attr_reader :result
    attr_reader :exception
    attr_reader :state
    attr_reader :max_tries
    attr_reader :tries

    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

    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

    def succeeded?
      @state == :succeeded
    end

    def failed?
      @state == :failed
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workers-0.6.1 lib/workers/task.rb
workers-0.6.0 lib/workers/task.rb