health_bit 0.1.2
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/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +139 -0
- data/.travis.yml +14 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +66 -0
- data/LICENSE.txt +21 -0
- data/README.md +188 -0
- data/Rakefile +8 -0
- data/bin/console +7 -0
- data/bin/release.sh +5 -0
- data/bin/setup +8 -0
- data/doc/logo.png +0 -0
- data/health_bit.gemspec +27 -0
- data/lib/health_bit.rb +86 -0
- data/lib/health_bit/check.rb +20 -0
- data/lib/health_bit/check_error.rb +37 -0
- data/lib/health_bit/version.rb +5 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2ab8c2587fcec36f8180678e9beb091f651e537271a7deef99b84b02cd79711d
|
4
|
+
data.tar.gz: 2f029f22d8a690e6b0c14d632abcfdb9139b2c38d7080bbb25bc58eb2097f427
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0eab6fb26e62a424b63d5b20a10859b1a2aaa087b358bb30b258e8a2affea605c652a18ef64f5fc6ea40b395fa7c9d157c99b0ed99d7d3277c81883f51804989
|
7
|
+
data.tar.gz: da6cde8e825a987c5cfc06c7759e49cdad9716264880202b1ee34344cc4a458075ab732880a607d1bc415d1391a0caf924c3877d1d70e245fac9eeeeb8a3a102
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
AutoCorrect: false
|
6
|
+
Exclude:
|
7
|
+
- 'click_house.gemspec'
|
8
|
+
- 'bin/*'
|
9
|
+
- 'spec/**/*'
|
10
|
+
- 'vendor/**/*'
|
11
|
+
TargetRubyVersion: 2.7.1
|
12
|
+
|
13
|
+
Bundler/OrderedGems:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
# ============================== Documentation ======================
|
17
|
+
Style/Documentation:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
# ============================== Metrics ============================
|
21
|
+
Metrics/ClassLength:
|
22
|
+
Max: 180
|
23
|
+
Metrics/BlockLength:
|
24
|
+
Enabled: true
|
25
|
+
Metrics/MethodLength:
|
26
|
+
Max: 25
|
27
|
+
Metrics/AbcSize:
|
28
|
+
Max: 40
|
29
|
+
|
30
|
+
# ============================== Naming =============================
|
31
|
+
Naming/PredicateName:
|
32
|
+
ForbiddenPrefixes:
|
33
|
+
- is_
|
34
|
+
Naming/FileName:
|
35
|
+
Enabled: true
|
36
|
+
Exclude:
|
37
|
+
- 'Gemfile'
|
38
|
+
Naming/MethodParameterName:
|
39
|
+
Enabled: false
|
40
|
+
Naming/AccessorMethodName:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
# ============================== Layout =============================
|
44
|
+
Layout/HashAlignment:
|
45
|
+
EnforcedHashRocketStyle: key
|
46
|
+
EnforcedColonStyle: key
|
47
|
+
Layout/ParameterAlignment:
|
48
|
+
EnforcedStyle: with_fixed_indentation
|
49
|
+
Layout/CaseIndentation:
|
50
|
+
EnforcedStyle: case
|
51
|
+
IndentOneStep: false
|
52
|
+
Layout/MultilineMethodCallIndentation:
|
53
|
+
Enabled: true
|
54
|
+
EnforcedStyle: indented
|
55
|
+
Layout/SpaceBeforeBlockBraces:
|
56
|
+
EnforcedStyle: space
|
57
|
+
EnforcedStyleForEmptyBraces: space
|
58
|
+
Layout/EmptyLines:
|
59
|
+
Enabled: true
|
60
|
+
Layout/EmptyLineAfterMagicComment:
|
61
|
+
Enabled: false
|
62
|
+
Layout/EmptyLinesAroundBlockBody:
|
63
|
+
Enabled: true
|
64
|
+
Layout/EndAlignment:
|
65
|
+
EnforcedStyleAlignWith: variable
|
66
|
+
Layout/FirstHashElementIndentation:
|
67
|
+
EnforcedStyle: consistent
|
68
|
+
Layout/HeredocIndentation:
|
69
|
+
Enabled: false
|
70
|
+
Layout/RescueEnsureAlignment:
|
71
|
+
Enabled: false
|
72
|
+
Layout/SpaceAroundMethodCallOperator:
|
73
|
+
Enabled: true
|
74
|
+
Layout/LineLength:
|
75
|
+
Max: 140
|
76
|
+
|
77
|
+
# ============================== Style ==============================
|
78
|
+
Style/RescueModifier:
|
79
|
+
Enabled: true
|
80
|
+
Style/PercentLiteralDelimiters:
|
81
|
+
PreferredDelimiters:
|
82
|
+
default: '[]'
|
83
|
+
'%i': '[]'
|
84
|
+
'%w': '[]'
|
85
|
+
Exclude:
|
86
|
+
- 'config/routes.rb'
|
87
|
+
Style/StringLiterals:
|
88
|
+
Enabled: true
|
89
|
+
Style/AsciiComments:
|
90
|
+
Enabled: false
|
91
|
+
Style/Copyright:
|
92
|
+
Enabled: false
|
93
|
+
Style/SafeNavigation:
|
94
|
+
Enabled: false
|
95
|
+
Style/Lambda:
|
96
|
+
Enabled: false
|
97
|
+
Style/Alias:
|
98
|
+
Enabled: true
|
99
|
+
EnforcedStyle: prefer_alias_method
|
100
|
+
Style/ClassAndModuleChildren:
|
101
|
+
Enabled: true
|
102
|
+
EnforcedStyle: nested
|
103
|
+
Style/TrailingCommaInArrayLiteral:
|
104
|
+
Enabled: true
|
105
|
+
EnforcedStyleForMultiline: no_comma
|
106
|
+
Style/RescueStandardError:
|
107
|
+
Enabled: true
|
108
|
+
EnforcedStyle: explicit
|
109
|
+
Style/InverseMethods:
|
110
|
+
AutoCorrect: false
|
111
|
+
Enabled: true
|
112
|
+
Style/IfUnlessModifier:
|
113
|
+
Enabled: false
|
114
|
+
Style/SpecialGlobalVars:
|
115
|
+
Enabled: false
|
116
|
+
Style/BlockComments:
|
117
|
+
Enabled: false
|
118
|
+
Style/GuardClause:
|
119
|
+
Enabled: false
|
120
|
+
Style/TrailingCommaInHashLiteral:
|
121
|
+
Enabled: false
|
122
|
+
Style/ExponentialNotation:
|
123
|
+
Enabled: true
|
124
|
+
Style/HashEachMethods:
|
125
|
+
Enabled: true
|
126
|
+
Style/HashTransformKeys:
|
127
|
+
Enabled: true
|
128
|
+
Style/HashTransformValues:
|
129
|
+
Enabled: true
|
130
|
+
|
131
|
+
# ============================== Lint ==============================
|
132
|
+
Lint/DuplicateMethods:
|
133
|
+
Enabled: false
|
134
|
+
Lint/AmbiguousOperator:
|
135
|
+
Enabled: false
|
136
|
+
Lint/RaiseException:
|
137
|
+
Enabled: true
|
138
|
+
Lint/StructNewOverride:
|
139
|
+
Enabled: true
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
ruby '2.7.1'
|
5
|
+
|
6
|
+
# Specify your gem's dependencies in health_bit.gemspec
|
7
|
+
gemspec
|
8
|
+
|
9
|
+
gem 'rake', '~> 13.0'
|
10
|
+
gem 'rspec', '~> 3.0'
|
11
|
+
gem 'pry'
|
12
|
+
gem 'rubocop'
|
13
|
+
gem 'rubocop-performance'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
health_bit (0.1.2)
|
5
|
+
rack
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.0)
|
11
|
+
coderay (1.1.2)
|
12
|
+
diff-lcs (1.3)
|
13
|
+
jaro_winkler (1.5.4)
|
14
|
+
method_source (1.0.0)
|
15
|
+
parallel (1.19.1)
|
16
|
+
parser (2.7.1.2)
|
17
|
+
ast (~> 2.4.0)
|
18
|
+
pry (0.13.1)
|
19
|
+
coderay (~> 1.1)
|
20
|
+
method_source (~> 1.0)
|
21
|
+
rack (2.2.2)
|
22
|
+
rainbow (3.0.0)
|
23
|
+
rake (13.0.1)
|
24
|
+
rexml (3.2.4)
|
25
|
+
rspec (3.9.0)
|
26
|
+
rspec-core (~> 3.9.0)
|
27
|
+
rspec-expectations (~> 3.9.0)
|
28
|
+
rspec-mocks (~> 3.9.0)
|
29
|
+
rspec-core (3.9.2)
|
30
|
+
rspec-support (~> 3.9.3)
|
31
|
+
rspec-expectations (3.9.1)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.9.0)
|
34
|
+
rspec-mocks (3.9.1)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.9.0)
|
37
|
+
rspec-support (3.9.3)
|
38
|
+
rubocop (0.82.0)
|
39
|
+
jaro_winkler (~> 1.5.1)
|
40
|
+
parallel (~> 1.10)
|
41
|
+
parser (>= 2.7.0.1)
|
42
|
+
rainbow (>= 2.2.2, < 4.0)
|
43
|
+
rexml
|
44
|
+
ruby-progressbar (~> 1.7)
|
45
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
46
|
+
rubocop-performance (1.5.2)
|
47
|
+
rubocop (>= 0.71.0)
|
48
|
+
ruby-progressbar (1.10.1)
|
49
|
+
unicode-display_width (1.7.0)
|
50
|
+
|
51
|
+
PLATFORMS
|
52
|
+
ruby
|
53
|
+
|
54
|
+
DEPENDENCIES
|
55
|
+
health_bit!
|
56
|
+
pry
|
57
|
+
rake (~> 13.0)
|
58
|
+
rspec (~> 3.0)
|
59
|
+
rubocop
|
60
|
+
rubocop-performance
|
61
|
+
|
62
|
+
RUBY VERSION
|
63
|
+
ruby 2.7.1p83
|
64
|
+
|
65
|
+
BUNDLED WITH
|
66
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Aliaksandr Shylau
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
[](https://travis-ci.com/shlima/click_house)
|
2
|
+
|
3
|
+
# HealthBit
|
4
|
+
|
5
|
+

|
6
|
+
|
7
|
+
This gem was inspired by the [health_check](https://github.com/ianheggie/health_check), but is simpler and more
|
8
|
+
extensible and contains up to 95% less code.
|
9
|
+
|
10
|
+
Key differences:
|
11
|
+
* is a rack application
|
12
|
+
* can be used with rails, sinatra or any other rack application
|
13
|
+
* can add custom checks
|
14
|
+
* can add multiple endpoints with independent checks
|
15
|
+
* can use any Rack middleware (http authorization, ip address check)
|
16
|
+
|
17
|
+
## Toc
|
18
|
+
|
19
|
+
* [Installation](#installation)
|
20
|
+
* [Configuration](#configuration)
|
21
|
+
* [Add Checks](#add-checks)
|
22
|
+
* [Add a Route](#add-a-route)
|
23
|
+
* [Password Protection](#password-protection)
|
24
|
+
* [Database check](#database-check)
|
25
|
+
* [Redis check](#redis-check)
|
26
|
+
* [Rails cache check](#rails-cache-check)
|
27
|
+
* [Elasticsearch check](#elasticsearch-check)
|
28
|
+
* [Multiple endpoints](#multiple-endpoints)
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
|
32
|
+
Add this line to your application's Gemfile:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
gem 'health_bit'
|
36
|
+
```
|
37
|
+
|
38
|
+
## Configuration
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
# config/initializers/health_bit.rb
|
42
|
+
# DEFAULT SETTINGS ARE SHOWN BELOW
|
43
|
+
HealthBit.configure do |c|
|
44
|
+
c.success_text = '%<count>d checks passed 🎉'
|
45
|
+
c.headers = {
|
46
|
+
'Content-Type' => 'text/plain',
|
47
|
+
'Cache-Control' => 'private,max-age=0,must-revalidate,no-store'
|
48
|
+
}
|
49
|
+
c.success_code = 200
|
50
|
+
c.fail_code = 500
|
51
|
+
c.show_backtrace = false
|
52
|
+
|
53
|
+
c.add('Check name') do
|
54
|
+
# Body check, should returns `true`
|
55
|
+
true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
## Add Checks
|
61
|
+
|
62
|
+
By default, the **gem does not contain any checks**, **you should add the
|
63
|
+
necessary checks by yourself**. The check should return `false` or `nil`
|
64
|
+
to be considered unsuccessful or throw an exception, any other
|
65
|
+
values are considered satisfactory.
|
66
|
+
|
67
|
+
Example checks:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
## Successful checks
|
71
|
+
HealthBit.add('PostgreSQL') do
|
72
|
+
ApplicationRecord.connection.select_value('SELECT 1') == 1
|
73
|
+
end
|
74
|
+
|
75
|
+
HealthBit.add('Custom') do
|
76
|
+
true
|
77
|
+
end
|
78
|
+
|
79
|
+
## Failed checks
|
80
|
+
HealthBit.add('Database') do
|
81
|
+
false
|
82
|
+
end
|
83
|
+
|
84
|
+
HealthBit.add('Docker service') do
|
85
|
+
raise 'not responding'
|
86
|
+
end
|
87
|
+
|
88
|
+
# The Check can be added as an object responding to a call
|
89
|
+
class Covid19Check
|
90
|
+
def self.call
|
91
|
+
true
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
HealthBit.add('COVID-19 Checker', Covid19Check)
|
96
|
+
```
|
97
|
+
|
98
|
+
## Add a Route
|
99
|
+
|
100
|
+
Since the gem is a rack application, you must mount it to app's
|
101
|
+
routes. Below is an example for the Rails.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
# config/routes.rb
|
105
|
+
|
106
|
+
Rails.application.routes.draw do
|
107
|
+
mount HealthBit.rack => '/health'
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
## Password Protection
|
112
|
+
|
113
|
+
Since the gem is a common rack application, you can add any rack
|
114
|
+
middleware. Below is an example with HTTP-auth for the Rails.
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
# config/routes.rb
|
118
|
+
|
119
|
+
Rails.application.routes.draw do
|
120
|
+
HealthBit.rack.use Rack::Auth::Basic do |username, password|
|
121
|
+
ActiveSupport::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(username), ::Digest::SHA256.hexdigest('user')) & ActiveSupport::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(password), ::Digest::SHA256.hexdigest('password'))
|
122
|
+
end
|
123
|
+
|
124
|
+
mount HealthBit.rack => '/health'
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
## Database check
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
HealthBit.add('Database') do
|
132
|
+
ApplicationRecord.connection.select_value('SELECT 1') == 1
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
## Redis check
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
HealthBit.add('Redis') do
|
140
|
+
Redis.current.ping == 'PONG'
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
## Rails cache check
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
HealthBit.add('Rails cache') do
|
148
|
+
Rails.cache.read('1').nil?
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
## Elasticsearch check
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
HealthBit.add('Elasticsearch') do
|
156
|
+
Elasticsearch::Client.new.ping
|
157
|
+
end
|
158
|
+
```
|
159
|
+
|
160
|
+
## Multiple endpoints
|
161
|
+
|
162
|
+
Sometimes you have to add several health check endpoints. Let's say
|
163
|
+
you have to check the docker container health and the health
|
164
|
+
of your application as a whole. Below is an example for rails.
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
# config/initializers/health_bit.rb
|
168
|
+
|
169
|
+
DockerCheck = HealthBit.clone
|
170
|
+
AppCheck = HealthBit.clone
|
171
|
+
|
172
|
+
DockerCheck.add('Docker Health') do
|
173
|
+
true
|
174
|
+
end
|
175
|
+
|
176
|
+
AppCheck.add('App Health') do
|
177
|
+
ApplicationRecord.connection.select_value("SELECT 't'::boolean")
|
178
|
+
end
|
179
|
+
```
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
# config/routes.rb
|
183
|
+
|
184
|
+
Rails.application.routes.draw do
|
185
|
+
mount DockerCheck.rack => '/docker'
|
186
|
+
mount AppCheck.rack => '/app'
|
187
|
+
end
|
188
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/release.sh
ADDED
data/bin/setup
ADDED
data/doc/logo.png
ADDED
Binary file
|
data/health_bit.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/health_bit/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'health_bit'
|
7
|
+
spec.version = HealthBit::VERSION
|
8
|
+
spec.authors = ['Aliaksandr Shylau']
|
9
|
+
spec.email = %w[alex.shilov.by@gmail.com]
|
10
|
+
|
11
|
+
spec.summary = 'Tiny health check of Rack apps like Rails, Sinatra'
|
12
|
+
spec.description = 'Tiny health check of Rack apps like Rails, Sinatra for use with uptime checking systems like Kubernetes, Docker or Uptimerobot'
|
13
|
+
spec.homepage = 'https://github.com/shlima/health_bit'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
18
|
+
spec.metadata['changelog_uri'] = 'https://github.com/shlima/health_bit/releases'
|
19
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = %w[lib]
|
25
|
+
|
26
|
+
spec.add_dependency 'rack'
|
27
|
+
end
|
data/lib/health_bit.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rack'
|
4
|
+
require 'health_bit/version'
|
5
|
+
|
6
|
+
module HealthBit
|
7
|
+
DEFAULT_SUCCESS_TEXT = '%<count>d checks passed 🎉'
|
8
|
+
DEFAULT_HEADERS = {
|
9
|
+
'Content-Type' => 'text/plain',
|
10
|
+
'Cache-Control' => 'private,max-age=0,must-revalidate,no-store'
|
11
|
+
}.freeze
|
12
|
+
DEFAULT_SUCCESS_CODE = 200
|
13
|
+
DEFAULT_FAIL_CODE = 500
|
14
|
+
|
15
|
+
autoload :Check, 'health_bit/check'
|
16
|
+
autoload :CheckError, 'health_bit/check_error'
|
17
|
+
|
18
|
+
extend self # rubocop:disable Style/ModuleFunction
|
19
|
+
|
20
|
+
attr_writer :success_text, :success_code, :fail_code, :headers
|
21
|
+
attr_accessor :show_backtrace
|
22
|
+
|
23
|
+
def success_text
|
24
|
+
format(@success_text || DEFAULT_SUCCESS_TEXT, count: checks.length)
|
25
|
+
end
|
26
|
+
|
27
|
+
def success_code
|
28
|
+
@success_code || DEFAULT_SUCCESS_CODE
|
29
|
+
end
|
30
|
+
|
31
|
+
def fail_code
|
32
|
+
@fail_code || DEFAULT_FAIL_CODE
|
33
|
+
end
|
34
|
+
|
35
|
+
def headers
|
36
|
+
@headers || DEFAULT_HEADERS
|
37
|
+
end
|
38
|
+
|
39
|
+
def checks
|
40
|
+
@checks ||= []
|
41
|
+
end
|
42
|
+
|
43
|
+
def configure
|
44
|
+
yield(self)
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [self]
|
48
|
+
def add(name, handler = nil, &block)
|
49
|
+
raise ArgumentError, <<~MSG if handler && block
|
50
|
+
Both <handler> and <block> were passed to the <#{name}> check
|
51
|
+
MSG
|
52
|
+
|
53
|
+
raise ArgumentError, <<~MSG unless handler || block
|
54
|
+
Not <handler> or <block> were passed to the <#{name}> check
|
55
|
+
MSG
|
56
|
+
|
57
|
+
checks.push(Check.new(name, handler || block))
|
58
|
+
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
# @return [nil, CheckError]
|
63
|
+
def check
|
64
|
+
checks.each do |check|
|
65
|
+
(exception = check.call).nil? ? next : (return exception)
|
66
|
+
end
|
67
|
+
|
68
|
+
nil
|
69
|
+
end
|
70
|
+
|
71
|
+
def rack(this = self)
|
72
|
+
@rack ||= begin
|
73
|
+
format = this.show_backtrace ? CheckError::FORMAT_FULL : CheckError::FORMAT_SHORT
|
74
|
+
|
75
|
+
Rack::Builder.new do
|
76
|
+
run ->(_env) do
|
77
|
+
if (error = this.check)
|
78
|
+
[this.fail_code, this.headers, [error.to_s(format)]]
|
79
|
+
else
|
80
|
+
[this.success_code, this.headers, [this.success_text]]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HealthBit
|
4
|
+
class Check
|
5
|
+
attr_reader :name, :handler
|
6
|
+
|
7
|
+
def initialize(name, handler)
|
8
|
+
@name = name
|
9
|
+
@handler = handler
|
10
|
+
end
|
11
|
+
|
12
|
+
# @return [nil] if its ok
|
13
|
+
# @return [CheckError] if not
|
14
|
+
def call
|
15
|
+
raise('The check has returned a negative value') unless handler.call
|
16
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
17
|
+
CheckError.new(name, exception: e)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HealthBit
|
4
|
+
class CheckError < StandardError
|
5
|
+
FORMAT_FULL = :full
|
6
|
+
FORMAT_SHORT = nil
|
7
|
+
|
8
|
+
attr_reader :name, :exception
|
9
|
+
|
10
|
+
def initialize(name, exception: nil)
|
11
|
+
@name = name
|
12
|
+
@exception = exception
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [String]
|
16
|
+
def to_s(format = nil)
|
17
|
+
io = StringIO.new
|
18
|
+
io.puts "Check <#{name}> failed"
|
19
|
+
|
20
|
+
case format
|
21
|
+
when FORMAT_FULL
|
22
|
+
describe_exception(io)
|
23
|
+
end
|
24
|
+
|
25
|
+
io.string
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def describe_exception(io)
|
31
|
+
return if exception.nil?
|
32
|
+
|
33
|
+
io.puts exception.inspect
|
34
|
+
io.puts exception.backtrace.join("\n")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: health_bit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aliaksandr Shylau
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-07 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Tiny health check of Rack apps like Rails, Sinatra for use with uptime
|
28
|
+
checking systems like Kubernetes, Docker or Uptimerobot
|
29
|
+
email:
|
30
|
+
- alex.shilov.by@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- ".rubocop.yml"
|
38
|
+
- ".travis.yml"
|
39
|
+
- Gemfile
|
40
|
+
- Gemfile.lock
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- bin/console
|
45
|
+
- bin/release.sh
|
46
|
+
- bin/setup
|
47
|
+
- doc/logo.png
|
48
|
+
- health_bit.gemspec
|
49
|
+
- lib/health_bit.rb
|
50
|
+
- lib/health_bit/check.rb
|
51
|
+
- lib/health_bit/check_error.rb
|
52
|
+
- lib/health_bit/version.rb
|
53
|
+
homepage: https://github.com/shlima/health_bit
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata:
|
57
|
+
homepage_uri: https://github.com/shlima/health_bit
|
58
|
+
source_code_uri: https://github.com/shlima/health_bit
|
59
|
+
changelog_uri: https://github.com/shlima/health_bit/releases
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.3.0
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.1.2
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Tiny health check of Rack apps like Rails, Sinatra
|
79
|
+
test_files: []
|