-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15494 from NickLaMuro/heartbeat_check
Add heartbeat_check script for file-based worker process heartbeating
- Loading branch information
Showing
8 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
end | ||
end | ||
end |