Class: Tasker::Health::ReadinessChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/tasker/health/readiness_checker.rb

Overview

ReadinessChecker validates basic system readiness

Performs lightweight checks to determine if the system can accept new requests. Designed for Kubernetes readiness probes with fast response times.

Examples:

Basic usage

checker = Tasker::Health::ReadinessChecker.new
result = checker.check
puts result[:ready]  # true/false

With custom timeout

checker = Tasker::Health::ReadinessChecker.new(timeout: 3.0)
result = checker.check

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout: nil) ⇒ ReadinessChecker

Initialize readiness checker



27
28
29
# File 'lib/tasker/health/readiness_checker.rb', line 27

def initialize(timeout: nil)
  @timeout = timeout || Tasker::Configuration.configuration.health.readiness_timeout_seconds
end

Instance Attribute Details

#timeoutFloat (readonly)



22
23
24
# File 'lib/tasker/health/readiness_checker.rb', line 22

def timeout
  @timeout
end

Class Method Details

.detailed_statusHash

Class method for detailed readiness status



41
42
43
# File 'lib/tasker/health/readiness_checker.rb', line 41

def self.detailed_status
  new.check
end

.ready?Boolean

Class method for simple readiness check



34
35
36
# File 'lib/tasker/health/readiness_checker.rb', line 34

def self.ready?
  new.check[:ready]
end

Instance Method Details

#checkHash

Perform readiness check



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tasker/health/readiness_checker.rb', line 52

def check
  start_time = Time.current
  checks = {}

  begin
    # Use timeout protection for all checks
    Timeout.timeout(@timeout) do
      checks[:database] = check_database_connection_with_timing
      checks[:cache] = check_cache_availability_with_timing
    end

    ready = checks.values.all? { |check| check[:status] == 'healthy' }
    failed_checks = checks.select { |_name, check| check[:status] == 'unhealthy' }.keys.map(&:to_s)

    result = {
      ready: ready,
      checks: checks,
      message: ready ? 'System is ready' : 'System is not ready',
      timestamp: Time.current,
      check_duration: Time.current - start_time
    }

    # Add failed_checks if there are any failures
    result[:failed_checks] = failed_checks unless failed_checks.empty?
    result
  rescue Timeout::Error
    {
      ready: false,
      checks: checks,
      message: "Readiness check timed out after #{@timeout} seconds",
      timestamp: Time.current,
      check_duration: Time.current - start_time
    }
  rescue StandardError => e
    {
      ready: false,
      checks: checks,
      message: "Readiness check failed: #{e.message}",
      error: e.class.name,
      timestamp: Time.current,
      check_duration: Time.current - start_time
    }
  end
end