services_health_check 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: 842c8c15f8766929a33acaacdaf629045285224e25f0dee4b38f0c4dc4ae1d12
4
+ data.tar.gz: 43cc485ed097501766ab0a969a6a28a690dad7c86e8182e5dd708624e6063157
5
+ SHA512:
6
+ metadata.gz: 644c3071863602e02ba456a559d3789a05363699f4ea1f425a8e5438b51608c9fbf5a72f3267f91b4c0682156a96d117ca04f84222d7bb944c71d6f2357d8c24
7
+ data.tar.gz: 7aabcf6713998a6a72f624882e746136d9a7612a70bc84d52c1af88d96333536ac2f2c36574e5d59eb251445e7c23f057d605773963f59af7c8dfdd763524a31
@@ -0,0 +1,26 @@
1
+ require "rails/generators"
2
+
3
+ module ServicesHealthCheck
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("templates", __dir__)
7
+ desc "Adds GET /health_check with HealthCheckController and HealthCheckService"
8
+
9
+ def add_route
10
+ if File.read(File.join(destination_root, "config/routes.rb")).include?("health_check#show")
11
+ say_status :skip, "health_check route already present", :yellow
12
+ else
13
+ route 'get "health_check", to: "health_check#show", as: :health_check'
14
+ end
15
+ end
16
+
17
+ def create_controller
18
+ template "health_check_controller.rb", "app/controllers/health_check_controller.rb"
19
+ end
20
+
21
+ def create_service
22
+ template "health_check_service.rb", "app/services/health_check_service.rb"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ # GET /health_check: always JSON, 200 when every check passes, 503 otherwise (services_health_check).
2
+ class HealthCheckController < ActionController::API
3
+ def show
4
+ results = HealthCheckService.new.call
5
+ render json: results, status: results.values.all? ? :ok : :service_unavailable
6
+ rescue StandardError, ScriptError => error
7
+ Rails.logger.error("[services_health_check] #{error.class}: #{error.message}")
8
+ render json: { app: false }, status: :service_unavailable
9
+ end
10
+ end
@@ -0,0 +1,37 @@
1
+ # Each public method is one check: its name is the JSON key, and it returns true or false.
2
+ # `app` is always here; uncomment and implement the others the project uses.
3
+ class HealthCheckService
4
+ TIMEOUT = 5
5
+
6
+ def call
7
+ checks.index_with { |name| safely { public_send(name) } }
8
+ end
9
+
10
+ # The project answers its own /up (Rails' built-in health page).
11
+ def app
12
+ HTTParty.get(ENV.fetch("SERVICES_HEALTH_CHECK_APP_URL", "http://localhost:#{ENV.fetch("PORT", 3000)}/up"),
13
+ timeout: TIMEOUT).success?
14
+ end
15
+
16
+ # def sidekiq
17
+ # TODO: implement the code
18
+ # end
19
+
20
+ # def redis
21
+ # TODO: implement the code
22
+ # end
23
+
24
+ private
25
+ # `app` first, then the other checks by name.
26
+ def checks
27
+ [ :app, *(self.class.public_instance_methods(false) - [ :call, :app ]).sort ]
28
+ end
29
+
30
+ # A check that raises, or returns anything but true, is off. ScriptError covers checks
31
+ # left as `raise NotImplementedError` and a `require` that fails.
32
+ def safely
33
+ yield == true
34
+ rescue StandardError, ScriptError
35
+ false
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module ServicesHealthCheck
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks { load File.expand_path("../tasks/services_health_check.rake", __dir__) }
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module ServicesHealthCheck
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "services_health_check/version"
2
+ require "httparty"
3
+ require "services_health_check/railtie" if defined?(Rails::Railtie)
@@ -0,0 +1,12 @@
1
+ namespace :services_health_check do
2
+ desc "Add GET /health_check (route, HealthCheckController, HealthCheckService); safe to re-run"
3
+ task install: :environment do
4
+ require "rails/generators"
5
+
6
+ # Same as `bin/rails generate services_health_check:install --skip`, but aimed at Rails.root
7
+ # directly: Rails::Command.root is only set when the task runs through bin/rails.
8
+ Rails.application.load_generators
9
+ Rails::Generators.invoke "services_health_check:install", [ "--skip" ],
10
+ behavior: :invoke, destination_root: Rails.root
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: services_health_check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andre Oliveira
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: railties
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.1'
26
+ - !ruby/object:Gem::Dependency
27
+ name: httparty
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.24'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.24'
40
+ description: Adds a GET /health_check endpoint to a Rails app that answers in JSON
41
+ whether the app and each service it declares are working.
42
+ email: andreoliveirar2d2@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/generators/services_health_check/install/install_generator.rb
48
+ - lib/generators/services_health_check/install/templates/health_check_controller.rb.tt
49
+ - lib/generators/services_health_check/install/templates/health_check_service.rb.tt
50
+ - lib/services_health_check.rb
51
+ - lib/services_health_check/railtie.rb
52
+ - lib/services_health_check/version.rb
53
+ - lib/tasks/services_health_check.rake
54
+ homepage: https://rubygems.org/gems/services_health_check
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '3.2'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.6.9
73
+ specification_version: 4
74
+ summary: Made for checking the rails and related services are up and running
75
+ test_files: []