Sha256: d94bc3a775ab1db0e68e95df2dc116531b394734b12b0394a689ae9bd3346ca9

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 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  
          if @circuit[:forward_unknown_exceptions] then
            raise e.class, "Sc4ry forward: #{e.message}" 
          else
            Sc4ry::Helpers.log level: :debug, message: "skipped : #{@last_exception}"
          end
          
        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

5 entries across 5 versions & 1 rubygems

Version Path
sc4ry-0.1.8 lib/sc4ry/run_controller.rb
sc4ry-0.1.7 lib/sc4ry/run_controller.rb
sc4ry-0.1.6 lib/sc4ry/run_controller.rb
sc4ry-0.1.5 lib/sc4ry/run_controller.rb
sc4ry-0.1.4 lib/sc4ry/run_controller.rb