healthier 0.1.7 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/healthier/application_controller.rb +0 -1
- data/lib/connectors/mongodb.rb +25 -0
- data/lib/connectors/postgresql.rb +22 -0
- data/lib/connectors/rabbitmq.rb +29 -0
- data/lib/connectors/redis.rb +24 -0
- data/lib/healthier/api_authenticator.rb +3 -7
- data/lib/healthier/engine.rb +29 -14
- data/lib/healthier/version.rb +1 -1
- metadata +14 -15
- data/app/lib/errors/api_errors.rb +0 -33
- data/lib/checkers/mongodb_checker.rb +0 -26
- data/lib/checkers/postgresql_checker.rb +0 -23
- data/lib/checkers/rabbitmq_checker.rb +0 -25
- data/lib/checkers/redis_checker.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54a75f98f4a16fb6e7c63563baedcbd7dee7d05a16848488a7eb02c416f139c3
|
4
|
+
data.tar.gz: b2bbff5440b799ac64161c151f3633d8e534569bba013b85a3426915ac517716
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19577350a2b28642f9b654d5c4bdb14b245b0de85f29e54f2ec7812a943d545eeda0757b9e0509462cdb82d6803fe029bead9f819660ffbce985a11dc8980dcb
|
7
|
+
data.tar.gz: 4a7278b141c18f6523882acac1fc8255ab7e44c05d599269169b00684575b5631b97298375c3047567fef6f92ff39dd00b8269b114504cdd63b53a9dae179a92
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Connectors
|
4
|
+
# Mongodb health checker
|
5
|
+
class Mongodb
|
6
|
+
def connect
|
7
|
+
@client = Mongo::Client.new(
|
8
|
+
ENV.fetch('MONGO_DB_URI', nil) || ['localhost:27017']
|
9
|
+
)
|
10
|
+
@client.command(ping: 1)
|
11
|
+
true
|
12
|
+
rescue Mongo::Error::SocketError, Mongo::Error::NoServerAvailable
|
13
|
+
puts "Mongodb Connection Error: #{e.message}"
|
14
|
+
false
|
15
|
+
ensure
|
16
|
+
@client&.close
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configured?
|
20
|
+
raise StandardError, 'Please make sure you have MongoDB installed' unless defined?(::Mongo::Client)
|
21
|
+
|
22
|
+
'configured'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Connectors
|
4
|
+
# Postgres health checker
|
5
|
+
class Postgresql
|
6
|
+
def connect
|
7
|
+
@conn = ActiveRecord::Base.connection
|
8
|
+
true
|
9
|
+
rescue ::PG::ConnectionBad
|
10
|
+
puts "Postgres Connection Error: #{e.message}"
|
11
|
+
false
|
12
|
+
ensure
|
13
|
+
@conn&.close
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.configured?
|
17
|
+
raise StandardError, 'Please make sure you have Postgres installed' unless defined?(::ActiveRecord::Base)
|
18
|
+
|
19
|
+
'configured'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Connectors
|
4
|
+
# Rabbitmq health checker
|
5
|
+
class Rabbitmq
|
6
|
+
def connect
|
7
|
+
@conn = ::Bunny.new
|
8
|
+
@conn.start
|
9
|
+
@channel = @conn.create_channel
|
10
|
+
@queue = @channel.queue('health_check')
|
11
|
+
@queue.publish('ping')
|
12
|
+
true
|
13
|
+
rescue ::Bunny::Exception, ::Bunny::TCPConnectionFailed, ::Bunny::PossibleAuthenticationFailureError => e
|
14
|
+
puts "RabbitMQ Connection Error: #{e.message}"
|
15
|
+
false
|
16
|
+
ensure
|
17
|
+
@queue&.delete
|
18
|
+
@queue&.purge
|
19
|
+
@channel&.close
|
20
|
+
@conn&.close
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.configured?
|
24
|
+
raise StandardError, 'Please make sure you have RabbitMQ installed' unless defined?(::Bunny)
|
25
|
+
|
26
|
+
'configured'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Connectors
|
4
|
+
# Redis health checker
|
5
|
+
class Redis
|
6
|
+
def connect
|
7
|
+
begin
|
8
|
+
@redis = ::Redis.new
|
9
|
+
@redis.ping == 'PONG'
|
10
|
+
rescue Redis::CannotConnectError
|
11
|
+
puts "Redis Connection Error: #{e.message}"
|
12
|
+
false
|
13
|
+
ensure
|
14
|
+
@redis&.close
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configured?
|
19
|
+
raise StandardError, 'Please make sure you have Redis installed' unless defined?(::Redis)
|
20
|
+
|
21
|
+
'configured'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -3,11 +3,10 @@
|
|
3
3
|
module Healthier
|
4
4
|
# Engine
|
5
5
|
class ApiAuthenticator
|
6
|
-
attr_accessor :controller
|
6
|
+
attr_accessor :controller
|
7
7
|
|
8
8
|
def initialize(controller)
|
9
9
|
@controller = controller
|
10
|
-
@request = controller.request
|
11
10
|
end
|
12
11
|
|
13
12
|
def authenticate
|
@@ -17,17 +16,14 @@ module Healthier
|
|
17
16
|
end
|
18
17
|
|
19
18
|
def authenticate_with_bearer_token
|
20
|
-
tok = request.authorization.to_s
|
21
|
-
raise StandardError, 'Access Denied' unless tok.starts_with?('Bearer')
|
22
|
-
|
23
19
|
controller.authenticate_with_http_token do |token, _options|
|
24
|
-
|
20
|
+
ENV['BEARER_TOKEN'] == token
|
25
21
|
end
|
26
22
|
end
|
27
23
|
|
28
24
|
def authenticate_with_basic_auth
|
29
25
|
controller.authenticate_or_request_with_http_basic do |username, password|
|
30
|
-
|
26
|
+
username == ENV['USERNAME'] && password == ENV['PASSWORD']
|
31
27
|
end
|
32
28
|
end
|
33
29
|
end
|
data/lib/healthier/engine.rb
CHANGED
@@ -1,18 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '../
|
4
|
-
require_relative '../
|
5
|
-
require_relative '../
|
6
|
-
require_relative '../
|
3
|
+
require_relative '../connectors/mongodb'
|
4
|
+
require_relative '../connectors/postgresql'
|
5
|
+
require_relative '../connectors/rabbitmq'
|
6
|
+
require_relative '../connectors/redis'
|
7
7
|
|
8
8
|
module Healthier
|
9
9
|
# Engine
|
10
10
|
class Engine < ::Rails::Engine
|
11
11
|
isolate_namespace Healthier
|
12
12
|
|
13
|
+
initializer 'healthier.after_initialize' do
|
14
|
+
ActiveSupport.on_load(:after_initialize) do
|
15
|
+
dependent_services = Healthier.depends_on.dig('healthier', 'depends_on')
|
16
|
+
dependent_services.each do |service_conf|
|
17
|
+
connector = "::Connectors::#{service_conf['name'].camelize}".constantize
|
18
|
+
connector.configured?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
13
23
|
def ping!
|
14
|
-
conf['depends_on'].each_with_object({}) do |
|
15
|
-
obj[
|
24
|
+
conf['depends_on'].each_with_object({}) do |dependent, obj|
|
25
|
+
obj[dependent['name']] = ping_it(dependent['name'])
|
16
26
|
end
|
17
27
|
end
|
18
28
|
|
@@ -21,15 +31,20 @@ module Healthier
|
|
21
31
|
end
|
22
32
|
|
23
33
|
def ping_it(service)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
34
|
+
connector = "::Connectors::#{service.camelize}".constantize
|
35
|
+
connection = connector.new.connect
|
36
|
+
|
37
|
+
if connection == true
|
38
|
+
return {
|
39
|
+
status: Healthier::SUCCESS,
|
40
|
+
message: 'pong'
|
41
|
+
}
|
42
|
+
end
|
30
43
|
|
31
|
-
|
32
|
-
|
44
|
+
{
|
45
|
+
status: Healthier::FAILURE,
|
46
|
+
message: "#{service.camelize} service is not running. Please check your connection"
|
47
|
+
}
|
33
48
|
end
|
34
49
|
end
|
35
50
|
end
|
data/lib/healthier/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: healthier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nima Yonten
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
version: '6.0'
|
27
27
|
description: Description of Healthier.
|
28
28
|
email:
|
29
|
-
-
|
29
|
+
- nima.yonten1729@gmail.com
|
30
30
|
executables: []
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
@@ -36,28 +36,27 @@ files:
|
|
36
36
|
- Rakefile
|
37
37
|
- app/controllers/healthier/application_controller.rb
|
38
38
|
- app/controllers/healthier/checks_controller.rb
|
39
|
-
- app/lib/errors/api_errors.rb
|
40
39
|
- config/routes.rb
|
41
|
-
- lib/
|
42
|
-
- lib/
|
43
|
-
- lib/
|
44
|
-
- lib/
|
40
|
+
- lib/connectors/mongodb.rb
|
41
|
+
- lib/connectors/postgresql.rb
|
42
|
+
- lib/connectors/rabbitmq.rb
|
43
|
+
- lib/connectors/redis.rb
|
45
44
|
- lib/generators/healthier/config_generator.rb
|
46
45
|
- lib/healthier.rb
|
47
46
|
- lib/healthier/api_authenticator.rb
|
48
47
|
- lib/healthier/engine.rb
|
49
48
|
- lib/healthier/version.rb
|
50
49
|
- lib/tasks/healthier_tasks.rake
|
51
|
-
homepage: https://github.com/
|
50
|
+
homepage: https://github.com/SELISEdigitalplatforms/healthier
|
52
51
|
licenses:
|
53
52
|
- MIT
|
54
53
|
metadata:
|
55
54
|
allowed_push_host: https://rubygems.org
|
56
|
-
homepage_uri: https://github.com/
|
57
|
-
source_code_uri: https://github.com/
|
58
|
-
changelog_uri: https://github.com/
|
55
|
+
homepage_uri: https://github.com/SELISEdigitalplatforms/healthier
|
56
|
+
source_code_uri: https://github.com/SELISEdigitalplatforms/healthier
|
57
|
+
changelog_uri: https://github.com/SELISEdigitalplatforms/healthier
|
59
58
|
rubygems_mfa_required: 'true'
|
60
|
-
post_install_message:
|
59
|
+
post_install_message:
|
61
60
|
rdoc_options: []
|
62
61
|
require_paths:
|
63
62
|
- lib
|
@@ -73,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
72
|
version: '0'
|
74
73
|
requirements: []
|
75
74
|
rubygems_version: 3.4.10
|
76
|
-
signing_key:
|
75
|
+
signing_key:
|
77
76
|
specification_version: 4
|
78
77
|
summary: Summary of Healthier.
|
79
78
|
test_files: []
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Errors
|
4
|
-
module ApiErrors
|
5
|
-
def self.included(base)
|
6
|
-
base.class_eval do
|
7
|
-
rescue_from StandardError, with: :bad_request
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def bad_request(error)
|
12
|
-
render_error(:bad_request, error: { message: error.message })
|
13
|
-
end
|
14
|
-
|
15
|
-
def render_success(status, options = {})
|
16
|
-
options[:success] = true
|
17
|
-
render json: options, status: status
|
18
|
-
end
|
19
|
-
|
20
|
-
def render_error(status, options = {})
|
21
|
-
options[:error] = { message: I18n.t("api_errors.#{status}") } if options[:error].blank?
|
22
|
-
options[:success] = false
|
23
|
-
Rollbar.error(options[:error])
|
24
|
-
render json: options, status: status
|
25
|
-
end
|
26
|
-
|
27
|
-
def render_silent_error(status, options = {})
|
28
|
-
options[:error] = { message: I18n.t("api_errors.#{status}") } if options[:error].blank?
|
29
|
-
options[:success] = false
|
30
|
-
render json: options, status: status
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Checkers
|
4
|
-
# Mongodb health checker
|
5
|
-
class MongodbChecker
|
6
|
-
class << self
|
7
|
-
def check
|
8
|
-
raise 'Please make sure you have Mongodb installed' unless defined?(::Mongo::Client)
|
9
|
-
|
10
|
-
begin
|
11
|
-
@client = Mongo::Client.new(
|
12
|
-
ENV.fetch('MONGO_DB_URI', nil) || ['localhost:27017'],
|
13
|
-
max_retries: 2,
|
14
|
-
retry_interval: 1
|
15
|
-
)
|
16
|
-
@client.command(ping: 1)
|
17
|
-
true
|
18
|
-
rescue Mongo::Error::SocketError, Mongo::Error::NoServerAvailable
|
19
|
-
false
|
20
|
-
ensure
|
21
|
-
@client&.close
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Checkers
|
4
|
-
# Postgres health checker
|
5
|
-
class PostgresqlChecker
|
6
|
-
class << self
|
7
|
-
def check
|
8
|
-
raise 'Please make sure you have Postgres installed' unless defined?(::ActiveRecord::Base)
|
9
|
-
|
10
|
-
begin
|
11
|
-
# @conn = ::PG.connect(dbname: ENV['DATABASE_NAME'])
|
12
|
-
# @conn.exec('SELECT 1')
|
13
|
-
@conn = ActiveRecord::Base.connection
|
14
|
-
true
|
15
|
-
rescue ::PG::ConnectionBad
|
16
|
-
false
|
17
|
-
ensure
|
18
|
-
@conn&.close
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Checkers
|
4
|
-
# Rabbitmq health checker
|
5
|
-
class RabbitmqChecker
|
6
|
-
class << self
|
7
|
-
def check
|
8
|
-
raise 'Please make sure you have RabbitMQ installed' unless defined?(::Bunny)
|
9
|
-
|
10
|
-
begin
|
11
|
-
@conn = ::Bunny.new
|
12
|
-
@conn.start
|
13
|
-
channel = @conn.create_channel
|
14
|
-
queue = channel.queue('health_check')
|
15
|
-
queue.publish('ping')
|
16
|
-
true
|
17
|
-
rescue ::Bunny::Exception, ::Bunny::TCPConnectionFailed, ::Bunny::PossibleAuthenticationFailureError
|
18
|
-
false
|
19
|
-
ensure
|
20
|
-
@conn&.close
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Checkers
|
4
|
-
# Redis health checker
|
5
|
-
class RedisChecker
|
6
|
-
class << self
|
7
|
-
def check
|
8
|
-
raise 'Please make sure you have Redis installed' unless defined?(::Redis)
|
9
|
-
|
10
|
-
begin
|
11
|
-
@redis = Redis.new
|
12
|
-
@redis.ping == 'PONG'
|
13
|
-
rescue Redis::CannotConnectError
|
14
|
-
false
|
15
|
-
ensure
|
16
|
-
@redis&.close
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|