healthy_rack 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 71fceaea319b30fb1a666c4fec1b2fb09cef701e2ed5aba1498747c9a72c6889
4
+ data.tar.gz: 5a4283544f58159dbc7295d39ff76637dcc5805b2223b7d66b8ee52e647577e0
5
+ SHA512:
6
+ metadata.gz: 83bc126ecc8717a498cf1e4cf960a271d8b798ef15cea196c7701551348ccb0248b378d227dd09c332737600a12692f6ffeb8f269e638d8ca9ddd2f1e88ba07f
7
+ data.tar.gz: 1e75b8d0133c2e8a174cae13a672e5df5fd09e7622f482ad66d311acf7806c3fddc3e94b72a98b3ab0c851f79c7e1e05f12906ddcc00d854e6492c0b917487b9
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ *.gem
11
+
12
+ # MacOS
13
+ .DS_Store
14
+
15
+ # rspec failure tracking
16
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'vendor/**/*'
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+
8
+ Style/ClassAndModuleChildren:
9
+ Enabled: false
10
+
11
+ Style/RescueStandardError:
12
+ EnforcedStyle: implicit
13
+
14
+ Style/StringLiterals:
15
+ Enabled: false
16
+
17
+ Style/FrozenStringLiteralComment:
18
+ Enabled: false
19
+
20
+ StringLiterals:
21
+ EnforcedStyle: single_quotes
22
+
23
+ Metrics/LineLength:
24
+ Max: 100
25
+
26
+ Metrics/BlockLength:
27
+ Exclude:
28
+ - '**/*_spec.rb'
29
+ - '*.gemspec'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.0
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ sudo: required
2
+
3
+ language: ruby
4
+
5
+ services:
6
+ - docker
7
+
8
+ before_install:
9
+ docker build -t healthy_rack .
10
+
11
+ script:
12
+ - docker run healthy_rack:latest bundle exec rake
data/Dockerfile ADDED
@@ -0,0 +1,4 @@
1
+ FROM ruby:2.5.0
2
+ WORKDIR /healthy_rack
3
+ COPY . ./
4
+ RUN bundle install
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in healthy_rack.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # HealthyRack
2
+
3
+ A simple Rack middleware/application for checking health state.
4
+
5
+ ### Disclaimer
6
+
7
+ This gem was made to fulfill personal needs and for fun. Hasn't been tested in production (yet?).
8
+
9
+ ### Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'healthy_rack'
15
+ ```
16
+
17
+ ### Configuration
18
+
19
+ *HealthyRack* comes with several built in checks available:
20
+
21
+ ```ruby
22
+ HealthyRack::Checks::Redis
23
+ HealthyRack::Checks::ActiveRecord
24
+ HealthyRack::Checks::Sequel
25
+ HealthyRack::Checks::Sidekiq
26
+ ```
27
+
28
+ By default all of them are disabled.
29
+
30
+ Also you can easily add custom check. Just provide a subclass of `HealthyRack::Check` or any object with similar interface.
31
+
32
+ **Example configuration:**
33
+
34
+ ```ruby
35
+ HealthyRack.configure do |config|
36
+ config.add_checks(
37
+ HealthyRack::Checks::ActiveRecord.new,
38
+ HealthyRack::Checks::Redis.new('redis://redis:7379/0')
39
+ )
40
+ end
41
+ ```
42
+
43
+ ### Usage as middleware
44
+
45
+ Just add `HealthyRack.middleware` to middleware stack of your rack application:
46
+
47
+ ```ruby
48
+ use HealthyRack.middleware, path: '/health'
49
+ ```
50
+
51
+ where `/health` is desired path.
52
+
53
+ ### Mounting as rack application
54
+
55
+ Mount`HealthyRack.app` at your router:
56
+
57
+ ```ruby
58
+ Rack::URLMap.new('/health' => HealthyRack.app)
59
+ ```
60
+
61
+ ### Similar projects
62
+ You may be interested in other projects such as:
63
+
64
+ * [Pinglish](https://github.com/jbarnette/pinglish)
65
+ * [OK Computer](https://github.com/sportngin/okcomputer)
66
+ * [Healthcheck](https://github.com/packethost/healthcheck-rb)
67
+
68
+ Thanks for their authors for the inspiration :)
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new(:rubocop)
7
+
8
+ task default: %i[rubocop spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'healthy_rack'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'healthy_rack/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'healthy_rack'
8
+ spec.version = HealthyRack::VERSION
9
+ spec.authors = ['Konstantin Nizhegorodov']
10
+ spec.email = ['k.nizhegorodov@gmail.com']
11
+
12
+ spec.summary = 'Rack middleware for checking health status.'
13
+ spec.description = "Checks that application's database, sidekiq etc are responding."
14
+ spec.homepage = 'https://github.com/konstantinn/healthy_rack'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.16'
24
+ spec.add_development_dependency 'pry-byebug', '~> 3.6'
25
+ spec.add_development_dependency 'rack-test', '~> 0.8'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0.53'
29
+ end
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+
3
+ module HealthyRack
4
+ class App
5
+ HEADERS = {
6
+ 'Content-Type' => 'application/json; charset=UTF-8'
7
+ }.freeze
8
+
9
+ class << self
10
+ def call(_env)
11
+ failed_checks = HealthyRack.config.checks.reject(&:call)
12
+ if failed_checks.any?
13
+ [500, HEADERS.dup, [error_response_body(failed_checks)]]
14
+ else
15
+ [200, HEADERS.dup, [successful_response_body]]
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def successful_response_body
22
+ JSON.generate(healthy: true)
23
+ end
24
+
25
+ def error_response_body(failed_checks)
26
+ JSON.generate(
27
+ healthy: false,
28
+ failed_checks: failed_checks.map(&:name)
29
+ )
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,28 @@
1
+ require 'healthy_rack/utils'
2
+
3
+ module HealthyRack
4
+ class Check
5
+ def verify; end
6
+
7
+ def name
8
+ class_name = self.class.name
9
+ raise NotImplementedError if class_name.nil? # Anonymous Classes
10
+
11
+ demodulized = Utils::String.demodulize(class_name)
12
+ Utils::String.underscore(demodulized)
13
+ end
14
+
15
+ def call
16
+ raise NotImplementedError
17
+ end
18
+
19
+ def ==(other)
20
+ self.class == other.class && name == other.name
21
+ end
22
+ alias eql? ==
23
+
24
+ def hash
25
+ name.hash
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ require 'healthy_rack/check'
2
+ require 'healthy_rack/verify_class_definition'
3
+
4
+ module HealthyRack
5
+ module Checks
6
+ class ActiveRecord < Check
7
+ def verify
8
+ VerifyClassDefinition.call('ActiveRecord')
9
+ end
10
+
11
+ def call
12
+ ::ActiveRecord::Migrator.current_version
13
+ true
14
+ rescue
15
+ false
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ require 'healthy_rack/check'
2
+ require 'healthy_rack/verify_class_definition'
3
+
4
+ module HealthyRack
5
+ module Checks
6
+ class Redis < Check
7
+ def initialize(redis_url)
8
+ @redis_url = redis_url
9
+ end
10
+
11
+ def verify
12
+ VerifyClassDefinition.call('Redis')
13
+ end
14
+
15
+ def call
16
+ response = ::Redis.new(redis_url).ping
17
+ response == 'PONG'
18
+ rescue
19
+ false
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :redis_url
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'healthy_rack/check'
2
+ require 'healthy_rack/verify_class_definition'
3
+
4
+ module HealthyRack
5
+ module Checks
6
+ class Sequel < Check
7
+ def verify
8
+ VerifyClassDefinition.call('Sequel')
9
+ end
10
+
11
+ def call
12
+ ::Sequel::Model.db.select(nil).first
13
+ true
14
+ rescue
15
+ false
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'healthy_rack/check'
2
+ require 'healthy_rack/verify_class_definition'
3
+
4
+ module HealthyRack
5
+ module Checks
6
+ class Sidekiq < Check
7
+ def verify
8
+ VerifyClassDefinition.call('Sidekiq')
9
+ end
10
+
11
+ def call
12
+ ::Sidekiq.redis_info
13
+ true
14
+ rescue
15
+ false
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ require 'set'
2
+
3
+ module HealthyRack
4
+ class Configuration
5
+ attr_reader :checks
6
+
7
+ def initialize
8
+ setup
9
+ end
10
+
11
+ def reset
12
+ setup
13
+ end
14
+
15
+ def add_checks(*checks_to_add)
16
+ checks_to_add.each do |check|
17
+ check.verify
18
+ checks.add(check)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def setup
25
+ @checks = Set.new
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ module HealthyRack
2
+ class Errors
3
+ UndefinedClassError = Class.new(StandardError)
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ require 'healthy_rack/app'
2
+
3
+ module HealthyRack
4
+ class Middleware
5
+ def initialize(app, path:)
6
+ @app = app
7
+ @path = path
8
+ end
9
+
10
+ def call(env)
11
+ request = Rack::Request.new(env)
12
+ if request.path_info == path
13
+ App.call(env)
14
+ else
15
+ app.call(env)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :app, :path
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ module HealthyRack
2
+ module Utils
3
+ # https://github.com/hanami/utils/blob/v1.1.2/lib/hanami/utils/string.rb
4
+ class String
5
+ NAMESPACE_SEPARATOR = '::'.freeze
6
+ UNDERSCORE_SEPARATOR = '/'.freeze
7
+ UNDERSCORE_DIVISION_TARGET = '\1_\2'.freeze
8
+
9
+ def self.demodulize(input)
10
+ ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).last
11
+ end
12
+
13
+ def self.underscore(input)
14
+ string = ::String.new(input.to_s)
15
+ string.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
16
+ string.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
17
+ string.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
18
+ string.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
19
+ string.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
20
+ string.downcase
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ module HealthyRack
2
+ class VerifyClassDefinition
3
+ def self.call(claz)
4
+ Object.const_get(claz)
5
+ rescue NameError
6
+ raise Errors::UndefinedClassError,
7
+ "#{claz} is not defined. Is corresponding gem installed?"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module HealthyRack
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,33 @@
1
+ require 'healthy_rack/middleware'
2
+ require 'healthy_rack/app'
3
+ require 'healthy_rack/configuration'
4
+ require 'healthy_rack/errors'
5
+
6
+ require 'healthy_rack/checks/active_record'
7
+ require 'healthy_rack/checks/redis'
8
+ require 'healthy_rack/checks/sidekiq'
9
+ require 'healthy_rack/checks/sequel'
10
+
11
+ module HealthyRack
12
+ class << self
13
+ def middleware
14
+ Middleware
15
+ end
16
+
17
+ def app
18
+ App
19
+ end
20
+
21
+ def config
22
+ @config ||= Configuration.new
23
+ end
24
+
25
+ def reset_config
26
+ config.reset
27
+ end
28
+
29
+ def configure
30
+ yield(config)
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: healthy_rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Konstantin Nizhegorodov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.53'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.53'
97
+ description: Checks that application's database, sidekiq etc are responding.
98
+ email:
99
+ - k.nizhegorodov@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - Dockerfile
110
+ - Gemfile
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - healthy_rack.gemspec
116
+ - lib/healthy_rack.rb
117
+ - lib/healthy_rack/app.rb
118
+ - lib/healthy_rack/check.rb
119
+ - lib/healthy_rack/checks/active_record.rb
120
+ - lib/healthy_rack/checks/redis.rb
121
+ - lib/healthy_rack/checks/sequel.rb
122
+ - lib/healthy_rack/checks/sidekiq.rb
123
+ - lib/healthy_rack/configuration.rb
124
+ - lib/healthy_rack/errors.rb
125
+ - lib/healthy_rack/middleware.rb
126
+ - lib/healthy_rack/utils.rb
127
+ - lib/healthy_rack/verify_class_definition.rb
128
+ - lib/healthy_rack/version.rb
129
+ homepage: https://github.com/konstantinn/healthy_rack
130
+ licenses: []
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.7.3
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Rack middleware for checking health status.
152
+ test_files: []