Class: Katello::Logging::Timer
- Inherits:
-
Object
- Object
- Katello::Logging::Timer
- Defined in:
- app/lib/katello/logging.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(key = "default") ⇒ Timer
constructor
A new instance of Timer.
- #log(msg = nil) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(key = "default") ⇒ Timer
Returns a new instance of Timer.
25 26 27 28 29 30 |
# File 'app/lib/katello/logging.rb', line 25 def initialize(key = "default") @key = key Thread.current[:timers] ||= {} Thread.current[:timers][key] = self self.start end |
Class Method Details
.find_by_key(key) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'app/lib/katello/logging.rb', line 51 def self.find_by_key(key) if Thread.current&.[](:timers)&.[](key) Thread.current[:timers][key] else Rails.logger.warn "Timer #{key} not found on current thread; creating a new timer" self.new(key) end end |
Instance Method Details
#log(msg = nil) ⇒ Object
46 47 48 49 |
# File 'app/lib/katello/logging.rb', line 46 def log(msg = nil) duration = (Time.now - @start_time).truncate(2) Rails.logger.info ["Timer #{@key} running at #{Time.now}", msg, "#{duration} sec"].compact.join(': ') end |
#start ⇒ Object
32 33 34 35 36 37 |
# File 'app/lib/katello/logging.rb', line 32 def start Rails.logger.info "Timer #{@key} already started; resetting start time" if @start_time Rails.logger.info "Timer #{@key} starting at #{Time.now}" @start_time = Time.now self end |
#stop ⇒ Object
39 40 41 42 43 44 |
# File 'app/lib/katello/logging.rb', line 39 def stop fail ::StandardError, "Timer #{@key} is not started" unless @start_time duration = (Time.now - @start_time).truncate(2) @start_time = nil Rails.logger.info "Timer #{@key} stopping at #{Time.now}: #{duration} sec" end |