Class: Karafka::Web::Pro::Ui::Lib::SafeRunner

Inherits:
Object
  • Object
show all
Includes:
Core::Helpers::Time
Defined in:
lib/karafka/web/pro/ui/lib/safe_runner.rb

Overview

Class used to execute code that can fail but we do not want to fail the whole operation. The primary use-case is for displaying deserialized data. We always need to assume, that part of the data can be corrupted and it should not crash the whole UI.

It caches the result and does not run the code twice (only once). Additionally, it measures the CPU usage and total time during the execution of the code block.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ SafeRunner

Returns a new instance of SafeRunner.

Parameters:

  • block (Proc)

    code we want to safe-guard



20
21
22
23
24
25
26
27
28
29
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 20

def initialize(&block)
  @code = block
  @executed = false
  @success = false
  @error = nil
  @result = nil
  @cpu_time = 0
  @total_time = 0
  @allocations = false
end

Instance Attribute Details

#allocationsObject (readonly)

Returns the value of attribute allocations.



17
18
19
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 17

def allocations
  @allocations
end

#cpu_timeObject (readonly)

Returns the value of attribute cpu_time.



17
18
19
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 17

def cpu_time
  @cpu_time
end

#errorObject (readonly)

Returns the value of attribute error.



17
18
19
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 17

def error
  @error
end

#resultObject (readonly)

Returns the value of attribute result.



17
18
19
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 17

def result
  @result
end

#total_timeObject (readonly)

Returns the value of attribute total_time.



17
18
19
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 17

def total_time
  @total_time
end

Instance Method Details

#callObject

Runs the execution and returns block result



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
72
73
74
75
76
77
78
79
80
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 46

def call
  return @result if executed?

  @executed = true

  if objspace?
    ObjectSpace.trace_object_allocations_start
    before = ObjectSpace.each_object.count
  end

  # We measure time as close to the process as possible so it is not impacted by the
  # objects allocations count (if applicable)
  start_time = monotonic_now
  start_cpu = ::Process.times
  @result = @code.call
  @success = true

  @result
rescue StandardError => e
  @error = e
  @success = false
ensure
  end_time = monotonic_now
  end_cpu = ::Process.times

  @cpu_time = (
    (end_cpu.utime - start_cpu.utime) + (end_cpu.stime - start_cpu.stime)
  ) * 1_000
  @total_time = (end_time - start_time)

  if objspace?
    @allocations = ObjectSpace.each_object.count - before
    ObjectSpace.trace_object_allocations_stop
  end
end

#executed?Boolean

Returns was the code executed already or not yet.

Returns:

  • (Boolean)

    was the code executed already or not yet



83
84
85
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 83

def executed?
  @executed
end

#failure?Boolean

Returns was the code execution failed or not.

Returns:

  • (Boolean)

    was the code execution failed or not



41
42
43
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 41

def failure?
  !success?
end

#success?Boolean

Returns was the code execution successful or not.

Returns:

  • (Boolean)

    was the code execution successful or not



32
33
34
35
36
37
38
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 32

def success?
  return @success if executed?

  call

  @success
end