-
Notifications
You must be signed in to change notification settings - Fork 898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add heartbeat_check script for file-based worker process heartbeating #15494
Changes from all commits
487b2ce
e6364e0
9f05328
fb08dc0
05c2597
3dd93ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "optparse" | ||
|
||
options = {} | ||
opt_parser = OptionParser.new do |opts| | ||
opts.banner = "usage: #{File.basename($PROGRAM_NAME, '.rb')} [HEARTBEAT_FILE]" | ||
|
||
opts.on("-b=HBFILE", "--heartbeat-file=HBFILE", "Heartbeat file to read (overrides arg val)") do |val| | ||
options[:heartbeat_file] = val | ||
end | ||
|
||
opts.on("-g=GUID", "--guid=GUID", "Use this guid for finding the heartbeat file") do |val| | ||
options[:guid] = val | ||
end | ||
|
||
opts.on("-v", "--[no-]verbose", "Verbose output") do |val| | ||
options[:verbose] = val | ||
end | ||
|
||
opts.on("-h", "--help", "Displays this help") do | ||
puts opts | ||
exit | ||
end | ||
end | ||
opt_parser.parse! | ||
|
||
require "English" | ||
require File.expand_path("../../heartbeat.rb", __FILE__) | ||
heartbeat_file = options[:heartbeat_file] || ARGV[0] | ||
heartbeat_file ||= Workers::MiqDefaults.heartbeat_file(options[:guid]) | ||
|
||
exit_status = Workers::Heartbeat.file_check(heartbeat_file) | ||
|
||
at_exit { puts $ERROR_INFO.status if options[:verbose] } | ||
exit exit_status |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require_relative "miq_defaults" | ||
|
||
module Workers | ||
class Heartbeat | ||
def self.file_check(heartbeat_file = Workers::MiqDefaults.heartbeat_file) | ||
if File.exist?(heartbeat_file) | ||
current_time = Time.now.utc | ||
contents = File.read(heartbeat_file) | ||
mtime = File.mtime(heartbeat_file).utc | ||
timeout = if contents.empty? | ||
(mtime + Workers::MiqDefaults.heartbeat_timeout).utc | ||
else | ||
Time.parse(contents).utc | ||
end | ||
|
||
current_time < timeout | ||
else | ||
false | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require "active_support/core_ext/numeric/time" | ||
|
||
module Workers | ||
class MiqDefaults | ||
HEARTBEAT_TIMEOUT = 2.minutes.freeze | ||
STARTING_TIMEOUT = 10.minutes.freeze | ||
STOPPING_TIMEOUT = 10.minutes.freeze | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need a new class for these values? Shouldn't we be using the ones in I'm not sure why they were in two different places to begin with... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smells like thin workers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These were removing hardcoded values that existed in the code base of just As far as why it is it's own class, since we want to have the |
||
|
||
def self.heartbeat_timeout | ||
HEARTBEAT_TIMEOUT | ||
end | ||
|
||
def self.starting_timeout | ||
STARTING_TIMEOUT | ||
end | ||
|
||
def self.stopping_timeout | ||
STOPPING_TIMEOUT | ||
end | ||
|
||
def self.heartbeat_file(guid = nil) | ||
guid ||= "miq_worker" | ||
ENV["WORKER_HEARTBEAT_FILE"] || File.expand_path("../../../tmp/#{guid}.hb", __FILE__) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require "workers/heartbeat" | ||
|
||
shared_examples_for "heartbeat file checker" do |heartbeat_file = nil| | ||
# This is used instead of just passing in the `heartbeat_file` value directly | ||
# into the method because we can splat it in the argument list and force a "no | ||
# args" method call in each of the tests | ||
let(:file_check_args) { [heartbeat_file].compact } | ||
let(:calculated_hb_file) { Pathname.new(heartbeat_file || ENV["WORKER_HEARTBEAT_FILE"]) } | ||
around do |example| | ||
FileUtils.mkdir_p(calculated_hb_file.parent) | ||
File.write(calculated_hb_file, "") | ||
|
||
example.run | ||
|
||
FileUtils.rm_f(calculated_hb_file.to_s) | ||
end | ||
|
||
it "returns false when the heartbeat file does not exist" do | ||
FileUtils.rm_f calculated_hb_file.to_s # Early delete | ||
expect(Workers::Heartbeat.file_check(*file_check_args)).to eq(false) | ||
end | ||
|
||
it "returns true with a newly created heartbeat file with no content" do | ||
expect(Workers::Heartbeat.file_check(*file_check_args)).to eq(true) | ||
end | ||
|
||
it "returns false with a stale heartbeat file with no content" do | ||
to_the_future = (2.minutes + 2.seconds).from_now | ||
Timecop.travel(to_the_future) do | ||
expect(Workers::Heartbeat.file_check(*file_check_args)).to eq(false) | ||
end | ||
end | ||
|
||
it "returns true with a heartbeat file with content within the timeout" do | ||
# Set timeout in heartbeat file | ||
File.write(calculated_hb_file, 3.minutes.from_now) | ||
|
||
to_the_future = (2.minutes + 2.seconds).from_now | ||
Timecop.travel(to_the_future) do | ||
expect(Workers::Heartbeat.file_check(*file_check_args)).to eq(true) | ||
end | ||
end | ||
end | ||
|
||
describe Workers::Heartbeat do | ||
describe ".file_check" do | ||
context "using the default heartbeat_file" do | ||
let(:test_heartbeat_file) { ManageIQ.root.join("tmp", "spec", "test.hb") } | ||
|
||
around do |example| | ||
# This is given the highest priority when calling | ||
# Workers::MiqDefaults.heartbeat_file. | ||
# | ||
# Trying to avoid using mocks... | ||
old_env = ENV["WORKER_HEARTBEAT_FILE"] | ||
ENV["WORKER_HEARTBEAT_FILE"] = test_heartbeat_file.to_s | ||
|
||
example.run | ||
|
||
ENV["WORKER_HEARTBEAT_FILE"] = old_env | ||
end | ||
|
||
it_should_behave_like "heartbeat file checker" | ||
end | ||
|
||
context "when passing in a filepath as an argument" do | ||
other_heartbeat_file = ManageIQ.root.join("tmp", "spec", "other.hb").to_s | ||
|
||
it_should_behave_like "heartbeat file checker", other_heartbeat_file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I not only disagree with this one, since this is how rspec itself says how to do it, but also would meant that we should be writing specs like this: describe(".some_method") do
context("with some changed variable") do
expect(subject.some_method).to(eq(true))
end
end Which... well isn't the case in... oh... 101% of our specs #fakeMath |
||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is
worker_settings[:poll]
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@carbonin basically, pulled from here: https://github.com/ManageIQ/manageiq/blob/8b04e133/app/models/miq_server/worker_management/monitor/settings.rb#L38-L41