Skip to content

Commit 1d2b042

Browse files
authored
Merge pull request #836 from agrare/non_rails_event_catcher_heartbeat
Heartbeat to file for process/container runtimes
2 parents 39a8b6f + dcaa651 commit 1d2b042

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

workers/event_catcher/event_catcher.rb

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
require_relative "event_parser"
2+
require "active_support/core_ext/numeric/time"
3+
require "more_core_extensions/core_ext/string/to_i_with_method"
24

35
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)
57
@ems = ems
68
@endpoint = endpoint
79
@authentication = authentication
810
@logger = logger
911
@messaging = messaging
1012
@page_size = page_size
13+
@settings = settings
1114
end
1215

1316
def run!
@@ -45,7 +48,7 @@ def stop!
4548

4649
private
4750

48-
attr_reader :ems, :endpoint, :authentication, :logger, :messaging, :page_size
51+
attr_reader :ems, :endpoint, :authentication, :logger, :messaging, :page_size, :settings
4952

5053
def connect
5154
vim_opts = {
@@ -135,14 +138,36 @@ def messaging_client
135138
end
136139

137140
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
139146
end
140147

141148
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
143154
end
144155

145156
def notify_stopping
146157
SdNotify.stopping if ENV.fetch("NOTIFY_SOCKET", nil)
147158
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
148173
end

workers/event_catcher/worker

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ require "bundler/inline"
55
gemfile(false) do
66
source "https://rubygems.org"
77

8+
gem "activesupport", "~>6.1.6"
89
gem "manageiq-loggers", "~>1.0"
910
gem "manageiq-messaging", "~> 1.0"
11+
gem "more_core_extensions"
1012
gem "rbvmomi2", "~> 3.3"
1113
if ENV.fetch("APPLIANCE", nil)
1214
gem "sd_notify"
@@ -40,8 +42,9 @@ def main(args)
4042
messaging = args["messaging"].symbolize_keys
4143
endpoint = ems["endpoints"].detect { |ep| ep["role"] == "default" }
4244
authentication = ems["authentications"].detect { |auth| auth["authtype"] == "default" }
45+
settings = args["settings"]
4346

44-
EventCatcher.new(ems, endpoint, authentication, messaging, logger).run!
47+
EventCatcher.new(ems, endpoint, authentication, settings, messaging, logger).run!
4548
end
4649

4750
def parse_args

0 commit comments

Comments
 (0)