Class: Murakumo::ActivityChecker
- Inherits:
-
Object
- Object
- Murakumo::ActivityChecker
- Defined in:
- lib/srv/murakumo_activity_checker.rb
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#initialize(address, name, cloud, logger, options) ⇒ ActivityChecker
constructor
A new instance of ActivityChecker.
- #mark_active ⇒ Object
- #mark_inactive ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #toggle_activity ⇒ Object
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, ) @address = address @name = name @cloud = cloud @logger = logger @options = # 各種変数の設定 ['interval', 'start-delay', 'active', 'inactive', 'init-status'].each {|key| value = [key] instance_variable_set("@#{key.gsub('-', '_')}", value) } # 通知オブジェクトの設定 if [:notification] @notifier = ActivityCheckNotifier.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_activity_checker.rb', line 143 def alive? @alive end |
#mark_active ⇒ Object
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_inactive ⇒ Object
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 |
#start ⇒ Object
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 = (["#{e.class}: #{e.}"] + (e.backtrace || [])).join("\n\tfrom ") @logger.error("activity check failed: #{@name}: #{}") 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 = (["#{e.class}: #{e.}"] + (e.backtrace || [])).join("\n\tfrom ") @logger.error("#{@name}: #{}") end # begin } end |
#stop ⇒ Object
139 140 141 |
# File 'lib/srv/murakumo_activity_checker.rb', line 139 def stop @alive = false end |
#toggle_activity ⇒ Object
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 |