Class: ServiceJynx::Jynx

Inherits:
Object
  • Object
show all
Defined in:
lib/service_jynx.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Jynx

Returns a new instance of Jynx.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/service_jynx.rb', line 47

def initialize(name, options)
  @name = name
  @down_at = 0
  @alive = true
  @errors = []
  opts = {
    time_window_in_seconds: 10,
    max_errors: 40,
    grace_period: 360
    }.merge!(options)
  @time_window_in_seconds = opts[:time_window_in_seconds]
  @max_errors = opts[:max_errors]
  @grace_period = opts[:grace_period]
end

Instance Attribute Details

#aliveObject

Returns the value of attribute alive.



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

def alive
  @alive
end

#down_atObject

Returns the value of attribute down_at.



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

def down_at
  @down_at
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#grace_periodObject

Returns the value of attribute grace_period.



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

def grace_period
  @grace_period
end

#max_errorsObject

Returns the value of attribute max_errors.



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

def max_errors
  @max_errors
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#time_window_in_secondsObject

Returns the value of attribute time_window_in_seconds.



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

def time_window_in_seconds
  @time_window_in_seconds
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/service_jynx.rb', line 80

def alive?
  return true if @alive
  near_past = Time.now.to_i - @grace_period
  up! if (@down_at < near_past) and return true
  false
end

#clean_aged(time_now) ⇒ Object

clean up errors that are older than time_window_in_secons



63
64
65
66
# File 'lib/service_jynx.rb', line 63

def clean_aged(time_now)
  near_past = time_now - @time_window_in_seconds
  @errors = @errors.reverse.select{|time_stamp| time_stamp  > near_past }.reverse.to_a
end

#down!(reason) ⇒ Object



68
69
70
71
72
# File 'lib/service_jynx.rb', line 68

def down!(reason)
  @alive = false
  @down_at = Time.now.to_i
  LoggerJynx.logger.error "Shutting down [#{@name}] #{reason} at #{@down_at}."      
end

#up!Object



74
75
76
77
78
# File 'lib/service_jynx.rb', line 74

def up!
  LoggerJynx.logger.error "Upping [#{@name}]."      
  @alive = true
  @down_at = 0
end