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

Make health probe server more general purpose #1079

Merged
merged 30 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
51aaf5e
Make health probe server more general purpose
jklina Sep 17, 2023
ff1c8ad
Merge remote-tracking branch 'origin/main' into make-web-server-gener…
jklina Dec 6, 2023
7a2ee38
Use new API
jklina Dec 6, 2023
eb63619
Revert server name change
jklina Dec 6, 2023
ea9a964
Restore original naming
jklina Dec 12, 2023
6535288
Appease linters
jklina Dec 12, 2023
8ff54d7
Add required message for mock
jklina Dec 12, 2023
a6ee6db
Make test description relevant
jklina Dec 12, 2023
d5520aa
Allow for handler to be injected into ProbeServer
jklina Jan 2, 2024
ac2885c
Add WEBrick WEBrick handler
jklina Jan 2, 2024
f92de5b
Add WEBrick as a development dependency
jklina Jan 2, 2024
911798d
Add WEBrick tests and configuration
jklina Jan 3, 2024
ed08af0
Merge branch 'main' into make-web-server-general-purpose
jklina Jan 3, 2024
cd59f43
Add idle_timeout method to mock
jklina Jan 3, 2024
54955d1
Namespace server handlers
jklina Jan 3, 2024
59486a1
Warn and fallback when WEBrick isn't loadable
jklina Jan 4, 2024
59efbe9
inspect load path
jklina Jan 4, 2024
0239a9b
Account for multiple webrick entries in $LOAD_PATH
jklina Jan 4, 2024
c0d66ce
try removing load path test
jklina Jan 4, 2024
4226b27
For error on require to initiate test
jklina Jan 4, 2024
0b8375f
Handle explicit nils in intialization
jklina Jan 5, 2024
be6bb19
Allow probe_handler to be set in configuration
jklina Jan 5, 2024
0615186
Add documentation for probe server customization
jklina Jan 5, 2024
3359a1d
Appease linter
jklina Jan 5, 2024
7ecca37
Merge branch 'main' into make-web-server-general-purpose
jklina Jan 5, 2024
5a1fdcd
retrigger CI
jklina Jan 5, 2024
5429fd7
Rename `probe_server_app` to `probe_app`; make handler name a symbol;…
bensheldon Jan 6, 2024
1c67ba6
Update readme to have relevant app example
bensheldon Jan 6, 2024
206660a
Fix readme grammar
bensheldon Jan 6, 2024
2d9a4d2
Merge branch 'main' into make-web-server-general-purpose
bensheldon Jan 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
require "good_job/multi_scheduler"
require "good_job/notifier"
require "good_job/poller"
require "good_job/middleware/catch_all"
require "good_job/middleware/healthcheck"
require "good_job/http_server"
require "good_job/probe_server"
require "good_job/utility_server"
require "good_job/scheduler"
require "good_job/shared_executor"
require "good_job/systemd_service"
Expand Down
6 changes: 4 additions & 2 deletions lib/good_job/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ def start
capsule.start
systemd.start

if configuration.probe_port
probe_server = GoodJob::ProbeServer.new(port: configuration.probe_port)
middleware = Rails.application.config.good_job.middleware
port = Rails.application.config.good_job.middleware_port
if middleware && port
probe_server = GoodJob::UtilityServer.new(app: middleware, port: port)
probe_server.start
end

Expand Down
9 changes: 9 additions & 0 deletions lib/good_job/middleware/catch_all.rb
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
25 changes: 25 additions & 0 deletions lib/good_job/middleware/healthcheck.rb
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
47 changes: 0 additions & 47 deletions lib/good_job/probe_server.rb

This file was deleted.

32 changes: 32 additions & 0 deletions lib/good_job/utility_server.rb
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