Class: PeriodicMonitor

Inherits:
Object show all
Defined in:
lib/wukong/periodic_monitor.rb

Overview

Periodic monitor

This is very much a work in progress

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extra_options = {}) ⇒ PeriodicMonitor

Returns a new instance of PeriodicMonitor.



15
16
17
18
19
20
21
22
23
24
# File 'lib/wukong/periodic_monitor.rb', line 15

def initialize extra_options={}
  @options       = {}
  @options.deep_merge!( extra_options || {} )
  @iter          = 0
  @start_time    = now
  @last_report   = @start_time
  @interval      = (options[:log_interval] || Settings[:log_interval]).to_i
  @interval      = 1000 unless @interval >= 1
  @time_interval = (options[:log_seconds]  || Settings[:log_seconds]).to_i
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



12
13
14
# File 'lib/wukong/periodic_monitor.rb', line 12

def interval
  @interval
end

#iterObject (readonly)

Returns the value of attribute iter.



11
12
13
# File 'lib/wukong/periodic_monitor.rb', line 11

def iter
  @iter
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/wukong/periodic_monitor.rb', line 11

def options
  @options
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



11
12
13
# File 'lib/wukong/periodic_monitor.rb', line 11

def start_time
  @start_time
end

#time_intervalObject

Returns the value of attribute time_interval.



13
14
15
# File 'lib/wukong/periodic_monitor.rb', line 13

def time_interval
  @time_interval
end

Instance Method Details

#elapsed_timeObject



60
61
62
# File 'lib/wukong/periodic_monitor.rb', line 60

def elapsed_time
  now - start_time
end

#emit(log_line) ⇒ Object



38
39
40
# File 'lib/wukong/periodic_monitor.rb', line 38

def emit log_line
  Log.info log_line
end

#incr!Object



42
43
44
# File 'lib/wukong/periodic_monitor.rb', line 42

def incr!
  @iter += 1
end

#nowObject



66
67
68
# File 'lib/wukong/periodic_monitor.rb', line 66

def now
  Time.now.utc
end

#periodically(*args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wukong/periodic_monitor.rb', line 26

def periodically *args, &block
  incr!
  if ready?
    @last_report = Time.now
    if block
      emit block.call(self, *args)
    else
      emit progress(*args)
    end
  end
end

#progress(*stuff) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/wukong/periodic_monitor.rb', line 50

def progress *stuff
  [
    "%15d" % iter,
    "%7.1f"% elapsed_time, "sec",
    "%7.1f"% rate, "/sec",
    now.to_flat,
    *stuff
  ].flatten.join("\t")
end

#rateObject



69
70
71
# File 'lib/wukong/periodic_monitor.rb', line 69

def rate
  iter.to_f / elapsed_time
end

#ready?Boolean

Returns:



46
47
48
# File 'lib/wukong/periodic_monitor.rb', line 46

def ready?
  (iter % @interval == 0) || (since > time_interval)
end

#sinceObject



63
64
65
# File 'lib/wukong/periodic_monitor.rb', line 63

def since
  now - @last_report
end