Created
July 9, 2025 11:12
-
-
Save le0pard/724cc24a849e2927fae6b50dfe930b28 to your computer and use it in GitHub Desktop.
Sidekiq healthcheck simple endpoint
This file contains hidden or 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
| module SidekiqAliveServer | |
| module_function | |
| def run! | |
| handler = ::Rackup::Handler.get(server) | |
| Signal.trap('TERM') { handler.shutdown } | |
| handler.run(self, Port: port, Host: host, AccessLog: [], Logger: Sidekiq.logger) | |
| end | |
| def host | |
| '0.0.0.0' | |
| end | |
| def port | |
| ENV.fetch('PORT', 7433) | |
| end | |
| def path | |
| '/health' | |
| end | |
| def server | |
| 'webrick' | |
| end | |
| def call(env) | |
| if Rack::Request.new(env).path == path | |
| [200, {}, ['Alive!']] | |
| else | |
| [404, {}, ['Not found']] | |
| end | |
| end | |
| end | |
| Sidekiq.configure_server do |config| | |
| config.on(:startup) do | |
| # skip for dev | |
| next if Rails.env.development? | |
| # live server | |
| @sidekiq_alive_server_pid = fork { SidekiqAliveServer.run! } | |
| config.logger.info "Starting sidekiq-alive-server on port: #{SidekiqAliveServer.port}" | |
| end | |
| config.on(:shutdown) do | |
| if @sidekiq_alive_server_pid.present? | |
| Process.kill('TERM', @sidekiq_alive_server_pid) | |
| Process.wait(@sidekiq_alive_server_pid) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment