Class: Murakumo::ActivityChecker

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

Instance Method Summary collapse

Constructor Details

#initialize(address, name, cloud, logger, options) ⇒ ActivityChecker

Returns a new instance of ActivityChecker.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/srv/murakumo_activity_checker.rb', line 10

def initialize(address, name, cloud, logger, options)
  @address = address
  @name = name
  @cloud = cloud
  @logger = logger
  @options = options

  # 各種変数の設定
  ['interval', 'start-delay', 'active', 'inactive', 'init-status'].each {|key|
    value = options[key]
    instance_variable_set("@#{key.gsub('-', '_')}", value)
  }

  # 通知オブジェクトの設定
  if options[:notification]
    @notifier = ActivityCheckNotifier.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_activity_checker.rb', line 143

def alive?
  @alive
end

#mark_activeObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/srv/murakumo_activity_checker.rb', line 33

def mark_active
  if @activated.nil?
    # 状態がなかったら、まず状態をセット
    @logger.info("initial activity: #{@name}: active")
    @activated = true
  elsif @activated == true # わざとですよ…
    @inactive_count = 0
  elsif (@active_count += 1) >= @active
    toggle_activity
  end
end

#mark_inactiveObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/srv/murakumo_activity_checker.rb', line 45

def mark_inactive
  if @activated.nil?
    # 状態がなかったら、まず状態をセット
    @logger.info("initial activity: #{@name}: inactive")
    @activated = false
  elsif @activated == false # わざとですよ…
    @active_count = 0
  elsif (@inactive_count += 1) >= @inactive
    toggle_activity
  end
end

#startObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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_activity_checker.rb', line 74

def start
  # 各種変数は初期状態にする
  @alive = true
  @active_count = 0
  @inactive_count = 0

  # アクティビティの初期状態を設定
  case @init_status
  when :active
    @activated = true
    @logger.info("initial activity: #{@name}: active")
  when :inactive
    @activated = false
    @logger.info("initial activity: #{@name}: inactive")
  else
    @activated = nil
  end

  # 既存のスレッドは破棄
  if @thread and @thread.alive?
    begin
      @thread.kill
    rescue ThreadError
    end
  end

  @thread = Thread.start {
    begin
      # 初回実行の遅延
      if @start_delay
        @logger.debug("activity check is delaying: #{@name}")
        sleep @start_delay
        @start_delay = nil
        @logger.debug("activity check is starting: #{@name}")
      end

      while @alive
        retval = nil

        begin
          retval = validate_activity
        rescue => e
          retval = false
          message = (["#{e.class}: #{e.message}"] + (e.backtrace || [])).join("\n\tfrom ")
          @logger.error("activity check failed: #{@name}: #{message}")
        end

        status = retval == true ? 'active' : retval == false ? 'inactive' : '-'
        @logger.debug("result of a activity check: #{@name}: #{status}")

        if retval == true
          mark_active
        elsif retval == false
          mark_inactive
        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_activity_checker.rb', line 139

def stop
  @alive = false
end

#toggle_activityObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/srv/murakumo_activity_checker.rb', line 57

def toggle_activity
  @activated = !@activated
  @active_count = 0
  @inactive_count = 0

  status = @activated ? 'active' : 'inactive'
  @logger.info("activity condition changed: #{@name}: #{status}")

  if @activated
    @notifier.notify_active if @notifier
    handle_event(@on_activate, 'active') if @on_activate
  else
    @notifier.notify_inactive if @notifier
    handle_event(@on_inactivate, 'inactive') if @on_inactivate
  end
end