Sha256: 8156949aef5cc84e8ef37f46540b063840ee7e023243a4df143f67a3f8075fa5

Contents?: true

Size: 905 Bytes

Versions: 6

Compression:

Stored size: 905 Bytes

Contents

module Workers
  class Task
    include Workers::Helpers

    attr_reader :args
    attr_reader :result
    attr_reader :exception
    attr_reader :state

    def initialize(options = {})
      @logger = Workers::LogProxy.new(options[:logger])
      @args = options[:args] || []
      @perform = options[:perform] || raise('Perform callback is required.')
      @finished = options[:finished]
      @state = :initialized

      return nil
    end

    def run
      raise "Invalid state (#{@state})." unless @state == :initialized

      @state = :running

      begin
        @result = @perform.call(*@args)
        @state = :succeeded
      rescue Exception => e
        @state = :failed
        @exception = e
      end

      @finished.call(self)

      return nil
    end

    def succeeded?
      return @state == :succeeded
    end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
workers-0.1.4 lib/workers/task.rb
workers-0.1.3 lib/workers/task.rb
workers-0.1.2 lib/workers/task.rb
workers-0.1.1 lib/workers/task.rb
workers-0.1.0 lib/workers/task.rb
workers-0.0.9 lib/workers/task.rb