health_check_rb 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/linters.yml +27 -0
  3. data/.github/workflows/tests.yml +50 -0
  4. data/.gitignore +33 -0
  5. data/.rspec +1 -0
  6. data/.rubocop.yml +110 -0
  7. data/CHANGELOG +122 -0
  8. data/Gemfile +23 -0
  9. data/MIT-LICENSE +23 -0
  10. data/README.md +392 -0
  11. data/Rakefile +27 -0
  12. data/config/routes.rb +7 -0
  13. data/health_check_rb.gemspec +26 -0
  14. data/init.rb +3 -0
  15. data/lib/health_check_rb/base_health_check.rb +7 -0
  16. data/lib/health_check_rb/check/elasticsearch.rb +18 -0
  17. data/lib/health_check_rb/check/rabbitmq.rb +19 -0
  18. data/lib/health_check_rb/check/redis.rb +30 -0
  19. data/lib/health_check_rb/check/resque.rb +18 -0
  20. data/lib/health_check_rb/check/s3.rb +62 -0
  21. data/lib/health_check_rb/check/sidekiq.rb +20 -0
  22. data/lib/health_check_rb/health_check_controller.rb +80 -0
  23. data/lib/health_check_rb/health_check_routes.rb +18 -0
  24. data/lib/health_check_rb/middleware_health_check.rb +108 -0
  25. data/lib/health_check_rb/utils.rb +194 -0
  26. data/lib/health_check_rb/version.rb +5 -0
  27. data/lib/health_check_rb.rb +129 -0
  28. data/spec/dummy/Rakefile +8 -0
  29. data/spec/dummy/app/assets/config/manifest.js +1 -0
  30. data/spec/dummy/app/controllers/example_controller.rb +7 -0
  31. data/spec/dummy/config/database.yml +10 -0
  32. data/spec/dummy/config/initializers/health_check.rb +18 -0
  33. data/spec/dummy/config/initializers/middleware.rb +3 -0
  34. data/spec/dummy/config/routes.rb +5 -0
  35. data/spec/dummy/db/migrate/.keep +0 -0
  36. data/spec/dummy/fake_app.rb +18 -0
  37. data/spec/dummy/tmp/.keep +0 -0
  38. data/spec/fixtures/migrate/9_create_countries.rb +9 -0
  39. data/spec/health_check_rb_spec.rb +300 -0
  40. data/spec/spec_helper.rb +36 -0
  41. metadata +101 -0
