7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/observed/observer_helpers/timer.rb', line 7
def time(options={}, &block)
timeout_in_seconds = options[:timeout_in_seconds] ||
fail("The key `:timeout_in_seconds` must be exist in the options: #{options}")
begin
before = system.now
r = Timeout::timeout(timeout_in_seconds) do
{ status: :success, result: block.call }
end
after = system.now
elapsed_time = after - before
r[:elapsed_time] = elapsed_time
r
rescue Timeout::Error => e
log_debug "Handled the error but logging it just for your info: #{e.message}\n#{e.backtrace.join("\n")}" if self.is_a? Logging
{ status: :error, error: {message: 'Timed out.'}, timed_out: true }
rescue => e
log_error "Handled the error: #{e.message}\n#{e.backtrace.join("\n")}" if self.is_a? Logging
{ status: :error, error: {message: e.message} }
end
end
|