-
-
Notifications
You must be signed in to change notification settings - Fork 288
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 basic health check API #679
Conversation
lib/shoryuken/launcher.rb
Outdated
@@ -32,6 +32,14 @@ def stop | |||
executor.wait_for_termination | |||
end | |||
|
|||
def healthy? | |||
Shoryuken.groups.keys.each do |group| | |||
return false unless @managers.find { |m| m.group == group }&.running? |
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.
Making @managers
into a hash map would be nicer here, but I'm not sure whether it's worth it for something so small. It won't be a performance impact unless there are way more groups than I can see happening, and it's only part of the health check anyway.
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.
Seems fine.
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.
I think all?
would be nice to use here.
diff --git a/lib/shoryuken/launcher.rb b/lib/shoryuken/launcher.rb
index e07c767..ec5a961 100644
--- a/lib/shoryuken/launcher.rb
+++ b/lib/shoryuken/launcher.rb
@@ -33,11 +33,9 @@ module Shoryuken
end
def healthy?
- Shoryuken.groups.keys.each do |group|
- return false unless @managers.find { |m| m.group == group }&.running?
+ Shoryuken.groups.keys.all? do |group|
+ @managers.find { |m| m.group == group }&.running?
end
-
- true
end
private
lib/shoryuken/runner.rb
Outdated
@@ -55,6 +55,10 @@ def run(options) | |||
end | |||
end | |||
|
|||
def healthy? | |||
@launcher&.healthy? || false |
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.
I'm not sure how I can test this... The specs for this file seem a bit bare-bones so there's not much in the way of code examples 😢
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.
Yeah, it's a little awkward. Try adding an attr_reader :launcher
so we can stub the Launcher
instance for a double in the spec:
diff --git a/lib/shoryuken/runner.rb b/lib/shoryuken/runner.rb
index 33459cc..a83eefa 100644
--- a/lib/shoryuken/runner.rb
+++ b/lib/shoryuken/runner.rb
@@ -15,6 +15,8 @@ module Shoryuken
include Util
include Singleton
+ attr_reader :launcher
+
def run(options)
self_read, self_write = IO.pipe
@@ -56,7 +58,7 @@ module Shoryuken
end
def healthy?
- @launcher&.healthy? || false
+ launcher&.healthy? || false
end
private
diff --git a/spec/shoryuken/runner_spec.rb b/spec/shoryuken/runner_spec.rb
index 43f5225..1bc5228 100644
--- a/spec/shoryuken/runner_spec.rb
+++ b/spec/shoryuken/runner_spec.rb
@@ -44,6 +44,20 @@ RSpec.describe Shoryuken::Runner do
end
end
+ describe '#healthy?' do
+ let(:runner) { Shoryuken::Runner.send(:new) }
+ let(:launcher) { instance_double('Shoryuken::Launcher') }
+
+ before do
+ allow(runner).to receive(:launcher).and_return(launcher)
+ end
+
+ it 'returns true if launcher is healthy' do
+ allow(launcher).to receive(:healthy?).and_return(true)
+ expect(runner).to be_healthy
+ end
+ end
+
describe '#daemonize' do
it 'calls Process.daemon' do
args = { daemon: true, logfile: '/dev/null' }
lib/shoryuken/launcher.rb
Outdated
@@ -32,6 +32,14 @@ def stop | |||
executor.wait_for_termination | |||
end | |||
|
|||
def healthy? | |||
Shoryuken.groups.keys.each do |group| | |||
return false unless @managers.find { |m| m.group == group }&.running? |
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.
I think all?
would be nice to use here.
diff --git a/lib/shoryuken/launcher.rb b/lib/shoryuken/launcher.rb
index e07c767..ec5a961 100644
--- a/lib/shoryuken/launcher.rb
+++ b/lib/shoryuken/launcher.rb
@@ -33,11 +33,9 @@ module Shoryuken
end
def healthy?
- Shoryuken.groups.keys.each do |group|
- return false unless @managers.find { |m| m.group == group }&.running?
+ Shoryuken.groups.keys.all? do |group|
+ @managers.find { |m| m.group == group }&.running?
end
-
- true
end
private
Thanks! |
This PR adds a basic health check API with a view to
a) expanding it in the future if desired
b) allowing consumers to hook in to the method to add their own processes for when to call it, for instance as part of a webserver they boot
Resolves #674