My opinionated ruby on rails template
1# frozen_string_literal: true
2
3say 'Setting up OkComputer for health checks...', :green
4
5gem 'okcomputer'
6
7say ' Creating OkComputer initializer...', :cyan
8initializer 'okcomputer.rb', <<~RUBY
9 # frozen_string_literal: true
10
11 # OkComputer Health Checks
12 # https://github.com/jphenow/okcomputer
13 #
14 # Custom check example:
15 # class MyCustomCheck < OkComputer::Check
16 # def check
17 # if some_condition
18 # mark_message "All good!"
19 # else
20 # mark_failure
21 # mark_message "Something went wrong"
22 # end
23 # end
24 # end
25
26 OkComputer.mount_at = 'health'
27
28 # Core checks
29 OkComputer::Registry.register 'database', OkComputer::ActiveRecordCheck.new
30 OkComputer::Registry.register 'cache', OkComputer::CacheCheck.new
31 OkComputer::Registry.register 'app_version', OkComputer::AppVersionCheck.new
32 OkComputer::Registry.register 'action_mailer', OkComputer::ActionMailerCheck.new
33
34 # Redis check (if using Redis)
35 if ENV['REDIS_URL'].present?
36 OkComputer::Registry.register 'redis', OkComputer::RedisCheck.new(url: ENV['REDIS_URL'])
37 end
38
39 # Run checks in parallel
40 OkComputer.check_in_parallel = true
41
42 # Log when health checks are run
43 OkComputer.logger = Rails.logger
44
45 # Require authentication for detailed health info in production
46 if Rails.env.production?
47 OkComputer.require_authentication(
48 ENV.fetch('HEALTH_CHECK_USER', 'health'),
49 ENV.fetch('HEALTH_CHECK_PASSWORD', 'check'),
50 except: %w[default]
51 )
52 end
53RUBY
54
55say 'OkComputer health checks configured!', :green
56say ' Endpoints:', :cyan
57say ' GET /health - all checks', :cyan
58say ' GET /health/database - database check', :cyan
59say ' GET /health/cache - cache check', :cyan
60say ' GET /health/all - all checks as JSON', :cyan