Class: Karafka::Web::Pro::Ui::Lib::SafeRunner
- Inherits:
-
Object
- Object
- Karafka::Web::Pro::Ui::Lib::SafeRunner
- 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
-
#allocations ⇒ Object
readonly
Returns the value of attribute allocations.
-
#cpu_time ⇒ Object
readonly
Returns the value of attribute cpu_time.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#total_time ⇒ Object
readonly
Returns the value of attribute total_time.
Instance Method Summary collapse
-
#call ⇒ Object
Runs the execution and returns block result.
-
#executed? ⇒ Boolean
Was the code executed already or not yet.
-
#failure? ⇒ Boolean
Was the code execution failed or not.
-
#initialize(&block) ⇒ SafeRunner
constructor
A new instance of SafeRunner.
-
#success? ⇒ Boolean
Was the code execution successful or not.
Constructor Details
#initialize(&block) ⇒ SafeRunner
Returns a new instance of SafeRunner.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 23 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
#allocations ⇒ Object (readonly)
Returns the value of attribute allocations.
20 21 22 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 20 def allocations @allocations end |
#cpu_time ⇒ Object (readonly)
Returns the value of attribute cpu_time.
20 21 22 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 20 def cpu_time @cpu_time end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
20 21 22 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 20 def error @error end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
20 21 22 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 20 def result @result end |
#total_time ⇒ Object (readonly)
Returns the value of attribute total_time.
20 21 22 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 20 def total_time @total_time end |
Instance Method Details
#call ⇒ Object
Runs the execution and returns block result
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 81 82 83 84 85 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 49 def call return @result if executed? @executed = true if objspace? GC.disable 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 GC.enable end end |
#executed? ⇒ Boolean
Returns was the code executed already or not yet.
88 89 90 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 88 def executed? @executed end |
#failure? ⇒ Boolean
Returns was the code execution failed or not.
44 45 46 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 44 def failure? !success? end |
#success? ⇒ Boolean
Returns was the code execution successful or not.
35 36 37 38 39 40 41 |
# File 'lib/karafka/web/pro/ui/lib/safe_runner.rb', line 35 def success? return @success if executed? call @success end |