liveness 0.1.0 → 0.2.2

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: fb9ece76b575586d5edbabc8b95571434e680653385589ec4f986d55fc3ccc8f
4
- data.tar.gz: 880fd29159c5a5ce9d0344a0a3542322fcc515445608a6f19aaf63a45af14043
3
+ metadata.gz: d8c4374a066d22f71ed43d6cbef9d3e8fa84d91ee74f4dcd99420dc1f0f05232
4
+ data.tar.gz: '0482fa040d357f8b4486576ababe08591dbc589a3e353bee9f0b4c9e332baf70'
5
5
  SHA512:
6
- metadata.gz: bd7366f49056af9feefaf7eb74fee9b0707652378e1c41cec4f0390af5f2156a01b434e78e980ad2e5f9bdb1372fb4dd3577573f2031711a432b879026a37b06
7
- data.tar.gz: 4418096a94fb37c10db94497e295e40b6038b50f51035eb79efaf3ac5bbd428bcc366b222f81e7d0ff7bc4a378de3b08793854302ebc705375276ff500f2d8dc
6
+ metadata.gz: f2f68bd1ee8ea102b3c8517f839e0d750ee6d9dc32487d8c9578ff31635eb5f1ae1b7eea39e1f0825438e9ff572824ee41d7dd49e8956ad938e7aa37d16dbdc9
7
+ data.tar.gz: f0083b3f15da4d97f12561f29af8628bf0b02ea53cd083d98783e5d5b29fb4bffdd4de90672ef733bf5c92a464914cae543a86021d0b6bbbef6dffbc439f6ee7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- liveness (0.1.0)
4
+ liveness (0.2.2)
5
5
  rack (>= 1.0)
6
6
 
7
7
  GEM
@@ -22,7 +22,7 @@ GEM
22
22
  parallel (1.21.0)
23
23
  parser (3.1.0.0)
24
24
  ast (~> 2.4.1)
25
- rack (1.6.13)
25
+ rack (2.2.3)
26
26
  rainbow (3.1.1)
27
27
  rake (13.0.6)
28
28
  regexp_parser (2.2.0)
@@ -67,6 +67,7 @@ GEM
67
67
 
68
68
  PLATFORMS
69
69
  x86_64-darwin-20
70
+ x86_64-linux
70
71
 
71
72
  DEPENDENCIES
72
73
  bundler-audit
data/README.md CHANGED
@@ -20,12 +20,44 @@ Liveness.config do |c|
20
20
  end
21
21
  ```
22
22
 
23
+ ### Named Dependency
24
+
25
+ ```ruby
26
+ Liveness.config do |c|
27
+ c.add :postgres, name: :primary_db, timeout: 10
28
+ c.add :postgres, name: :read_replica, timeout: 10
29
+ end
30
+ ```
31
+
32
+ ### Customize Connector
33
+
34
+ ```ruby
35
+ Liveness.config do |c|
36
+ c.add :redis, timeout: 10 do
37
+ Redis.new(url: 'redis://example:6379/15')
38
+ end
39
+ end
40
+ ```
41
+
42
+ ### Rails Application
43
+
44
+ Mount the `Liveness::Status` to `config/routes.rb`
45
+
46
+ ```ruby
47
+ Rails.application.routes.draw do
48
+ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
49
+
50
+ mount Liveness::Status => '/health'
51
+ end
52
+ ```
53
+
23
54
  ## Dependency Providers
24
55
 
25
- | Name | Descroption |
56
+ | Name | Description |
26
57
  |------------|-------------------------------------------------------|
27
58
  | `postgres` | The PostgreSQL adapter which for `ActiveRecord::Base` |
28
59
  | `mysql` | The MySQL adapter which for `ActiveRecord::Base` |
60
+ | `redis` | Test Redis via `redis-rb` gem via `#ping` method |
29
61
 
30
62
  ## Roadmap
31
63
 
@@ -38,7 +70,8 @@ end
38
70
  * [ ] MySQL
