http_health_check 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.bumpversion.cfg +10 -0
- data/.env +1 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.rubocop.yml +9 -0
- data/.tool-versions +1 -0
- data/CHANGELOG.md +19 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +76 -0
- data/LICENSE +21 -0
- data/README.md +151 -0
- data/Rakefile +4 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/docker-compose.yml +13 -0
- data/http_health_check.gemspec +35 -0
- data/lib/http_health_check/config/dsl.rb +24 -0
- data/lib/http_health_check/probe/result.rb +29 -0
- data/lib/http_health_check/probe.rb +29 -0
- data/lib/http_health_check/probes/delayed_job.rb +25 -0
- data/lib/http_health_check/probes/sidekiq.rb +33 -0
- data/lib/http_health_check/probes.rb +8 -0
- data/lib/http_health_check/rack_app.rb +44 -0
- data/lib/http_health_check/version.rb +5 -0
- data/lib/http_health_check.rb +52 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 15d5d70900a1cdd00cc1121e6304e7660660ed6b1cb79295eb07865eaff51a9e
|
4
|
+
data.tar.gz: 6d275eab1b0c357c8883648ad3e04c59d0ce2298f0f351d3f9985533372cc336
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7e7a7e0510233606c94af8990e76262eafbe122a9130a0908ad9ee23679f5f2852d877acdaef51b1845d858a1bec4057f3ca268d676d243d3ca5042330b4c917
|
7
|
+
data.tar.gz: 9ef706f14f7afcefafa45f4278eb603b3c88bf01d238ccbe1b0d1f865df1ea78049479ee6bca2693ce030dc3776b0b46fd283b5ac24d6ebeb42789d12226761f
|
data/.bumpversion.cfg
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 2.5.8
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# CHANGELOG.md
|
2
|
+
|
3
|
+
## 0.1.1 (2022-07-17)
|
4
|
+
|
5
|
+
Features:
|
6
|
+
|
7
|
+
- implement basic functionality
|
8
|
+
- add builtin sidekiq probe
|
9
|
+
- add builtin delayed job probe
|
10
|
+
|
11
|
+
## 0.2.0 (2022-07-18)
|
12
|
+
|
13
|
+
Features:
|
14
|
+
|
15
|
+
- add an ability to configure logger
|
16
|
+
|
17
|
+
Fix:
|
18
|
+
|
19
|
+
- fix builtin probes requirement
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in http_health_check.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'rake', '~> 13.0'
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem 'rspec_junit_formatter'
|
12
|
+
gem 'simplecov', require: false
|
13
|
+
gem 'simplecov-cobertura', require: false
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
http_health_check (0.2.0)
|
5
|
+
rack (~> 2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
diff-lcs (1.5.0)
|
12
|
+
docile (1.4.0)
|
13
|
+
dotenv (2.7.6)
|
14
|
+
parallel (1.22.1)
|
15
|
+
parser (3.1.2.0)
|
16
|
+
ast (~> 2.4.1)
|
17
|
+
rack (2.2.4)
|
18
|
+
rainbow (3.1.1)
|
19
|
+
rake (13.0.6)
|
20
|
+
redis (4.2.5)
|
21
|
+
regexp_parser (2.5.0)
|
22
|
+
rexml (3.2.5)
|
23
|
+
rspec (3.11.0)
|
24
|
+
rspec-core (~> 3.11.0)
|
25
|
+
rspec-expectations (~> 3.11.0)
|
26
|
+
rspec-mocks (~> 3.11.0)
|
27
|
+
rspec-core (3.11.0)
|
28
|
+
rspec-support (~> 3.11.0)
|
29
|
+
rspec-expectations (3.11.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.11.0)
|
32
|
+
rspec-mocks (3.11.1)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.11.0)
|
35
|
+
rspec-support (3.11.0)
|
36
|
+
rspec_junit_formatter (0.5.1)
|
37
|
+
rspec-core (>= 2, < 4, != 2.12.0)
|
38
|
+
rubocop (0.93.1)
|
39
|
+
parallel (~> 1.10)
|
40
|
+
parser (>= 2.7.1.5)
|
41
|
+
rainbow (>= 2.2.2, < 4.0)
|
42
|
+
regexp_parser (>= 1.8)
|
43
|
+
rexml
|
44
|
+
rubocop-ast (>= 0.6.0)
|
45
|
+
ruby-progressbar (~> 1.7)
|
46
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
47
|
+
rubocop-ast (1.17.0)
|
48
|
+
parser (>= 3.1.1.0)
|
49
|
+
ruby-progressbar (1.11.0)
|
50
|
+
simplecov (0.21.2)
|
51
|
+
docile (~> 1.1)
|
52
|
+
simplecov-html (~> 0.11)
|
53
|
+
simplecov_json_formatter (~> 0.1)
|
54
|
+
simplecov-cobertura (2.1.0)
|
55
|
+
rexml
|
56
|
+
simplecov (~> 0.19)
|
57
|
+
simplecov-html (0.12.3)
|
58
|
+
simplecov_json_formatter (0.1.4)
|
59
|
+
unicode-display_width (1.8.0)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
x86_64-darwin-21
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
dotenv (~> 2.7.6)
|
66
|
+
http_health_check!
|
67
|
+
rake (~> 13.0)
|
68
|
+
redis (~> 4.2.5)
|
69
|
+
rspec (~> 3.2)
|
70
|
+
rspec_junit_formatter
|
71
|
+
rubocop (~> 0.81)
|
72
|
+
simplecov
|
73
|
+
simplecov-cobertura
|
74
|
+
|
75
|
+
BUNDLED WITH
|
76
|
+
2.3.15
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 SberMarket Tech
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# HttpHealthCheck
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'http_health_check', '~> 0.2.0'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install http_health_check
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Sidekiq
|
22
|
+
|
23
|
+
Sidekiq health check is available at `/readiness/sidekiq`.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# ./config/initializers/sidekiq.rb
|
27
|
+
Sidekiq.configure_server do |config|
|
28
|
+
HttpHealthCheck.run_server_async(port: 5555)
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
### Delayed Job
|
33
|
+
|
34
|
+
DelayedJob health check is available at `/readiness/delayed_job`.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# ./script/delayed_job
|
38
|
+
module Delayed::AfterFork
|
39
|
+
def after_fork
|
40
|
+
HttpHealthCheck.run_server_async(port: 5555)
|
41
|
+
super
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
### Changing global configuration
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
HttpHealthCheck.configure do |c|
|
50
|
+
# add probe with any callable class
|
51
|
+
c.probe '/health/my_service', MyProbe.new
|
52
|
+
|
53
|
+
# or with block
|
54
|
+
c.probe '/health/fake' do |_env|
|
55
|
+
[200, {}, ['OK']]
|
56
|
+
end
|
57
|
+
|
58
|
+
# optionally add builtin probes
|
59
|
+
HttpHealthCheck.add_builtin_probes(c)
|
60
|
+
|
61
|
+
# optionally override fallback (route not found) handler
|
62
|
+
c.fallback_handler do |env|
|
63
|
+
[404, {}, ['not found :(']]
|
64
|
+
end
|
65
|
+
|
66
|
+
# configure requests logger. Disabled by default
|
67
|
+
c.logger Rails.logger
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Running server with custom rack app
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
rack_app = HttpHealthCheck::RackApp.configure do |c|
|
75
|
+
c.probe '/health/my_service', MyProbe.new
|
76
|
+
end
|
77
|
+
HttpHealthCheck.run_server_async(port: 5555, rack_app: rack_app)
|
78
|
+
```
|
79
|
+
|
80
|
+
### Writing your own probes
|
81
|
+
|
82
|
+
Probes are built around [HttpHealthCheck::Probe](./lib/http_health_check/probe.rb) mixin. Every probe defines **probe** method which receives [rack env](https://www.rubydoc.info/gems/rack/Rack/Request/Env)
|
83
|
+
and should return [HttpHealthCheck::Probe::Result](./lib/http_health_check/probe/result.rb) or rack-compatible response (status-headers-body tuple).
|
84
|
+
Probe-mixin provides convenience methods `probe_ok` and `probe_error` for creating [HttpHealthCheck::Probe::Result](./lib/http_health_check/probe/result.rb) instance. Both of them accept optional metadata hash that will be added to response body.
|
85
|
+
Any exception (StandardError) will be captured and converted into error-result.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class MyProbe
|
89
|
+
include HttpHealthCheck::Probe
|
90
|
+
|
91
|
+
def probe(_env)
|
92
|
+
status = MyService.status
|
93
|
+
return probe_ok if status == :ok
|
94
|
+
|
95
|
+
probe_error status: status
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
HttpHealthCheck.configure do |config|
|
102
|
+
config.probe '/readiness/my_service', MyProbe.new
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
### Builtin probes
|
107
|
+
|
108
|
+
#### [Sidekiq](./lib/http_health_check/probes/sidekiq.rb)
|
109
|
+
|
110
|
+
Sidekiq probe ensures that sidekiq is ready by checking redis is available and writable. It uses sidekiq's redis connection pool to avoid spinning up extra connections.
|
111
|
+
Be aware, that this approach does not cover issues with sidekiq being stuck processing slow/endless jobs. Such cases are nearly impossible to cover without false-positive alerts.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
HttpHealthCheck.configure do |config|
|
115
|
+
config.probe '/readiness/delayed_job', HttpHealthCheck::Probes::Sidekiq.new
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
#### [DelayedJob](./lib/http_health_check/probes/delayed_job.rb) (active record)
|
120
|
+
|
121
|
+
Delayed Job probe is intended to work with [active record backend](https://github.com/collectiveidea/delayed_job_active_record).
|
122
|
+
It checks DelayedJob is healthy by enqueuing an empty job which will be deleted right after insertion. This allows us to be sure that underlying database is connectable and writable.
|
123
|
+
Be aware, that by enqueuing a new job with every health check we are incrementing primary key sequence.
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
HttpHealthCheck.configure do |config|
|
127
|
+
config.probe '/readiness/delayed_job', HttpHealthCheck::Probes::DelayedJob.new
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
## Development
|
132
|
+
|
133
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
134
|
+
Some specs require redis to be run. You can use your own installation or start one via docker-compose.
|
135
|
+
|
136
|
+
```shell
|
137
|
+
docker-compose up redis
|
138
|
+
```
|
139
|
+
|
140
|
+
## Deployment
|
141
|
+
|
142
|
+
Every new (git) tag will be built and deployed automatically via gitlab CI pipeline. We recommend using [bump2version](https://github.com/c4urself/bump2version) to tag new releases.
|
143
|
+
|
144
|
+
1. Update changelog and git add it
|
145
|
+
2.
|
146
|
+
|
147
|
+
```sh
|
148
|
+
bump2version patch --allow-dirty
|
149
|
+
```
|
150
|
+
|
151
|
+
3. git push && git push --tags
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'http_health_check'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/docker-compose.yml
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/http_health_check/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'http_health_check'
|
7
|
+
spec.version = HttpHealthCheck::VERSION
|
8
|
+
spec.authors = ['SberMarket team']
|
9
|
+
spec.email = ['pochi.73@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Simple and extensible HTTP health checks server.'
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = 'https://github.com/SberMarket-Tech/http_health_check'
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
|
15
|
+
|
16
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
|
17
|
+
|
18
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
19
|
+
spec.metadata['source_code_uri'] = 'https://github.com/SberMarket-Tech/http_health_check'
|
20
|
+
spec.metadata['changelog_uri'] = 'https://github.com/SberMarket-Tech/http_health_check/blob/main/CHANGELOG.md'
|
21
|
+
|
22
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = 'exe'
|
26
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ['lib']
|
28
|
+
|
29
|
+
spec.add_dependency 'rack', '~> 2.0'
|
30
|
+
|
31
|
+
spec.add_development_dependency 'dotenv', '~> 2.7.6'
|
32
|
+
spec.add_development_dependency 'redis', '~> 4.2.5'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
34
|
+
spec.add_development_dependency 'rubocop', '~> 0.81'
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HttpHealthCheck
|
4
|
+
module Config
|
5
|
+
class Dsl
|
6
|
+
def initialize
|
7
|
+
@routes = {}
|
8
|
+
end
|
9
|
+
attr_reader :routes, :configured_fallback_app, :configured_logger
|
10
|
+
|
11
|
+
def probe(path, handler = nil, &block)
|
12
|
+
@routes[path] = block_given? ? block : handler
|
13
|
+
end
|
14
|
+
|
15
|
+
def fallback_app(handler = nil, &block)
|
16
|
+
@configured_fallback_app = block_given? ? block : handler
|
17
|
+
end
|
18
|
+
|
19
|
+
def logger(logger)
|
20
|
+
@configured_logger = logger
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HttpHealthCheck
|
4
|
+
module Probe
|
5
|
+
class Result
|
6
|
+
def self.ok(meta)
|
7
|
+
new(true, meta)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.error(meta)
|
11
|
+
new(false, meta)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(is_ok, meta)
|
15
|
+
@meta = Hash(meta)
|
16
|
+
@ok = is_ok
|
17
|
+
rescue StandardError => e
|
18
|
+
e = ::HttpHealthCheck::ConfigurationError.new(e.message)
|
19
|
+
e.set_backtrace(e.backtrace)
|
20
|
+
raise e
|
21
|
+
end
|
22
|
+
attr_reader :meta
|
23
|
+
|
24
|
+
def ok?
|
25
|
+
@ok
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'probe/result'
|
4
|
+
|
5
|
+
module HttpHealthCheck
|
6
|
+
module Probe
|
7
|
+
def call(env)
|
8
|
+
with_error_handler { probe(env) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def meta
|
12
|
+
{}
|
13
|
+
end
|
14
|
+
|
15
|
+
def probe_ok(extra_meta = {})
|
16
|
+
Result.ok(meta.merge(extra_meta))
|
17
|
+
end
|
18
|
+
|
19
|
+
def probe_error(extra_meta = {})
|
20
|
+
Result.error(meta.merge(extra_meta))
|
21
|
+
end
|
22
|
+
|
23
|
+
def with_error_handler
|
24
|
+
yield
|
25
|
+
rescue StandardError => e
|
26
|
+
probe_error(error_class: e.class.name, error_message: e.message)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HttpHealthCheck
|
4
|
+
module Probes
|
5
|
+
class DelayedJob
|
6
|
+
class HealthCheckJob
|
7
|
+
def self.perform; end
|
8
|
+
|
9
|
+
def self.queue_name
|
10
|
+
'health-check'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
include ::HttpHealthCheck::Probe
|
14
|
+
|
15
|
+
def initialize(delayed_job: ::Delayed::Job)
|
16
|
+
@delayed_job = delayed_job
|
17
|
+
end
|
18
|
+
|
19
|
+
def probe(_env)
|
20
|
+
@delayed_job.enqueue(HealthCheckJob).destroy!
|
21
|
+
probe_ok
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HttpHealthCheck
|
4
|
+
module Probes
|
5
|
+
class Sidekiq
|
6
|
+
include ::HttpHealthCheck::Probe
|
7
|
+
|
8
|
+
TTL_SEC = 3
|
9
|
+
MAGIC_NUMBER = 42
|
10
|
+
|
11
|
+
def initialize(sidekiq: ::Sidekiq)
|
12
|
+
@sidekiq_module = sidekiq
|
13
|
+
end
|
14
|
+
|
15
|
+
def probe(_env)
|
16
|
+
@sidekiq_module.redis do |conn|
|
17
|
+
conn.setex(meta[:redis_key], TTL_SEC, MAGIC_NUMBER)
|
18
|
+
probe_ok
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def meta
|
23
|
+
@meta ||= { redis_key: redis_key }
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def redis_key
|
29
|
+
@redis_key ||= ['sidekiq-healthcheck', ::Socket.gethostname, ::Process.pid].join('::')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
module HttpHealthCheck
|
7
|
+
class RackApp
|
8
|
+
HEADERS = { 'Content-Type' => 'application/json' }.freeze
|
9
|
+
DEFAULT_FALLBACK_APP = ->(_env) { [404, HEADERS, ['{"error": "not_found"}']] }.freeze
|
10
|
+
LIVENESS_CHECK_APP = ->(_env) { [200, HEADERS, ["{}\n"]] }
|
11
|
+
|
12
|
+
def self.configure
|
13
|
+
config = Config::Dsl.new
|
14
|
+
yield config
|
15
|
+
|
16
|
+
fallback_app = config.configured_fallback_app || DEFAULT_FALLBACK_APP
|
17
|
+
new(config.routes, fallback_app: fallback_app, logger: config.configured_logger)
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(routes, fallback_app: DEFAULT_FALLBACK_APP, logger: nil)
|
21
|
+
@logger = logger || Logger.new(IO::NULL, level: Logger::Severity::UNKNOWN)
|
22
|
+
@fallback_app = ensure_callable!(fallback_app)
|
23
|
+
@routes = routes.each_with_object('/liveness' => LIVENESS_CHECK_APP) do |(path, handler), acc|
|
24
|
+
acc[path.to_s] = ensure_callable!(handler)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
attr_reader :routes, :fallback_app, :logger
|
28
|
+
|
29
|
+
def call(env)
|
30
|
+
result = routes.fetch(env[Rack::REQUEST_PATH], fallback_app).call(env)
|
31
|
+
return result unless result.is_a?(Probe::Result)
|
32
|
+
|
33
|
+
[result.ok? ? 200 : 500, HEADERS, [result.meta.to_json]]
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def ensure_callable!(obj)
|
39
|
+
return obj if obj.respond_to?(:call)
|
40
|
+
|
41
|
+
raise ::HttpHealthCheck::ConfigurationError, 'HTTP handler must be callable'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rack'
|
4
|
+
|
5
|
+
require_relative 'http_health_check/version'
|
6
|
+
require_relative 'http_health_check/config/dsl'
|
7
|
+
require_relative 'http_health_check/probe'
|
8
|
+
require_relative 'http_health_check/rack_app'
|
9
|
+
require_relative 'http_health_check/probes'
|
10
|
+
|
11
|
+
module HttpHealthCheck
|
12
|
+
class Error < StandardError; end
|
13
|
+
|
14
|
+
class ConfigurationError < Error; end
|
15
|
+
|
16
|
+
def self.configure(&block)
|
17
|
+
@rack_app = RackApp.configure(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.rack_app
|
21
|
+
@rack_app ||= RackApp.configure { |c| add_builtin_probes(c) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.add_builtin_probes(conf)
|
25
|
+
if defined?(::Sidekiq)
|
26
|
+
require_relative 'http_health_check/probes/sidekiq' unless defined?(Probes::Sidekiq)
|
27
|
+
conf.probe '/readiness/sidekiq', Probes::Sidekiq.new
|
28
|
+
end
|
29
|
+
|
30
|
+
if defined?(::Delayed::Job)
|
31
|
+
require_relative 'http_health_check/probes/delayed_job' unless defined?(Probes::DelayedJob)
|
32
|
+
conf.probe '/readiness/delayed_job', Probes::DelayedJob.new
|
33
|
+
end
|
34
|
+
|
35
|
+
conf
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.run_server_async(opts)
|
39
|
+
Thread.new { run_server(opts) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.run_server(port:, host: '0.0.0.0', rack_app: nil)
|
43
|
+
rack_app ||= ::HttpHealthCheck.rack_app
|
44
|
+
rack_app_with_logger = ::Rack::CommonLogger.new(rack_app, rack_app.logger)
|
45
|
+
|
46
|
+
::Rack::Handler::WEBrick.run(rack_app_with_logger,
|
47
|
+
Host: host,
|
48
|
+
Port: port,
|
49
|
+
AccessLog: [],
|
50
|
+
Logger: rack_app.logger)
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: http_health_check
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SberMarket team
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dotenv
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.7.6
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.7.6
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: redis
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.2.5
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.2.5
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.81'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.81'
|
83
|
+
description: Simple and extensible HTTP health checks server.
|
84
|
+
email:
|
85
|
+
- pochi.73@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".bumpversion.cfg"
|
91
|
+
- ".env"
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".rubocop.yml"
|
95
|
+
- ".tool-versions"
|
96
|
+
- CHANGELOG.md
|
97
|
+
- Gemfile
|
98
|
+
- Gemfile.lock
|
99
|
+
- LICENSE
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- bin/console
|
103
|
+
- bin/setup
|
104
|
+
- docker-compose.yml
|
105
|
+
- http_health_check.gemspec
|
106
|
+
- lib/http_health_check.rb
|
107
|
+
- lib/http_health_check/config/dsl.rb
|
108
|
+
- lib/http_health_check/probe.rb
|
109
|
+
- lib/http_health_check/probe/result.rb
|
110
|
+
- lib/http_health_check/probes.rb
|
111
|
+
- lib/http_health_check/probes/delayed_job.rb
|
112
|
+
- lib/http_health_check/probes/sidekiq.rb
|
113
|
+
- lib/http_health_check/rack_app.rb
|
114
|
+
- lib/http_health_check/version.rb
|
115
|
+
homepage: https://github.com/SberMarket-Tech/http_health_check
|
116
|
+
licenses: []
|
117
|
+
metadata:
|
118
|
+
allowed_push_host: https://rubygems.org/
|
119
|
+
homepage_uri: https://github.com/SberMarket-Tech/http_health_check
|
120
|
+
source_code_uri: https://github.com/SberMarket-Tech/http_health_check
|
121
|
+
changelog_uri: https://github.com/SberMarket-Tech/http_health_check/blob/main/CHANGELOG.md
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 2.5.0
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubygems_version: 3.2.3
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Simple and extensible HTTP health checks server.
|
141
|
+
test_files: []
|