Class: Murakumo::HealthChecker
- Inherits:
-
Object
- Object
- Murakumo::HealthChecker
- Defined in:
- lib/srv/murakumo_health_checker.rb
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #bad ⇒ Object
- #good ⇒ Object
-
#initialize(address, name, cloud, logger, options) ⇒ HealthChecker
constructor
A new instance of HealthChecker.
- #start ⇒ Object
- #stop ⇒ Object
- #toggle_health ⇒ Object
Constructor Details
#initialize(address, name, cloud, logger, options) ⇒ HealthChecker
Returns a new instance of HealthChecker.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/srv/murakumo_health_checker.rb', line 13 def initialize(address, name, cloud, logger, ) @address = address @name = name @cloud = cloud @logger = logger @options = # 各種変数の設定 ['interval', 'timeout', 'healthy', 'unhealthy'].each {|key| value = [key] instance_variable_set("@#{key}", value) } # スクリプトの読み込み @script = ['script'] raise "health check script of #{@name} is not found" unless @script @script = File.read(@script) if File.exists?(@script) # 通知オブジェクトの設定 if [:notification] @notifier = HealthCheckNotifier.new(@address, @name, @logger, [:notification]) end # イベントハンドラの設定 @on_activate = ['on-activate'] @on_inactivate = ['on-inactivate'] end |
Instance Method Details
#alive? ⇒ Boolean
143 144 145 |
# File 'lib/srv/murakumo_health_checker.rb', line 143 def alive? @alive end |
#bad ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/srv/murakumo_health_checker.rb', line 49 def bad if not @normal_health @healthy_count = 0 elsif (@unhealthy_count += 1) >= @unhealthy toggle_health end end |
#good ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/srv/murakumo_health_checker.rb', line 41 def good if @normal_health @unhealthy_count = 0 elsif (@healthy_count += 1) >= @healthy toggle_health end end |
#start ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/srv/murakumo_health_checker.rb', line 89 def start # 各種変数は初期状態にする @alive = true @normal_health = (@options['init-status'] == ACTIVE) @healthy_count = 0 @unhealthy_count = 0 # 既存のスレッドは破棄 if @thread and @thread.alive? begin @thread.kill rescue ThreadError end end @thread = Thread.start { begin while @alive retval = nil begin retval = timeout(@timeout) { HealthCheckContext.new(:name => @name, :logger => @logger, :options => @options).instance_eval(@script) } rescue Timeout::Error retval = false rescue => e retval = false = (["#{e.class}: #{e.}"] + (e.backtrace || [])).join("\n\tfrom ") @logger.error("health check failed: #{@name}: #{}") end status = retval == true ? 'good' : retval == false ? 'bad' : '-' @logger.debug("result of a health check: #{@name}: #{status}") if retval == true good elsif retval == false bad end sleep @interval end # while rescue Exception => e = (["#{e.class}: #{e.}"] + (e.backtrace || [])).join("\n\tfrom ") @logger.error("#{@name}: #{}") end # begin } end |
#stop ⇒ Object
139 140 141 |
# File 'lib/srv/murakumo_health_checker.rb', line 139 def stop @alive = false end |
#toggle_health ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/srv/murakumo_health_checker.rb', line 57 def toggle_health @normal_health = !@normal_health activity = (@normal_health ? ACTIVE : INACTIVE) @cloud.gossip.transaction do @cloud.gossip.data.each do |i| # 名前の一致するデータを更新 i[4] = activity if i[0] == @name end end @cloud.db.execute(<<-EOS, activity, @cloud.address, @name) UPDATE records SET activity = ? WHERE ip_address = ? AND name = ? EOS @healthy_count = 0 @unhealthy_count = 0 status = @normal_health ? 'healthy' : 'unhealthy' @logger.info("health condition changed: #{@name}: #{status}") case activity when ACTIVE @notifier.notify_active if @notifier handle_event(@on_activate, 'healthy') if @on_activate when INACTIVE @notifier.notify_inactive if @notifier handle_event(@on_inactivate, 'unhealthy') if @on_inactivate end end |