Sha256: 55fa831a1c3c38428bbaa75fcd5b411255603d61478dce30b8b604b2a91b1fa1

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module Sc4ry
  class RunController

    attr_reader :execution_time

    def initialize(circuit = {})
      @circuit = circuit
      @execution_time = 0
      @timeout = false
      @failure = false
      @overtime = false
    end

    def failed?
      return @failure
    end 
    
    def overtimed? 
      return @overtime
    end

    def timeout? 
      return @timeout
    end

    
    def run(options = {})
      start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      begin 
        if @circuit[:timeout] == true
          Timeout::timeout(@circuit[:timeout_value]) do 
            options[:block].call
          end
          @timeout = false
        else
          options[:block].call
        end
      rescue Exception => e
        @last_exception = e.class
        if e.class  == Timeout::Error then 
          @timeout = true
        elsif @circuit[:exceptions].include? e.class
          @failure = true
        else  
          Sc4ry::Loggers.warning "skipped : #{@last_exception}"
        end 
      end
      @end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      @execution_time = @end_time - start_time
      @overtime = @execution_time > @circuit[:max_time] 
      return {failure: @failure, overtime: @overtime, timeout: @timeout, execution_time: @execution_time, end_time: @end_time, last_exception: @last_exception}

    end



  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sc4ry-0.1.2 lib/sc4ry/run_controller.rb
sc4ry-0.1.1 lib/sc4ry/run_controller.rb
sc4ry-0.1.0 lib/sc4ry/run_controller.rb