rx-healthcheck 0.1.2 → 0.1.6

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: b1566710004f2caf4611964415d144921c8d6ea9b32936ca08fba2189282042e
4
- data.tar.gz: ca37d15363b876ebabf6bc2d0fb5cf13cd156e51c2afb29dc0860e7aa8f9f613
3
+ metadata.gz: 9a5089cc2aca4dab181a11a5aa9a3135862d7de57976ded482366a9e147ac7a8
4
+ data.tar.gz: 4ec642b3e93f6b0f983cb7dff58b4bed8ede3e64dd7655bd1cac7f76583c7dfe
5
5
  SHA512:
6
- metadata.gz: e474fcde410e1c2f73e4e48cfd4588c227d29d6ad2b443a7f5a92c4c64542cd334e766212ed9ade803b2fd4344a75351129209c116c7e5830f1aa3ecf1ef881e
7
- data.tar.gz: d4e3a2fd87c407e13cd645afa4b07a3f4ffc68457515a69ea360b1c6761431e9df1bb1629d500366cab7ab9b436afef516b31c063acf3306de5e31a5bdf3e8bf
6
+ metadata.gz: 439bc089d1ce63f1c63f687cdf0b8b473bbcb2f6bbf3c04a847905d176cdb667e6f6c3b6e754121af685b87fc2b00f318ca3e4c6fc2de17fd80cd4cd88693ff2
7
+ data.tar.gz: 37c97bcd6967d3d336e669d78b6474978771c0d7dbc79daca8276dd66484c2485398f1cc6bd888c5f46f63e4855c33c9b4bb70a7b7e042937b588259e7b61a0f
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rx-healthcheck (0.1.1)
4
+ rx-healthcheck (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -3,17 +3,20 @@ module Rx
3
3
  class FileSystemCheck
4
4
  FILENAME = "rx".freeze
5
5
 
6
- attr_reader :name
6
+ attr_reader :name, :timeout
7
7
 
8
- def initialize(name = "fs")
8
+ def initialize(name = "fs", timeout = 0)
9
9
  @name = name
10
+ @timeout = 0
10
11
  end
11
12
 
12
13
  def check
13
14
  Result.from(name) do
14
- !!Tempfile.open(FILENAME) do |f|
15
- f.write("ok")
16
- f.flush
15
+ Timeout::timeout(timeout) do
16
+ !!Tempfile.open(FILENAME) do |f|
17
+ f.write("ok")
18
+ f.flush
19
+ end
17
20
  end
18
21
  end
19
22
  end
@@ -1,16 +1,19 @@
1
+ require "timeout"
2
+
1
3
  module Rx
2
4
  module Check
3
5
  class GenericCheck
4
- attr_reader :name
6
+ attr_reader :name, :timeout
5
7
 
6
- def initialize(callable, name = "generic")
8
+ def initialize(callable, name = "generic", timeout = 0)
7
9
  @callable = callable
8
10
  @name = name
11
+ @timeout = timeout
9
12
  end
10
13
 
11
14
  def check
12
15
  Result.from(name) do
13
- callable.call
16
+ Timeout::timeout(timeout) { callable.call }
14
17
  end
15
18
  end
16
19
 
@@ -3,17 +3,18 @@ require "uri"
3
3
  module Rx
4
4
  module Check
5
5
  class HttpCheck
6
- attr_reader :name
6
+ attr_reader :name, :timeout
7
7
 
8
- def initialize(url, name = "http")
8
+ def initialize(url, name = "http", timeout: 1)
9
9
  @url = URI(url)
10
10
  @name = name
11
+ @timeout = timeout
11
12
  end
12
13
 
13
14
  def check
14
15
  Result.from(name) do
15
16
  http = Net::HTTP.new(url.host, url.port)
16
- http.read_timeout = 1
17
+ http.read_timeout = timeout
17
18
  http.use_ssl = url.scheme == "https"
18
19
 
19
20
  response = http.request(Net::HTTP::Get.new(path))
@@ -2,7 +2,7 @@ module Rx
2
2
  module Check
3
3
  class Result
4
4
  def self.from(check_name)
5
- start_at = Time.now
5
+ start_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
6
6
  err = nil
7
7
  result = false
8
8
 
@@ -12,7 +12,7 @@ module Rx
12
12
  err = ex
13
13
  end
14
14
 
15
- end_at = Time.now
15
+ end_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
16
16
 
17
17
  Result.new(check_name, result, ((end_at - start_at) * 1000).round(2), err)
18
18
  end
@@ -14,6 +14,10 @@ module Rx
14
14
  Future.new(&block).execute
15
15
  end
16
16
 
17
+ def self.thread_pool
18
+ @@pool
19
+ end
20
+
17
21
  def initialize(&block)
18
22
  @channel = Queue.new
19
23
  @state = :pending
@@ -6,6 +6,7 @@ module Rx
6
6
  def initialize(size = Etc.nprocessors)
7
7
  @pool = []
8
8
  @size = size
9
+ @pid = Process.pid
9
10
  end
10
11
 
11
12
  def shutdown
@@ -25,17 +26,33 @@ module Rx
25
26
  self
26
27
  end
27
28
 
29
+ def restart
30
+ shutdown
31
+ start
32
+ end
33
+
28
34
  def started?
29
35
  pool.map(&:alive?).any?
30
36
  end
31
37
 
32
38
  def submit(&block)
39
+ restart_on_fork if forked?
40
+
33
41
  return unless started?
34
42
  queue << block
35
43
  end
36
44
 
37
45
  private
38
- attr_reader :pool, :queue, :size
46
+ attr_reader :pid, :pool, :queue, :size
47
+
48
+ def forked?
49
+ Process.pid != pid
50
+ end
51
+
52
+ def restart_on_fork
53
+ restart
54
+ @pid = Process.pid
55
+ end
39
56
 
40
57
  def worker
41
58
  -> {
data/lib/rx/middleware.rb CHANGED
@@ -23,11 +23,11 @@ module Rx
23
23
  end
24
24
 
25
25
  def call(env)
26
- unless health_check_request?(env)
26
+ unless health_check_request?(path(env))
27
27
  return app.call(env)
28
28
  end
29
29
 
30
- case env["REQUEST_PATH"]
30
+ case path(env)
31
31
  when "/liveness"
32
32
  ok = check_to_component(liveness_checks).map { |x| x[:status] == 200 }.all?
33
33
  liveness_response(ok)
@@ -57,14 +57,18 @@ module Rx
57
57
  Rx::Cache::InMemoryCache.new
58
58
  end
59
59
 
60
- def health_check_request?(env)
61
- %w[/liveness /readiness /deep].include?(env["REQUEST_PATH"])
60
+ def health_check_request?(path)
61
+ %w[/liveness /readiness /deep].include?(path)
62
62
  end
63
63
 
64
64
  def liveness_response(is_ok)
65
65
  [is_ok ? 200 : 503, {}, []]
66
66
  end
67
67
 
68
+ def path(env)
69
+ env["PATH_INFO"] || env["REQUEST_PATH"] || env["REQUEST_URI"]
70
+ end
71
+
68
72
  def readiness_response(components)
69
73
  status = components.map { |x| x[:status] == 200 }.all? ? 200 : 503
70
74
 
data/lib/rx/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rx
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.6"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rx-healthcheck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Pendleton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-25 00:00:00.000000000 Z
11
+ date: 2021-09-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  requirements: []
63
- rubygems_version: 3.1.2
63
+ rubygems_version: 3.1.6
64
64
  signing_key:
65
65
  specification_version: 4
66
66
  summary: Standard health checks for Rails and Rack applications