Class: Murakumo::HealthChecker

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

Instance Method Summary collapse

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, options)
  @address = address
  @name = name
  @cloud = cloud
  @logger = logger
  @options = options

  # 各種変数の設定
  ['interval', 'timeout', 'healthy', 'unhealthy'].each {|key|
    value = options[key]
    instance_variable_set("@#{key}", value)
  }

  # スクリプトの読み込み
  @script = options['script']
  raise "health check script of #{@name} is not found" unless @script
  @script = File.read(@script) if File.exists?(@script)

  # 通知オブジェクトの設定
  if options[:notification]
    @notifier = HealthCheckNotifier.new(@address, @name, @logger, options[:notification])
  end

  # イベントハンドラの設定
  @on_activate = options['on-activate']
  @on_inactivate = options['on-inactivate']
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/srv/murakumo_health_checker.rb', line 143

def alive?
  @alive
end

#badObject



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

#goodObject



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

#startObject



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
          message = (["#{e.class}: #{e.message}"] + (e.backtrace || [])).join("\n\tfrom ")
          @logger.error("health check failed: #{@name}: #{message}")
        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
      message = (["#{e.class}: #{e.message}"] + (e.backtrace || [])).join("\n\tfrom ")
      @logger.error("#{@name}: #{message}")
    end # begin
  }
end

#stopObject



139
140
141
# File 'lib/srv/murakumo_health_checker.rb', line 139

def stop
  @alive = false
end

#toggle_healthObject



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