health_check_toolbox 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +84 -0
  3. data/Rakefile +6 -0
  4. data/config/locales/en.yml +6 -0
  5. data/config/routes.rb +3 -0
  6. data/lib/health_check_toolbox.rb +19 -0
  7. data/lib/health_check_toolbox/config.rb +39 -0
  8. data/lib/health_check_toolbox/engine.rb +6 -0
  9. data/lib/health_check_toolbox/health_check_routes.rb +15 -0
  10. data/lib/health_check_toolbox/health_controller.rb +18 -0
  11. data/lib/health_check_toolbox/services/base_health_check.rb +21 -0
  12. data/lib/health_check_toolbox/services/database.rb +22 -0
  13. data/lib/health_check_toolbox/services/redis_service.rb +21 -0
  14. data/lib/health_check_toolbox/services/rest_services.rb +74 -0
  15. data/lib/health_check_toolbox/services/wso2.rb +21 -0
  16. data/lib/health_check_toolbox/status_manager.rb +49 -0
  17. data/lib/health_check_toolbox/version.rb +5 -0
  18. data/spec/dummy/Rakefile +9 -0
  19. data/spec/dummy/bin/bundle +5 -0
  20. data/spec/dummy/bin/rails +6 -0
  21. data/spec/dummy/bin/rake +6 -0
  22. data/spec/dummy/bin/setup +37 -0
  23. data/spec/dummy/bin/update +32 -0
  24. data/spec/dummy/bin/yarn +13 -0
  25. data/spec/dummy/config.ru +7 -0
  26. data/spec/dummy/config/application.rb +14 -0
  27. data/spec/dummy/config/boot.rb +7 -0
  28. data/spec/dummy/config/cable.yml +10 -0
  29. data/spec/dummy/config/database.yml +16 -0
  30. data/spec/dummy/config/environment.rb +5 -0
  31. data/spec/dummy/config/environments/development.rb +32 -0
  32. data/spec/dummy/config/environments/production.rb +26 -0
  33. data/spec/dummy/config/environments/test.rb +25 -0
  34. data/spec/dummy/config/initializers/application_controller_renderer.rb +10 -0
  35. data/spec/dummy/config/initializers/assets.rb +5 -0
  36. data/spec/dummy/config/initializers/backtrace_silencers.rb +1 -0
  37. data/spec/dummy/config/initializers/content_security_policy.rb +1 -0
  38. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  39. data/spec/dummy/config/initializers/filter_parameter_logging.rb +3 -0
  40. data/spec/dummy/config/initializers/inflections.rb +1 -0
  41. data/spec/dummy/config/initializers/mime_types.rb +1 -0
  42. data/spec/dummy/config/initializers/wrap_parameters.rb +5 -0
  43. data/spec/dummy/config/puma.rb +10 -0
  44. data/spec/dummy/config/routes.rb +4 -0
  45. data/spec/dummy/config/spring.rb +8 -0
  46. data/spec/dummy/config/storage.yml +8 -0
  47. data/spec/dummy/package.json +5 -0
  48. data/spec/health_check_toolbox/config_spec.rb +87 -0
  49. data/spec/health_check_toolbox/health_controller_spec.rb +47 -0
  50. data/spec/health_check_toolbox/services/database_spec.rb +19 -0
  51. data/spec/health_check_toolbox/services/redis_service_spec.rb +27 -0
  52. data/spec/health_check_toolbox/services/rest_services_spec.rb +45 -0
  53. data/spec/health_check_toolbox/services/wso2_spec.rb +26 -0
  54. data/spec/health_check_toolbox/status_manager_spec.rb +78 -0
  55. data/spec/health_check_toolbox_spec.rb +22 -0
  56. data/spec/spec_helper.rb +20 -0
  57. metadata +263 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '08e0d29ece6bf2d0b6f18612857048f37d193a1a1248550cb80ef0bfe246e20d'
