forked from bensheldon/good_job
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make health probe server more general purpose
This removes the health check logic from the ProbeServer and renames the ProbeServer to UtilityServer that accepts any Rack based app. The health check and catchall logic are moved into simple Rack middleware that can be composed by users however they like and be used to preserve existing health check behavior while transitioning to a more general purpose utility server. All and all this pattern will allow users to add whatever functionality they like to GoodJob's web server by composing Rack apps and using GoodJob's configuration to pass in users' Rack apps. IE: ``` config.good_job.middleware = Rack::Builder.app do use GoodJob::Middleware::MyCustomMiddleware use GoodJob::Middleware::PrometheusExporter use GoodJob::Middleware::Healthcheck run GoodJob::Middleware::CatchAll end config.good_job.middleware_port = 7001 ``` This could help resolve: * bensheldon#750 * bensheldon#532
- Loading branch information
Showing
6 changed files
with
73 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module GoodJob | ||
module Middleware | ||
class CatchAll | ||
def self.call(env) | ||
[404, {}, ["Not found"]] | ||
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,25 @@ | ||
module GoodJob | ||
module Middleware | ||
class Healthcheck | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
case Rack::Request.new(env).path | ||
when '/', '/status' | ||
[200, {}, ["OK"]] | ||
when '/status/started' | ||
started = GoodJob::Scheduler.instances.any? && GoodJob::Scheduler.instances.all?(&:running?) | ||
started ? [200, {}, ["Started"]] : [503, {}, ["Not started"]] | ||
when '/status/connected' | ||
connected = GoodJob::Scheduler.instances.any? && GoodJob::Scheduler.instances.all?(&:running?) && | ||
GoodJob::Notifier.instances.any? && GoodJob::Notifier.instances.all?(&:listening?) | ||
connected ? [200, {}, ["Connected"]] : [503, {}, ["Not connected"]] | ||
else | ||
@app.call(env) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module GoodJob | ||
class UtilityServer | ||
def self.task_observer(time, output, thread_error) # rubocop:disable Lint/UnusedMethodArgument | ||
return if thread_error.is_a? Concurrent::CancelledOperationError | ||
|
||
GoodJob._on_thread_error(thread_error) if thread_error | ||
end | ||
|
||
def initialize(app:, port:) | ||
@port = port | ||
@app = app | ||
end | ||
|
||
def start | ||
@handler = HttpServer.new(@app, port: @port, logger: GoodJob.logger) | ||
@future = Concurrent::Future.new { @handler.run } | ||
@future.add_observer(self.class, :task_observer) | ||
@future.execute | ||
end | ||
|
||
def running? | ||
@handler&.running? | ||
end | ||
|
||
def stop | ||
@handler&.stop | ||
@future&.value # wait for Future to exit | ||
end | ||
end | ||
end |