39
71
  * [x] ActiveRecord
40
72
  * [ ] Sequel
41
- * Redis
73
+ * [x] Redis
74
+ * [ ] HTTP Endpoint
42
75
 
43
76
  ## Development
44
77
 
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Liveness
4
+ # :nodoc:
5
+ module Dependencies
6
+ # The PostgreSQL Provider
7
+ #
8
+ # @since 0.2.0
9
+ class Redis < Dependency
10
+ # @see [Liveness::Dependency#check!]
11
+ #
12
+ # @since 0.2.0
13
+ def check!
14
+ return false unless defined?(::Redis)
15
+
16
+ redis = connect || ::Redis.new
17
+ redis.ping == 'PONG'
18
+ rescue ::Redis::BaseError
19
+ false
20
+ end
21
+ end
22
+
23
+ Liveness.container.register(:redis, Redis)
24
+ end
25
+ end
@@ -7,5 +7,6 @@ module Liveness
7
7
  module Dependencies
8
8
  require_relative 'dependencies/postgresql'
9
9
  require_relative 'dependencies/mysql'
10
+ require_relative 'dependencies/redis'
10
11
  end
11
12
  end
@@ -14,8 +14,18 @@ module Liveness
14
14
  def initialize(name: nil, timeout: 5, &block)
15
15
  @name = name
16
16
  @timeout = timeout
17
+ @connector = block
18
+ end
17
19
 
18
- instance_exec(self, &block) if defined?(yield)
20
+ # Return status
21
+ #
22
+ # @return [Hash]
23
+ #
24
+ # @since 0.2.0
25
+ def status
26
+ {
27
+ status: alive? ? 'ok' : 'failed'
28
+ }
19
29
  end
20
30
 
21
31
  # Check the dependency service alive
@@ -25,10 +35,12 @@ module Liveness
25
35
  # @since 0.1.0
26
36
  def alive?
27
37
  Timeout.timeout(@timeout) do
28
- check!
38
+ connect || check!
29
39
  rescue StandardError
30
40
  false
31
41
  end
42
+ rescue Timeout::Error
43
+ false
32
44
  end
33
45
 
34
46
  # Check the dependency service alive
@@ -39,5 +51,16 @@ module Liveness
39
51
  def check!
40
52
  raise NotImplementedError
41
53
  end
54
+
55
+ # Connect with connector
56
+ #
57
+ # @return [Object]
58
+ #
59
+ # @since 0.2.0
60
+ def connect
61
+ return unless @connector.respond_to?(:call)
62
+
63
+ instance_exec(self, &@connector)
64
+ end
42
65
  end
43
66
  end
@@ -27,7 +27,7 @@ module Liveness
27
27
  # @since 0.1.0
28
28
  def metrics
29
29
  @metrics ||= @config.dependencies.map do |dependency|
30
- Thread.new { [dependency.name, dependency.alive?] }
30
+ Thread.new { [dependency.name, dependency.status] }
31
31
  end.map(&:value).to_h
32
32
  end
33
33
 
@@ -37,7 +37,10 @@ module Liveness
37
37
  #
38
38
  # @since 0.1.0
39
39
  def live?
40
- metrics.values.reduce(true, :&)
40
+ metrics
41
+ .values
42
+ .map { |service| service[:status] == 'ok' }
43
+ .reduce(true, :&)
41
44
  end
42
45
 
43
46
  # @return [Rack::Response]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Liveness
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liveness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - 蒼時弦也
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-18 00:00:00.000000000 Z
11
+ date: 2022-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -47,6 +47,7 @@ files:
47
47
  - lib/liveness/dependencies.rb
48
48
  - lib/liveness/dependencies/mysql.rb
49
49
  - lib/liveness/dependencies/postgresql.rb
50
+ - lib/liveness/dependencies/redis.rb
50
51
  - lib/liveness/dependency.rb
51
52
  - lib/liveness/status.rb
52
53
  - lib/liveness/version.rb