4
+ data.tar.gz: 8994b78e5e9bab353aaab7abcdef79062d769fe5efebc3cb165e9a5bca91ad4e
5
+ SHA512:
6
+ metadata.gz: 850010882c01d96ae0953b2c27950ceb232eb90080c4c84441e0d7e70d14219e6dbd77875a7c7858daf83a4c5d26a76bea0818ee498d75d9cbb071ce97ea4938
7
+ data.tar.gz: b86ae94c8f772b531a43279db58f24f18af934a0b4ffcaa1ff36f88adbea1f60a5ae9c0518a2451e7a65f2b6b82feb08163e2cd958d3f704a058b6721a6132bc
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # HealthCheckToolbox
2
+
3
+ A HealthCheckToolbox é uma gem para verificar o status e versão dos serviços da guide.
4
+
5
+ ## Installation
6
+ Adicione essa linha em seu gem file:
7
+
8
+ ```ruby
9
+ gem 'health_check_toolbox'
10
+ ```
11
+
12
+ Execute:
13
+ ```bash
14
+ $ bundle
15
+ ```
16
+
17
+ Ou instale você mesmo.
18
+
19
+ ```bash
20
+ $ gem install health_check_toolbox
21
+ ```
22
+
23
+ ## Configuração
24
+ Adicione no `config/initializers/` o file `health_check.rb` para configurar os serviços para verificação.
25
+
26
+ ```ruby
27
+ HealthCheckToolbox.setup do
28
+ end
29
+ ```
30
+
31
+ Ex.: configuração do projeto: `fund_data_service`
32
+ ```ruby
33
+ HealthCheckToolbox.setup do
34
+ redis_service true
35
+ database true
36
+ wso2 true
37
+ rest_services do
38
+ file_storage_service ENV['file_storage']
39
+ asset_classification_service ENV['asset_classification']
40
+ tema_service ENV['tema_ws']
41
+ end
42
+ end
43
+ ```
44
+
45
+ Configurações permitidas:
46
+
47
+ ```ruby
48
+ redis_service
49
+ ```
50
+ ```ruby
51
+ database
52
+ ```
53
+ ```ruby
54
+ wso2
55
+ ```
56
+ ```ruby
57
+ rest_services # validação de serviços externos ( é necessário passar "_service" nas opções )
58
+ ```
59
+
60
+ ## Como Usar
61
+
62
+ No arquivo `routes.rb` basta adicionar o seguinte código
63
+
64
+ ```ruby
65
+ health_check_routes
66
+ ```
67
+
68
+ Para verificar se as rotas foram incluídas no projeto rodar `rake routes`
69
+
70
+ A gem possui duas rotas:
71
+
72
+ ```ruby
73
+ /service_status
74
+ ```
75
+ Verifica a versão do projeto e o status do serviço.
76
+
77
+ ```ruby
78
+ /health_check
79
+ ```
80
+ Verifica o status de todos os serviços ligados ao projeto.
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ en:
2
+ time:
3
+ formats:
4
+ timestamp: '%Y-%m-%d %H:%M:%S %z'
5
+ timestamp_without_zone: '%Y-%m-%d %H:%M:%S'
6
+ short: '%Y-%m-%d'
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ health_check_routes
3
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'health_check_toolbox/version'
4
+ require 'health_check_toolbox/health_check_routes'
5
+ require 'health_check_toolbox/health_controller'
6
+ require 'health_check_toolbox/config'
7
+ require 'health_check_toolbox/status_manager'
8
+ require 'health_check_toolbox/engine'
9
+ require 'health_check_toolbox/services/redis_service'
10
+ require 'health_check_toolbox/services/database'
11
+ require 'health_check_toolbox/services/wso2'
12
+ require 'health_check_toolbox/services/rest_services'
13
+
14
+ module HealthCheckToolbox
15
+ def self.setup(&block)
16
+ config = Config.new(&block)
17
+ config.instance_eval(&block)
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckToolbox
4
+ class Config
5
+ @services = []
6
+
7
+ STATIC = %w[redis database wso2 rest_services].freeze
8
+ SERVICE = '_service'
9
+
10
+ class << self
11
+ attr_accessor :services
12
+
13
+ def add_service(service)
14
+ services.push(service)
15
+ end
16
+
17
+ def services_status
18
+ HealthCheckToolbox::StatusManager.generate(services)
19
+ end
20
+ end
21
+
22
+ def method_missing(method, *args, &block)
23
+ method_name = method.to_s
24
+
25
+ if respond_to_missing?(method_name)
26
+ config_class =
27
+ "HealthCheckToolbox::Services::#{method_name.camelize}".constantize
28
+ service = config_class.new(args, block)
29
+ self.class.add_service(service)
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ def respond_to_missing?(method_name)
36
+ STATIC.include?(method_name.to_s) || method_name.include?(SERVICE)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckToolbox
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionDispatch
4
+ module Routing
5
+ class Mapper
6
+ def health_check_routes
7
+ match 'health_check(/:checks)(.:format)', to:
8
+ 'health_check_toolbox/health#health_check', via: :get
9
+
10
+ match 'service_status(/:checks)(.:format)', to:
11
+ 'health_check_toolbox/health#service_status', via: :get
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckToolbox
4
+ class HealthController < ActionController::Base
5
+ def service_status
6
+ version = File.read('.version').chomp
7
+ render json: { ok: true, version: version }
8
+ end
9
+
10
+ def health_check
11
+ status = HealthCheckToolbox::Config.services_status
12
+ render json: {
13
+ timestamp: I18n.l(Time.current, format: :timestamp),
14
+ health_check: [status]
15
+ }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckToolbox
4
+ module Services
5
+ class BaseHealthCheck
6
+ attr_reader :args, :block
7
+
8
+ STATUS_OK = 'ok'
9
+ STATUS_ERROR = 'erro'
10
+
11
+ def initialize(args = nil, block = nil)
12
+ @args = args
13
+ @block = block
14
+ end
15
+
16
+ def build_response(condition)
17
+ condition ? STATUS_OK : STATUS_ERROR
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'health_check_toolbox/services/base_health_check'
4
+
5
+ module HealthCheckToolbox
6
+ module Services
7
+ class Database < HealthCheckToolbox::Services::BaseHealthCheck
8
+ def try_connect
9
+ ActiveRecord::Base.establish_connection
10
+ build_response(
11
+ begin
12
+ ActiveRecord::Base.connection_pool.with_connection do
13
+ ActiveRecord::Base.connection.active?
14
+ end
15
+ rescue StandardError
16
+ false
17
+ end
18
+ )
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'health_check_toolbox/services/base_health_check'
4
+ require 'redis'
5
+
6
+ module HealthCheckToolbox
7
+ module Services
8
+ class RedisService < HealthCheckToolbox::Services::BaseHealthCheck
9
+ def try_connect
10
+ build_response(
11
+ begin
12
+ Redis.new.ping
13
+ true
14
+ rescue StandardError
15
+ false
16
+ end
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'health_check_toolbox/services/base_health_check'
4
+ require 'wso2_toolbox'
5
+ require 'rest-client'
6
+
7
+ module HealthCheckToolbox
8
+ module Services
9
+ class RestServices < HealthCheckToolbox::Services::BaseHealthCheck
10
+ attr_accessor :services
11
+
12
+ SERVICE = '_service'
13
+
14
+ def initialize(args, block)
15
+ super(args, block)
16
+ @services = []
17
+ instance_eval(&block)
18
+ end
19
+
20
+ # rubocop:disable Style/MissingRespondToMissing
21
+ def method_missing(method, *args)
22
+ method_name = method.to_s
23
+
24
+ if method_name.include?(SERVICE)
25
+
26
+ class_name = method_name.camelize
27
+
28
+ HealthCheckToolbox::Services.const_set(class_name, class_service)
29
+
30
+ services.push(
31
+ "HealthCheckToolbox::Services::#{class_name}".constantize.new(args)
32
+ )
33
+ else
34
+ super
35
+ end
36
+ end
37
+ # rubocop:enable Style/MissingRespondToMissing
38
+
39
+ # rubocop:disable Metrics/MethodLength
40
+ def class_service
41
+ Class.new(Object) do
42
+ attr_reader :url
43
+
44
+ def initialize(url)
45
+ @url = url.first
46
+ end
47
+
48
+ def try_connect
49
+ build_response(
50
+ begin
51
+ response = RestClient.get(url, headers)
52
+ response.code == 200
53
+ rescue StandardError
54
+ false
55
+ end
56
+ )
57
+ end
58
+
59
+ private
60
+
61
+ def headers
62
+ { 'Authorization' => Wso2Toolbox::TokenManager.generate_token,
63
+ 'Content-Type' => 'application/json' }
64
+ end
65
+
66
+ def build_response(condition)
67
+ condition ? STATUS_OK : STATUS_ERROR
68
+ end
69
+ end
70
+ end
71
+ # rubocop:enable Metrics/MethodLength
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'health_check_toolbox/services/base_health_check'
4
+ require 'wso2_toolbox'
5
+
6
+ module HealthCheckToolbox
7
+ module Services
8
+ class Wso2 < HealthCheckToolbox::Services::BaseHealthCheck
9
+ def try_connect
10
+ build_response(
11
+ begin
12
+ Wso2Toolbox::TokenManager.generate_token
13
+ true
14
+ rescue StandardError
15
+ false
16
+ end
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckToolbox
4
+ class StatusManager
5
+ attr_reader :services
6
+
7
+ def self.generate(services)
8
+ new(services).generate
9
+ end
10
+
11
+ def initialize(services)
12
+ @services = services
13
+ end
14
+
15
+ def generate
16
+ statuses = {}
17
+ services.each.with_index(1) do |service, index|
18
+ status_index = "status_#{index}".to_sym
19
+
20
+ if service.respond_to?(:services)
21
+ services_block(service, index, statuses)
22
+ else
23
+ statuses[status_index] = status(service)
24
+ end
25
+ end
26
+ statuses
27
+ end
28
+
29
+ private
30
+
31
+ def status(service)
32
+ "#{service_name(service)}:#{service.try_connect}"
33
+ end
34
+
35
+ def service_name(service)
36
+ service.class.name.split('::').last.underscore
37
+ end
38
+
39
+ def services_block(element, index, statuses)
40
+ cont = index
41
+
42
+ element.services.each do |service|
43
+ status_index = "status_#{cont > index ? cont : index}".to_sym
44
+ statuses[status_index] = status(service)
45
+ cont += 1
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HealthCheckToolbox
4
+ VERSION = '1.0.1'
5
+ end