data/README.md ADDED
@@ -0,0 +1,392 @@
1
+ # health_check_rb gem
2
+
3
+ [![Tests](https://github.com/AlphaNodes/health_check_rb/workflows/Tests/badge.svg)](https://github.com/alphanodes/health_check_rb/actions/workflows/tests.yml) [![Run Linters](https://github.com/alphanodes/health_check_rb/workflows/Run%20Linters/badge.svg)](https://github.com/alphanodes/health_check_rb/actions/workflows/linters.yml)
4
+
5
+ Simple health check of Rails apps for use with Pingdom, NewRelic, EngineYard etc.
6
+
7
+ This is a fork of <https://github.com/Purple-Devs/health_check>
8
+
9
+ The basic goal is to quickly check that rails is up and running and that it has access to correctly configured resources (database, email gateway)
10
+
11
+ health_check provides various monitoring URIs, for example:
12
+
13
+ ```shell
14
+ curl localhost:3000/health_check
15
+ ```
16
+
17
+ ```text
18
+ success
19
+ ```
20
+
21
+ ```shell
22
+ curl localhost:3000/health_check/all.json
23
+ ```
24
+
25
+ ```json
26
+ { "healthy": true, "message": "success" }
27
+ ```
28
+
29
+ ```shell
30
+ curl localhost:3000/health_check/database_cache_migration.xml
31
+ ```
32
+
33
+ ```xml
34
+ <?xml version="1.0" encoding="UTF-8"?>
35
+ <hash>
36
+ <healthy type="boolean">true</healthy>
37
+ <message>success</message>
38
+ </hash>
39
+ ```
40
+
41
+ You may also issue POST calls instead of GET to these urls.
42
+
43
+ On failure (detected by health_check) a 500 http status is returned with a simple explanation of the failure (if include_error_in_response_body is true)
44
+
45
+ ```shell
46
+ curl localhost:3000/health_check/fail
47
+ ```
48
+
49
+ ```text
50
+ health_check failed: invalid argument to health_test.
51
+ ```
52
+
53
+ The health_check controller disables sessions for versions that eagerly load sessions.
54
+
55
+ ## Supported Versions
56
+
57
+ * Ruby 3.1, 3.2, 3.3, 3.4
58
+ * Rails 7.2, 8.0
59
+
60
+ ## Checks
61
+
62
+ * standard (default) - site, database and migrations checks are run plus email if ActionMailer is defined and it is not using the default configuration
63
+ * all / full - all checks are run (can be overridden in config block)
64
+ * cache - checks that a value can be written to the cache
65
+ * custom - runs checks added via config.add_custom_check
66
+ * database - checks that the current migration level can be read from the database
67
+ * email - basic check of email - :test returns true, :sendmail checks file is present and executable, :smtp sends HELO command to server and checks response
68
+ * migration - checks that the database migration level matches that in db/migrations
69
+ * rabbitmq - RabbitMQ Health Check
70
+ * redis / redis-if-present - checks Redis connectivity
71
+ * resque-redis / resque-redis-if-present - checks Resque connectivity to Redis
72
+ * s3 / s3-if-present - checks proper permissions to s3 buckets
73
+ * sidekiq-redis / sidekiq-redis-if-present - checks Sidekiq connectivity to Redis
74
+ * elasticsearch / elasticsearch-if-present - checks Elasticsearch connectivity
75
+ * site - checks rails is running sufficiently to render text
76
+
77
+ Some checks have a *-if-present form, which only runs the check if the corresponding library has been required.
78
+
79
+ The email gateway is not checked unless the smtp settings have been changed.
80
+ Specify full or include email in the list of checks to verify the smtp settings
81
+ (eg use 127.0.0.1 instead of localhost).
82
+
83
+ Note: rails also checks migrations by default in development mode and throws an `ActiveRecord::PendingMigrationError` exception (http error 500) if there is an error
84
+
85
+ ## Installation
86
+
87
+ Add the following line to Gemfile (after the rails gems are listed)
88
+
89
+ ```ruby
90
+ gem 'health_check_rb'
91
+ ```
92
+
93
+ And then execute
94
+
95
+ ```shell
96
+ bundle install
97
+ ```
98
+
99
+ ## Configuration
100
+
101
+ To change the configuration of health_check, create a file `config/initializers/health_check_rb.rb` and add a configuration block like:
102
+
103
+ ```ruby
104
+ HealthCheckRb.setup do |config|
105
+
106
+ # uri prefix (no leading slash)
107
+ config.uri = 'health_check'
108
+
109
+ # Text output upon success
110
+ config.success = 'success'
111
+
112
+ # Text output upon failure
113
+ config.failure = 'health_check failed'
114
+
115
+ # Disable the error message to prevent /health_check from leaking
116
+ # sensitive information
117
+ config.include_error_in_response_body = false
118
+
119
+ # Log level (success or failure message with error details is sent to rails log unless this is set to nil)
120
+ config.log_level = 'info'
121
+
122
+ # Timeout in seconds used when checking smtp server
123
+ config.smtp_timeout = 30.0
124
+
125
+ # http status code used when plain text error message is output
126
+ # Set to 200 if you want your want to distinguish between partial (text does not include success) and
127
+ # total failure of rails application (http status of 500 etc)
128
+
129
+ config.http_status_for_error_text = 500
130
+
131
+ # http status code used when an error object is output (json or xml)
132
+ # Set to 200 if you want to distinguish between partial (healthy property == false) and
133
+ # total failure of rails application (http status of 500 etc)
134
+
135
+ config.http_status_for_error_object = 500
136
+
137
+ # bucket names to test connectivity - required only if s3 check used, access permissions can be mixed
138
+ config.buckets = {'bucket_name' => [:R, :W, :D]}
139
+
140
+ # You can customize which checks happen on a standard health check, eg to set an explicit list use:
141
+ config.standard_checks = [ 'database', 'migrations', 'custom' ]
142
+
143
+ # Or to exclude one check:
144
+ config.standard_checks -= [ 'emailconf' ]
145
+
146
+ # You can set what tests are run with the 'full' or 'all' parameter
147
+ config.full_checks = ['database', 'migrations', 'custom', 'email', 'cache', 'redis', 'resque-redis', 'sidekiq-redis', 's3']
148
+
149
+ # Add one or more custom checks that return a blank string if ok, or an error message if there is an error
150
+ config.add_custom_check do
151
+ CustomHealthCheck.perform_check # any code that returns blank on success and non blank string upon failure
152
+ end
153
+
154
+ # Add another custom check with a name, so you can call just specific custom checks. This can also be run using
155
+ # the standard 'custom' check.
156
+ # You can define multiple tests under the same name - they will be run one after the other.
157
+ config.add_custom_check('sometest') do
158
+ CustomHealthCheck.perform_another_check # any code that returns blank on success and non blank string upon failure
159
+ end
160
+
161
+ # max-age of response in seconds
162
+ # cache-control is public when max_age > 1 and basic_auth_username is not set
163
+ # You can force private without authentication for longer max_age by
164
+ # setting basic_auth_username but not basic_auth_password
165
+ config.max_age = 1
166
+
167
+ # Protect health endpoints with basic auth
168
+ # These default to nil and the endpoint is not protected
169
+ config.basic_auth_username = 'my_username'
170
+ config.basic_auth_password = 'my_password'
171
+
172
+ # Whitelist requesting IPs by a list of IP and/or CIDR ranges, either IPv4 or IPv6 (uses IPAddr.include? method to check)
173
+ # Defaults to blank which allows any IP
174
+ config.origin_ip_whitelist = %w(123.123.123.123 10.11.12.0/24 2400:cb00::/32)
175
+
176
+ # Use ActionDispatch::Request's remote_ip method when behind a proxy to pick up the real remote IP for origin_ip_whitelist check
177
+ # Otherwise uses Rack::Request's ip method (the default, and always used by Middleware), which is more susceptible to spoofing
178
+ # See https://stackoverflow.com/questions/10997005/whats-the-difference-between-request-remote-ip-and-request-ip-in-rails
179
+ config.accept_proxied_requests = false
180
+
181
+ # http status code used when the ip is not allowed for the request
182
+ config.http_status_for_ip_whitelist_error = 403
183
+
184
+ # rabbitmq
185
+ config.rabbitmq_config = {}
186
+
187
+ # When redis url/password is non-standard
188
+ config.redis_url = 'redis_url' # default ENV['REDIS_URL']
189
+ # Only included if set, as url can optionally include passwords as well
190
+ config.redis_password = 'redis_password' # default ENV['REDIS_PASSWORD']
191
+
192
+ # Failure Hooks to do something more ...
193
+ # checks lists the checks requested
194
+ config.on_failure do |checks, msg|
195
+ # log msg somewhere
196
+ end
197
+
198
+ config.on_success do |checks|
199
+ # flag that everything is well
200
+ end
201
+ end
202
+ ```
203
+
204
+ You may call add_custom_check multiple times with different tests. These tests will be included in the default list ("standard").
205
+
206
+ If you have a catchall route then add the following line above the catch all route (in `config/routes.rb`):
207
+
208
+ ```ruby
209
+ health_check_rb_routes
210
+ ```
211
+
212
+ ### Installing As Middleware
213
+
214
+ Install health_check as middleware if you want to sometimes ignore exceptions from later parts of the Rails middleware stack, eg DB connection errors from QueryCache. The "middleware" check will fail if you have not installed health_check as middleware.
215
+
216
+ To install health_check as middleware add the following line to the config/application.rb:
217
+
218
+ ```ruby
219
+ config.middleware.insert_after Rails::Rack::Logger, HealthCheckRb::MiddlewareHealthcheck
220
+ ```
221
+
222
+ Note: health_check is installed as a full rails engine even if it has been installed as middleware. This is so the remaining checks continue to run through the complete rails stack.
223
+
224
+ You can also adjust what checks are run from middleware, eg if you want to exclude the checking of the database etc, then set
225
+
226
+ ```ruby
227
+ config.middleware_checks = ['middleware', 'standard', 'custom']
228
+ config.standard_checks = ['middleware', 'custom']
229
+ ```
230
+
231
+ Middleware checks are run first, and then full stack checks.
232
+ When installed as middleware, exceptions thrown when running the full stack tests are formatted in the standard way.
233
+
234
+ ## Uptime Monitoring
235
+
236
+ Use a website monitoring service to check the url regularly for the word "success" (without the quotes) rather than just a 200 http status so
237
+ that any substitution of a different server or generic information page should also be reported as an error.
238
+
239
+ If an error is encountered, the text "health_check failed: some error message/s" will be returned and the http status will be 500.
240
+
241
+ See
242
+
243
+ * Pingdom Website Monitoring - <https://www.pingdom.com/>
244
+ * NewRelic Availability Monitoring - <https://newrelic.com/>
245
+ * Engine Yard's guide - <https://support.engineyard.com/hc/en-us/articles/7598752539282-Monitor-Application-Uptime> (although the guide is based on fitter_happier plugin it will also work with this gem)
246
+ * Nagios check_http (with -s success) - <https://nagios-plugins.org/doc/man/check_http.html>
247
+ * Any other montoring service that can be set to check for the word success in the text returned from a url
248
+
249
+ ### Requesting Json and XML responses
250
+
251
+ Health_check will respond with an encoded hash object if json or xml is requested.
252
+ Either set the HTTP Accept header or append .json or .xml to the url.
253
+
254
+ The hash contains two keys:
255
+
256
+ * healthy - true if requested checks pass (boolean)
257
+ * message - text message ("success" or error message)
258
+
259
+ The following commands
260
+
261
+ ```shell
262
+ curl -v localhost:3000/health_check.json
263
+ curl -v localhost:3000/health_check/email.json
264
+ curl -v -H "Accept: application/json" localhost:3000/health_check
265
+ ```
266
+
267
+ Will return a result with Content-Type: application/json and body like:
268
+
269
+ ```json
270
+ { "healthy": true, "message": "success" }
271
+ ```
272
+
273
+ These following commands
274
+
275
+ ```shell
276
+ curl -v localhost:3000/health_check.xml
277
+ curl -v localhost:3000/health_check/migration_cache.xml
278
+ curl -v -H "Accept: text/xml" localhost:3000/health_check/cache
279
+ ```
280
+
281
+ Will return a result with Content-Type: application/xml and body like:
282
+
283
+ ```xml
284
+ <?xml version="1.0" encoding="UTF-8"?>
285
+ <hash>
286
+ <healthy type="boolean">true</healthy>
287
+ <message>success</message>
288
+ </hash>
289
+ ```
290
+
291
+ See <https://github.com/ianheggie/health_check/wiki/Ajax-Example> for an Ajax example
292
+
293
+ ## Silencing log output
294
+
295
+ It is recommended that you use silencer, lograge or one of the other log filtering gems.
296
+
297
+ For example, with lograge use the following to exclude health_check from being logged:
298
+
299
+ ```ruby
300
+ config.lograge.ignore_actions = ["HealthCheckRb::HealthCheckController#index"]
301
+ ```
302
+
303
+ Likewise you will probably want to exclude health_check from monitoring systems like newrelic.
304
+
305
+ ## Caching
306
+
307
+ Cache-control is set with
308
+
309
+ * public if max_age is > 1 and basic_auth_username is not set (otherwise private)
310
+ * no-cache
311
+ * must-revalidate
312
+ * max-age (default 1)
313
+
314
+ Last-modified is set to the current time (rounded down to a multiple of max_age when max_age > 1)
315
+
316
+ ## Known Issues
317
+
318
+ * See <https://github.com/alphanodes/health_check_rb/issues>
319
+
320
+ ## Similar projects
321
+
322
+ * health_check by Ian Heggie - this plugin is a fork of it
323
+ * fitter_happier plugin by atmos - plugin with similar goals, but not compatible with uptime, and does not check email gateway
324
+ * HealthBit - inspired by this gem but with a fresh start as a simpler rack only application, no travis CI tests (yet?) but looks interesting.
325
+
326
+ ## Manual testing
327
+
328
+ The instructions have been changed to using a vagrant virtual box for consistent results.
329
+
330
+ Install vagrant 1.9.7 or later and virtual_box or other local virtual machine provider. Add the vagrant plugin called vbguest.
331
+
332
+ ```shell
333
+ vagrant plugin install vagrant-vbguest
334
+ ```
335
+
336
+ Create a temp directory for throw away testing, and clone the health_check gem into it
337
+
338
+ ```shell
339
+ mkdir -p ~/tmp
340
+ cd ~/tmp
341
+ git clone https://github.com/ianheggie/health_check.git ~/tmp/health_check
342
+ ```
343
+
344
+ The Vagrantfile includes provisioning rules to install chruby (ruby version control),
345
+ ruby-build will also be installed and run to build various rubies under /opt/rubies.
346
+
347
+ Use <tt>vagrant ssh</tt> to connect to the virtual box and run tests.
348
+
349
+ The test script will package up and install the gem under a temporary path, create a dummy rails app configured for sqlite,
350
+ install the gem, and then run up tests against the server.
351
+ This will require TCP port 3456 to be free.
352
+
353
+ Cd to the checked out health_check directory and then run the test as follows:
354
+
355
+ ```shell
356
+ cd ~/tmp/health_check
357
+
358
+ vagrant up # this will also run vagrant provision and take some time
359
+ # chruby and various ruby versions will be installed
360
+
361
+ vagrant ssh
362
+
363
+ cd /vagrant # the current directory on your host is mounted here on the virtual machine
364
+
365
+ chruby 2.2.2 # or some other ruby version (run chruby with no arguments to see the current list)
366
+
367
+ test/test_with_railsapp
368
+
369
+ exit # from virtual machine when finished
370
+ ```
371
+
372
+ The script will first call `test/setup_railsapp` to setup a rails app with health_check installed and then
373
+ run up the rails server and perform veraious tests.
374
+
375
+ The script `test/setup_railsapp` will prompt you for which gemfile under test you wish to use to install the appropriate rails version, and then
376
+ setup tmp/railsapp accordingly.
377
+
378
+ The command `rake test` will also launch these tests, except it cannot install the bundler and rake gems if they are missing first (unlike test/test_with_railsapp)
379
+
380
+ ## Copyright
381
+
382
+ Copyright (c) 2025 Alexander Meindl, released under the MIT license.
383
+ Copyright (c) 2010-2021 Ian Heggie, released under the MIT license.
384
+ See MIT-LICENSE for details.
385
+
386
+ ## Contributors
387
+
388
+ Thanks go to the various people who have given feedback and suggestions via the issues list and pull requests.
389
+
390
+ ### Contributing
391
+
392
+ *Feedback welcome! Especially with suggested replacement code, tests and documentation*
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake'
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new :spec
8
+
9
+ task default: :spec
10
+
11
+ begin
12
+ gem 'rdoc'
13
+ require 'rdoc/task'
14
+
15
+ Rake::RDocTask.new do |rdoc|
16
+ version = HealthCheckRb::VERSION
17
+
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = "health_check_rb #{version}"
20
+ rdoc.rdoc_files.include 'README*'
21
+ rdoc.rdoc_files.include 'CHANGELOG'
22
+ rdoc.rdoc_files.include 'MIT-LICENSE'
23
+ rdoc.rdoc_files.include 'lib/**/*.rb'
24
+ end
25
+ rescue Gem::LoadError
26
+ puts 'rdoc (or a dependency) not available. Install it with: gem install rdoc'
27
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ unless HealthCheckRb::Engine.routes_explicitly_defined
4
+ Rails.application.routes.draw do
5
+ add_health_check_rb_routes
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path 'lib', __dir__
4
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
5
+ require 'health_check_rb/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = 'health_check_rb'
9
+ gem.version = HealthCheckRb::VERSION
10
+ gem.required_rubygems_version = Gem::Requirement.new('>= 0') if gem.respond_to? :required_rubygems_version=
11
+ gem.authors = ['Ian Heggie', 'Alexander Meindl']
12
+ gem.email = ['ian@heggie.biz']
13
+ gem.summary = 'Simple health check of Rails app for uptime monitoring with Pingdom, NewRelic, EngineYard etc.'
14
+ gem.description = <<-EOF
15
+ Simple health check of Rails app for uptime monitoring with Pingdom, NewRelic, EngineYard etc.
16
+ EOF
17
+ gem.homepage = 'https://github.com/alphanodes/health_check_rb'
18
+ gem.license = 'MIT'
19
+
20
+ gem.files = `git ls-files`.split $/
21
+ gem.extra_rdoc_files = ['README.md']
22
+ gem.require_paths = ['lib']
23
+ gem.required_ruby_version = '>= 3.1'
24
+ gem.add_dependency 'railties', ['>= 5.0']
25
+ gem.metadata['rubygems_mfa_required'] = 'true'
26
+ end
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'health_check_rb'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BaseHealthCheck
4
+ def create_error(check_type, error_message)
5
+ "[#{check_type} - #{error_message}] "
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckRb
4
+ module Check
5
+ class Elasticsearch
6
+ extend BaseHealthCheck
7
+
8
+ def self.check
9
+ raise "Wrong configuration. Missing 'elasticsearch' gem" unless defined?(::Elasticsearch)
10
+
11
+ res = ::Elasticsearch::Client.new.ping
12
+ res == true ? '' : "Elasticsearch returned #{res.inspect} instead of true"
13
+ rescue StandardError => e
14
+ create_error 'elasticsearch', e.message
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckRb
4
+ module Check
5
+ class RabbitMQ
6
+ extend BaseHealthCheck
7
+ def self.check
8
+ raise "Wrong configuration. Missing 'bunny' gem" unless defined?(::Bunny)
9
+
10
+ connection = Bunny.new HealthCheckRb.rabbitmq_config
11
+ connection.start
12
+ connection.close
13
+ ''
14
+ rescue StandardError => e
15
+ create_error 'rabbitmq', e.message
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckRb
4
+ module Check
5
+ class Redis
6
+ extend BaseHealthCheck
7
+
8
+ class << self
9
+ def check
10
+ raise "Wrong configuration. Missing 'redis' gem" unless defined?(::Redis)
11
+
12
+ client.ping == 'PONG' ? '' : "Redis.ping returned #{res.inspect} instead of PONG"
13
+ rescue StandardError => e
14
+ create_error 'redis', e.message
15
+ ensure
16
+ client.close if client.connected?
17
+ end
18
+
19
+ def client
20
+ @client ||= Redis.new(
21
+ {
22
+ url: HealthCheckRb.redis_url,
23
+ password: HealthCheckRb.redis_password
24
+ }.compact
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckRb
4
+ module Check
5
+ class Resque
6
+ extend BaseHealthCheck
7
+
8
+ def self.check
9
+ raise "Wrong configuration. Missing 'resque' gem" unless defined?(::Resque)
10
+
11
+ res = ::Resque.redis.ping
12
+ res == 'PONG' ? '' : "Resque.redis.ping returned #{res.inspect} instead of PONG"
13
+ rescue StandardError => e
14
+ create_error 'resque-redis', e.message
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckRb
4
+ module Check
5
+ class S3
6
+ extend BaseHealthCheck
7
+
8
+ class << self
9
+ def check
10
+ raise "Wrong configuration. Missing 'aws-sdk' or 'aws-sdk-s3' gem" unless defined?(::Aws)
11
+ return create_error 's3', 'Could not connect to aws' if aws_s3_client.nil?
12
+
13
+ HealthCheckRb.buckets.each do |bucket_name, permissions|
14
+ permissions = %i[read write delete] if permissions.nil? # backward compatible
15
+ permissions.each do |permission|
16
+ send :"try_#{permission}", bucket_name
17
+ rescue StandardError => e
18
+ raise "bucket:#{bucket_name}, permission:#{permission} - #{e.message}"
19
+ end
20
+ end
21
+ ''
22
+ rescue StandardError => e
23
+ create_error 's3', e.message
24
+ end
25
+
26
+ private
27
+
28
+ # We already assume you are using Rails. Let's also assume you have an initializer
29
+ # created for your Aws config. We will set the region here so you can use an
30
+ # instance profile and simply set the region in your environment.
31
+ def configure_client
32
+ ::Aws.config[:s3] = { force_path_style: true }
33
+ ::Aws.config[:region] ||= ENV['AWS_REGION'] || ENV.fetch('DEFAULT_AWS_REGION', nil)
34
+
35
+ ::Aws::S3::Client.new
36
+ end
37
+
38
+ def aws_s3_client
39
+ @aws_s3_client ||= configure_client
40
+ end
41
+
42
+ def try_read(bucket)
43
+ aws_s3_client.list_objects bucket:
44
+ end
45
+
46
+ def try_write(bucket)
47
+ app_name = ::Rails.application.class.module_parent_name
48
+
49
+ aws_s3_client.put_object bucket:,
50
+ key: "healthcheck_#{app_name}",
51
+ body: Time.zone.now.to_s
52
+ end
53
+
54
+ def try_delete(bucket)
55
+ app_name = ::Rails.application.class.module_parent_name
56
+ aws_s3_client.delete_object bucket:,
57
+ key: "healthcheck_#{app_name}"
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckRb
4
+ module Check
5
+ class Sidekiq
6
+ extend BaseHealthCheck
7
+
8
+ def self.check
9
+ raise "Wrong configuration. Missing 'sidekiq' gem" unless defined?(::Sidekiq)
10
+
11
+ ::Sidekiq.redis do |r|
12
+ res = r.ping
13
+ res == 'PONG' ? '' : "Sidekiq.redis.ping returned #{res.inspect} instead of PONG"
14
+ end
15
+ rescue StandardError => e
16
+ create_error 'sidekiq-redis', e.message
17
+ end
18
+ end
19
+ end
20
+ end