|
1 | 1 | require_relative "event_parser" |
| 2 | +require "active_support/core_ext/numeric/time" |
| 3 | +require "more_core_extensions/core_ext/string/to_i_with_method" |
2 | 4 |
|
3 | 5 | class EventCatcher |
4 | | - def initialize(ems, endpoint, authentication, messaging, logger, page_size = 20) |
| 6 | + def initialize(ems, endpoint, authentication, settings, messaging, logger, page_size = 20) |
5 | 7 | @ems = ems |
6 | 8 | @endpoint = endpoint |
7 | 9 | @authentication = authentication |
8 | 10 | @logger = logger |
9 | 11 | @messaging = messaging |
10 | 12 | @page_size = page_size |
| 13 | + @settings = settings |
11 | 14 | end |
12 | 15 |
|
13 | 16 | def run! |
@@ -45,7 +48,7 @@ def stop! |
45 | 48 |
|
46 | 49 | private |
47 | 50 |
|
48 | | - attr_reader :ems, :endpoint, :authentication, :logger, :messaging, :page_size |
| 51 | + attr_reader :ems, :endpoint, :authentication, :logger, :messaging, :page_size, :settings |
49 | 52 |
|
50 | 53 | def connect |
51 | 54 | vim_opts = { |
@@ -135,14 +138,36 @@ def messaging_client |
135 | 138 | end |
136 | 139 |
|
137 | 140 | def notify_started |
138 | | - SdNotify.ready if ENV.fetch("NOTIFY_SOCKET", nil) |
| 141 | + if ENV.fetch("NOTIFY_SOCKET", nil) |
| 142 | + SdNotify.ready |
| 143 | + elsif ENV.fetch("WORKER_HEARTBEAT_FILE", nil) |
| 144 | + heartbeat_to_file |
| 145 | + end |
139 | 146 | end |
140 | 147 |
|
141 | 148 | def heartbeat |
142 | | - SdNotify.watchdog if ENV.fetch("NOTIFY_SOCKET", nil) |
| 149 | + if ENV.fetch("NOTIFY_SOCKET", nil) |
| 150 | + SdNotify.watchdog |
| 151 | + elsif ENV.fetch("WORKER_HEARTBEAT_FILE", nil) |
| 152 | + heartbeat_to_file |
| 153 | + end |
143 | 154 | end |
144 | 155 |
|
145 | 156 | def notify_stopping |
146 | 157 | SdNotify.stopping if ENV.fetch("NOTIFY_SOCKET", nil) |
147 | 158 | end |
| 159 | + |
| 160 | + def heartbeat_to_file |
| 161 | + heartbeat_file = ENV.fetch("WORKER_HEARTBEAT_FILE") |
| 162 | + |
| 163 | + File.write(heartbeat_file, heartbeat_timeout) |
| 164 | + end |
| 165 | + |
| 166 | + def heartbeat_timeout |
| 167 | + timeout = settings.dig(:workers, :worker_base, :event_catcher, :event_catcher_vmware, :heartbeat_timeout) |
| 168 | + timeout ||= settings.dig(:workers, :worker_base, :defaults, :heartbeat_timeout) |
| 169 | + timeout ||= "2.minutes" |
| 170 | + |
| 171 | + Time.now.to_i + timeout.to_i_with_method |
| 172 | + end |
148 | 173 | end |
0 commit comments