Sha256: 4f3a0af04118d3e067ea7582bfbedac7f0933e83c632dd2c65559d01b252090e

Contents?: true

Size: 953 Bytes

Versions: 2

Compression:

Stored size: 953 Bytes

Contents

require 'system_timer'
require 'qs'
require 'qs/runner'

module Qs

  class QsRunner < Runner

    attr_reader :timeout

    def initialize(handler_class, args = nil)
      super(handler_class, args)
      @timeout = handler_class.timeout || Qs.config.timeout
    end

    def run
      OptionalTimeout.new(self.timeout) do
        self.handler.qs_run_callback 'before'
        self.handler.qs_init
        self.handler.qs_run
        self.handler.qs_run_callback 'after'
      end
    rescue TimeoutError => exception
      error = TimeoutError.new "#{handler_class} timed out (#{timeout}s)"
      error.set_backtrace(exception.backtrace)
      raise error
    end

    private

    module OptionalTimeout
      def self.new(timeout, &block)
        if !timeout.nil?
          SystemTimer.timeout_after(timeout, TimeoutError, &block)
        else
          block.call
        end
      end
    end

  end

  TimeoutError = Class.new(RuntimeError)

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
qs-0.6.1 lib/qs/qs_runner.rb
qs-0.6.0 lib/qs/qs_runner.rb