rails-healthcheck 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d97361dff356d270a871f8db2f19d6f8d30bb591aef43bb646bf8bb9c1829477
4
- data.tar.gz: 77e4d0501a81cea51466508b87449eff38c434c01f74ef81b8c7b2ddb5f23bba
3
+ metadata.gz: 0101c38ef564e878ce55f0aaea701606c624fd2f7c7bb35d1b5c40d814dccf1b
4
+ data.tar.gz: baf68a65097dfaae941df75caa1a263b7e7a2c27bb8f32f818a62af95c4f85f1
5
5
  SHA512:
6
- metadata.gz: 860bc298d1d6ae2287c3697f1bd1867881e07c449e40025c616da36c688f57fea38f55dfe9a6b94d480aa032349505c5b1603bd7cd98c1a0dde2769121ef9622
7
- data.tar.gz: fd95568f86ee97ef48b5d65399e83942cf96a4b0ef04b29885fdf2d63ae1160b1cce32e593ebb492e30107d045630c31d06153e5e7962454fe6b2a7de566506b
6
+ metadata.gz: ae53712ee12b40e78121ebf6de7d9d36b278d8c5b57472e046354b247c92bafbfe69966e691b4b2ae712034da84d223742d85498587cb81b6c342c9ce1ea5f70
7
+ data.tar.gz: a6237e5a946d94fc3ad885fdc963b89db21b124260e3c5544594736856893130d5f31165e748a2d585c5f6115b0fae1b5c5897d3a9f847423e73fe2725ad58aa
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'healthcheck/version'
4
+ require 'healthcheck/configuration'
5
+ require 'healthcheck/check'
6
+ require 'healthcheck/checker'
7
+ require 'healthcheck/error'
8
+ require 'healthcheck/router'
9
+ require 'healthcheck/engine'
10
+
11
+ module Healthcheck
12
+ require 'healthcheck/railtie' if defined?(Rails)
13
+
14
+ Configuration::SETTINGS.each do |setting|
15
+ send(:define_singleton_method, setting, -> { configuration.send(setting) })
16
+ end
17
+
18
+ def self.configure
19
+ yield(configuration)
20
+ end
21
+
22
+ def self.configuration
23
+ @configuration ||= Configuration.new
24
+ end
25
+
26
+ def self.routes(router)
27
+ Healthcheck::Router.mount(router)
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Healthcheck
4
+ class Check
5
+ attr_accessor :name, :block
6
+
7
+ def initialize(name, block)
8
+ @name = name
9
+ @block = block
10
+ end
11
+
12
+ def execute!
13
+ block.call
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Healthcheck
4
+ class Checker
5
+ attr_accessor :errors
6
+
7
+ def initialize
8
+ @errors = []
9
+ end
10
+
11
+ def check
12
+ Healthcheck.checks.map { |c| Thread.new { execute(c) } }.each(&:join)
13
+ end
14
+
15
+ def errored?
16
+ @errors.any?
17
+ end
18
+
19
+ private
20
+
21
+ def execute(check)
22
+ check.execute!
23
+ rescue StandardError => e
24
+ @errors << Error.new(check.name, e.class, e.message)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Healthcheck
4
+ class Configuration
5
+ SETTINGS = %i[success error verbose route method checks].freeze
6
+
7
+ attr_accessor(*SETTINGS)
8
+
9
+ def initialize
10
+ SETTINGS.each { |key, _| instance_variable_set("@#{key}", nil) }
11
+ clear!
12
+ end
13
+
14
+ def add_check(name, block)
15
+ @checks << Check.new(name, block)
16
+ end
17
+
18
+ def clear!
19
+ @checks = []
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+
5
+ module Healthcheck
6
+ class Engine < Rails::Engine
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Healthcheck
4
+ class Error
5
+ attr_accessor :name, :exception, :message
6
+
7
+ def initialize(name, exception, message)
8
+ @name = name
9
+ @exception = exception.to_s
10
+ @message = message&.squish
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'healthcheck'
4
+ require 'rails'
5
+
6
+ module Healthcheck
7
+ class Railtie < Rails::Railtie
8
+ rake_tasks do
9
+ Dir[File.join(File.dirname(__FILE__), 'tasks/*.rake')].each { |f| load f }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Healthcheck
4
+ class Router
5
+ def self.mount(router)
6
+ router.send(Healthcheck.method, Healthcheck.route => 'healthcheck/healthchecks#check')
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ INITIALIZER = 'config/initializers/healthcheck.rb'
6
+ OLD_ROUTES = 'config/routes.rb'
7
+ NEW_ROUTES = 'config/routes.rb.new'
8
+ ROUTES_SETUP = ' Healthcheck.routes(self)'
9
+ ROUTES_INIT = 'Rails.application.routes.draw do'
10
+
11
+ namespace :healthcheck do
12
+ desc 'Install the files and settings to the gem Healthcheck works'
13
+ task :install do
14
+ puts ' - invoke Rails::Healthcheck'
15
+ create_initializer
16
+ mount_route
17
+ end
18
+ end
19
+
20
+ def create_initializer
21
+ FileUtils.mkdir_p(File.dirname(INITIALIZER))
22
+ File.open(INITIALIZER, 'w') { |file| file << settings }
23
+ puts ' - create config/initializers/healthcheck.rb'
24
+ end
25
+
26
+ def mount_route
27
+ return unless File.exist?(OLD_ROUTES)
28
+
29
+ puts ' - modify config/routes.rb'
30
+ File.open(NEW_ROUTES, 'w') do |new_routes|
31
+ File.foreach(OLD_ROUTES) do |line|
32
+ new_routes.puts(line)
33
+ if line.strip == ROUTES_INIT
34
+ new_routes.puts(ROUTES_SETUP)
35
+ new_routes.puts ''
36
+ end
37
+ end
38
+ end
39
+
40
+ File.delete(OLD_ROUTES)
41
+ File.rename(NEW_ROUTES, OLD_ROUTES)
42
+ end
43
+
44
+ def settings
45
+ <<~SETTINGS
46
+ # frozen_string_literal: true
47
+
48
+ Healthcheck.configure do |config|
49
+ config.success = 200
50
+ config.error = 503
51
+ config.verbose = false
52
+ config.route = '/healthcheck'
53
+ config.method = :get
54
+
55
+ # -- Checks --
56
+ # Check if the db is available
57
+ # config.add_check :database, -> { ActiveRecord::Base.connection.execute('select 1') }
58
+ # Check if the db is available and without pending migrations
59
+ # config.add_check :migrations,-> { ActiveRecord::Migration.check_pending! }
60
+ # Check if the cache is available
61
+ # config.add_check :cache, -> { Rails.cache.read('some_key') }
62
+ # Check if the application required envs are defined
63
+ # config.add_check :environments, -> { Dotenv.require_keys('ENV_NAME', 'ANOTHER_ENV') }
64
+ end
65
+ SETTINGS
66
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Healthcheck
4
+ VERSION = '1.0.3'
5
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'healthcheck'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-healthcheck
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - linqueta
@@ -131,6 +131,17 @@ extra_rdoc_files: []
131
131
  files:
132
132
  - README.md
133
133
  - app/controllers/healthcheck/healthchecks_controller.rb
134
+ - lib/healthcheck.rb
135
+ - lib/healthcheck/check.rb
136
+ - lib/healthcheck/checker.rb
137
+ - lib/healthcheck/configuration.rb
138
+ - lib/healthcheck/engine.rb
139
+ - lib/healthcheck/error.rb
140
+ - lib/healthcheck/railtie.rb
141
+ - lib/healthcheck/router.rb
142
+ - lib/healthcheck/tasks/setup.rake
143
+ - lib/healthcheck/version.rb
144
+ - lib/rails-healthcheck.rb
134
145
  homepage: https://github.com/linqueta/rails-healthcheck
135
146
  licenses:
136
147
  - MIT