Skip to content

Commit 06ee2a0

Browse files
committed
Heartbeat using sd_notify and heartbeat file
1 parent 00d7638 commit 06ee2a0

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

workers/event_catcher/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ gem "manageiq-messaging", "~> 0.1"
44
gem "manageiq-password", "~> 0.3"
55
gem "optimist", "~> 3.0"
66
gem "rbvmomi", "~> 2.0"
7+
gem "sd_notify"

workers/event_catcher/event_catcher.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "manageiq-messaging"
44
require "pathname"
5+
require "sd_notify"
56

67
class EventCatcher
78
def initialize(ems_id, hostname, username, password, port, messaging_host, messaging_port, page_size = 20)
@@ -20,13 +21,16 @@ def run!
2021
event_history_collector = create_event_history_collector(vim, page_size)
2122
property_filter = create_property_filter(vim, event_history_collector)
2223

24+
notify_started
25+
2326
wait_for_updates(vim) do |property_change|
2427
next unless property_change.name =~ /latestPage.*/
2528

2629
events = Array(property_change.val).map { |event| parse_event(event) }
2730
publish_events(events)
2831
end
2932
ensure
33+
notify_stopping
3034
property_filter&.DestroyPropertyFilter
3135
event_history_collector&.DestroyCollector
3236
vim&.close
@@ -89,6 +93,7 @@ def wait_for_updates(vim)
8993

9094
loop do
9195
update_set = vim.propertyCollector.WaitForUpdatesEx(:version => version, :options => options)
96+
heartbeat
9297
next if update_set.nil?
9398

9499
version = update_set.version
@@ -144,6 +149,24 @@ def messaging_client
144149
)
145150
end
146151
end
152+
153+
def notify_started
154+
SdNotify.ready if ENV["NOTIFY_SOCKET"]
155+
end
156+
157+
def heartbeat
158+
if ENV["NOTIFY_SOCKET"]
159+
SdNotify.watchdog
160+
else
161+
heartbeat_file = File.join(ENV["APP_ROOT"], "tmp", "#{ENV["GUID"]}.hb")
162+
timeout = 120
163+
File.write(heartbeat_file, (Time.now.utc + timeout).to_s)
164+
end
165+
end
166+
167+
def notify_stopping
168+
SdNotify.stopping if ENV["NOTIFY_SOCKET"]
169+
end
147170
end
148171

149172
def decrypt_env_vars

0 commit comments

Comments
 (0)