Class: Katello::Logging::Timer

Inherits:
Object
  • Object
show all
Defined in:
app/lib/katello/logging.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = "default") ⇒ Timer

Returns a new instance of Timer.



20
21
22
23
24
25
# File 'app/lib/katello/logging.rb', line 20

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



46
47
48
49
50
51
52
53
# File 'app/lib/katello/logging.rb', line 46

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



41
42
43
44
# File 'app/lib/katello/logging.rb', line 41

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

#startObject



27
28
29
30
31
32
# File 'app/lib/katello/logging.rb', line 27

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

#stopObject



34
35
36
37
38
39
# File 'app/lib/katello/logging.rb', line 34

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