Class: Clacky::DeployTools::CheckHealth

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/default_skills/deploy/tools/check_health.rb

Overview

Perform HTTP health check on deployed application

Constant Summary collapse

DEFAULT_PATH =
'/'
DEFAULT_TIMEOUT =
30
MAX_TIMEOUT =
120

Class Method Summary collapse

Class Method Details

.execute(url: nil, path: DEFAULT_PATH, timeout: DEFAULT_TIMEOUT) ⇒ Hash

Perform health check

Parameters:

  • url (String) (defaults to: nil)

    Optional URL (defaults to RAILWAY_PUBLIC_DOMAIN env var)

  • path (String) (defaults to: DEFAULT_PATH)

    Health check path (default: “/”)

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Request timeout in seconds (default: 30)

Returns:

  • (Hash)

    Result of health check



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/clacky/default_skills/deploy/tools/check_health.rb', line 21

def self.execute(url: nil, path: DEFAULT_PATH, timeout: DEFAULT_TIMEOUT)
  # Get URL from parameter or environment
  target_url = url || ENV['RAILWAY_PUBLIC_DOMAIN']
  
  if target_url.nil? || target_url.empty?
    return {
      error: "No URL provided",
      details: "Please provide a URL or set RAILWAY_PUBLIC_DOMAIN environment variable"
    }
  end

  # Ensure URL has protocol
  target_url = "https://#{target_url}" unless target_url.start_with?('http://', 'https://')

  # Build full URL with path
  full_url = "#{target_url.chomp('/')}#{path}"

  # Validate timeout
  timeout = timeout.to_i
  if timeout <= 0 || timeout > MAX_TIMEOUT
    timeout = DEFAULT_TIMEOUT
  end

  puts "🏥 Checking health: #{full_url} (timeout: #{timeout}s)"

  begin
    uri = URI.parse(full_url)
    result = perform_request(uri, timeout)
    
    if result[:success]
      puts "✅ Health check passed: #{result[:status_code]}"
    else
      puts "❌ Health check failed: #{result[:error]}"
    end
    
    result.merge(url: full_url, path: path)
  rescue URI::InvalidURIError => e
    {
      success: false,
      error: "Invalid URL",
      details: e.message,
      url: full_url
    }
  end
end

.perform_request(uri, timeout) ⇒ Hash

Perform HTTP request

Parameters:

  • uri (URI)

    Target URI

  • timeout (Integer)

    Timeout in seconds

Returns:

  • (Hash)

    Request result



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/clacky/default_skills/deploy/tools/check_health.rb', line 72

def self.perform_request(uri, timeout)
  start_time = Time.now
  
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  http.open_timeout = timeout
  http.read_timeout = timeout
  
  request = Net::HTTP::Get.new(uri.request_uri)
  request['User-Agent'] = 'Clacky-Deploy-Health-Check/1.0'
  
  response = http.request(request)
  elapsed = Time.now - start_time

  {
    success: response.is_a?(Net::HTTPSuccess),
    status_code: response.code.to_i,
    status_message: response.message,
    elapsed: elapsed.round(2),
    headers: response.to_hash,
    body_preview: response.body&.slice(0, 500) # First 500 chars
  }
rescue Net::OpenTimeout, Net::ReadTimeout => e
  {
    success: false,
    error: "Request timeout",
    details: e.message,
    elapsed: timeout
  }
rescue SocketError => e
  {
    success: false,
    error: "Network error",
    details: e.message
  }
rescue StandardError => e
  {
    success: false,
    error: "Health check failed",
    details: "#{e.class}: #{e.message}"
  }
end