Skip to content
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

Merged
merged 6 commits into from
Oct 19, 2021
Merged

Add basic health check API #679

merged 6 commits into from
Oct 19, 2021

Conversation

LyleDavis
Copy link
Contributor

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

@@ -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?
Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine.

Copy link
Contributor

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

@@ -55,6 +55,10 @@ def run(options)
end
end

def healthy?
@launcher&.healthy? || false
Copy link
Contributor Author

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 😢

Copy link
Contributor

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' }

@@ -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?
Copy link
Contributor

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

@cjlarose
Copy link
Contributor

Thanks!

@cjlarose cjlarose merged commit e89c2f7 into ruby-shoryuken:master Oct 19, 2021
@LyleDavis LyleDavis deleted the add_health_check branch October 25, 2021 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recommendation for healthcheck
2 participants