Fix health check server by using Puma with Rack directly
Some checks are pending
Test and Build Docker Image / test (push) Waiting to run
Test and Build Docker Image / build (push) Blocked by required conditions

- Replace Sinatra run! calls with direct Puma::Server usage
- Use Rack-compatible approach to avoid Sinatra::Wrapper issues
- Remove problematic set method calls and run! method calls
- Both integrated and standalone health servers now use same Puma/Rack approach
- Remove outdated baktainer/logger require from main file

This should resolve the undefined method errors with Sinatra::Wrapper.
This commit is contained in:
James Paterni 2025-07-14 23:15:26 -04:00
parent cbde87e2ef
commit a68196431f
5 changed files with 35 additions and 8 deletions

View file

@ -26,7 +26,14 @@ class HealthServerRunner
@logger.info(" GET /metrics - Prometheus metrics")
begin
@health_server.run!(host: bind, port: port.to_i)
# Use Rack to run the Sinatra app
require 'rack'
require 'puma'
# Start Puma server with Rack
server = Puma::Server.new(@health_server)
server.add_tcp_listener(bind, port.to_i)
server.run.join
rescue Interrupt
@logger.info("Health check server stopped")
rescue => e

View file

@ -34,7 +34,6 @@ end
require 'docker-api'
require 'cron_calc'
require 'concurrent/executor/fixed_thread_pool'
require 'baktainer/logger'
require 'baktainer/container'
require 'baktainer/backup_command'
require 'baktainer/dependency_container'
@ -146,14 +145,20 @@ class Baktainer::Runner
run_at = ENV['BT_CRON'] || '0 0 * * *'
begin
@cron = CronCalc.new(run_at)
rescue
LOGGER.error("Invalid cron format for BT_CRON: #{run_at}.")
rescue => e
@logger.error("Invalid cron format for BT_CRON: #{run_at}. Error: #{e.message}")
@cron = CronCalc.new('0 0 * * *') # Fall back to default
end
loop do
now = Time.now
next_run = @cron.next
# CronCalc.next returns an array, get the first element
next_runs = @cron.next(now)
next_run = next_runs.is_a?(Array) ? next_runs.first : next_runs
# Convert to Time object if necessary
next_run = Time.at(next_run) if next_run.is_a?(Numeric)
sleep_duration = next_run - now
@logger.info("Sleeping for #{sleep_duration} seconds until #{next_run}.")
sleep(sleep_duration)
@ -323,14 +328,24 @@ class Baktainer::Runner
def start_health_server
@health_server_thread = Thread.new do
begin
health_server = @dependency_container.get(:health_check_server)
port = ENV['BT_HEALTH_PORT'] || 8080
bind = ENV['BT_HEALTH_BIND'] || '0.0.0.0'
@logger.info("Starting health check server on #{bind}:#{port}")
health_server.run!(host: bind, port: port.to_i)
# Use Rack to run the Sinatra app
require 'rack'
require 'puma'
app = Baktainer::HealthCheckServer.new(@dependency_container)
# Start Puma server with Rack
server = Puma::Server.new(app)
server.add_tcp_listener(bind, port.to_i)
server.run.join
rescue => e
@logger.error("Health check server error: #{e.message}")
@logger.debug(e.backtrace.join("\n"))
end
end

View file

@ -17,6 +17,7 @@ class Baktainer::HealthCheckServer < Sinatra::Base
configure do
set :environment, :production
set :logging, false # We'll handle logging ourselves
set :server, 'puma'
set :port, ENV['BT_HEALTH_PORT'] || 8080
set :bind, ENV['BT_HEALTH_BIND'] || '0.0.0.0'
end

View file

@ -74,7 +74,7 @@ example_id | status | run_time |
./spec/unit/baktainer_spec.rb[1:3:1] | passed | 0.00182 seconds |
./spec/unit/baktainer_spec.rb[1:3:2] | passed | 0.00171 seconds |
./spec/unit/baktainer_spec.rb[1:3:3] | passed | 0.00189 seconds |
./spec/unit/baktainer_spec.rb[1:3:4] | passed | 0.00243 seconds |
./spec/unit/baktainer_spec.rb[1:3:4] | passed | 0.00673 seconds |
./spec/unit/baktainer_spec.rb[1:4:1:1] | passed | 0.00201 seconds |
./spec/unit/baktainer_spec.rb[1:4:2:1] | passed | 0.27045 seconds |
./spec/unit/container_spec.rb[1:1:1] | passed | 0.00145 seconds |

View file

@ -4,11 +4,15 @@ services:
image: jamez001/baktainer:latest
container_name: baktainer
restart: unless-stopped
ports:
- "8080:8080"
volumes:
- ./backups:/backups
- ./config:/config
- /var/run/docker.sock:/var/run/docker.sock
environment:
- BT_HEALTH_SERVER_ENABLED=true
- BT_HEALTH_PORT=8080
- "BT_DOCKER_URL=unix:///var/run/docker.sock"
- BT_CRON=0 0 * * * # Backup every day at midnight
- BT_THREADS=4 # Number of threads to use for backups