Class: Sc4ry::RunController

Inherits:
Object
  • Object
show all
Defined in:
lib/sc4ry/run_controller.rb

Overview

class Facility to run and update values/status for a circuit Proc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(circuit = {}) ⇒ RunController

constructor

Parameters:

  • circuit (Hash) (defaults to: {})

    the data of the circuit



13
14
15
16
17
18
19
# File 'lib/sc4ry/run_controller.rb', line 13

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

Instance Attribute Details

#execution_timeObject (readonly)

return the execution time of the proc



9
10
11
# File 'lib/sc4ry/run_controller.rb', line 9

def execution_time
  @execution_time
end

Instance Method Details

#failed?Boolean

return if the Proc failed on a covered exception by this circuit

Returns:

  • (Boolean)


23
24
25
# File 'lib/sc4ry/run_controller.rb', line 23

def failed?
  @failure
end

#overtimed?Boolean

return if the Proc overtime the specified time of the circuit

Returns:

  • (Boolean)


29
30
31
# File 'lib/sc4ry/run_controller.rb', line 29

def overtimed?
  @overtime
end

#run(block:) ⇒ Hash

run and update values for the bloc given by keyword

Parameters:

  • block (Proc)

    a block to run and calculate

Returns:

  • (Hash)

    a result Hash



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sc4ry/run_controller.rb', line 42

def run(block:)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  begin
    if @circuit[:timeout] == true
      Timeout.timeout(@circuit[:timeout_value]) do
        block.call
      end
      @timeout = false
    else
      block.call
    end
  rescue StandardError => e
    @last_exception = e.class.to_s
    if e.instance_of?(Timeout::Error)
      @timeout = true
    elsif @circuit[:exceptions].include? e.class
      @failure = true
    elsif @circuit[:forward_unknown_exceptions]
      raise e.class, "Sc4ry forward: #{e.message}"
    else
      Sc4ry::Helpers.log level: :debug, message: "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]
  { failure: @failure, overtime: @overtime, timeout: @timeout, execution_time: @execution_time,
    end_time: @end_time, last_exception: @last_exception }
end

#timeout?Boolean

return if the Proc timeout the timeout defined value of the circuit, if timeout is active

Returns:

  • (Boolean)


35
36
37
# File 'lib/sc4ry/run_controller.rb', line 35

def timeout?
  @timeout
end