umbra-rb 0.1.2.pre → 0.1.3.pre

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: e4684500b81274dd2138addcd92553e0da823b5b26a357ddecb277032315a70d
4
- data.tar.gz: 5766bac48f8ff39c374dd44b2e93553483b2d8af72419ac1d49665526d336fd5
3
+ metadata.gz: f02fb8bd32e28add55e66431d5d04b94586e66245c31a75a6e6d391781680d11
4
+ data.tar.gz: 65b2eeed6b27b8e72966102e534e7d29cb46bc639acacff46f63b79e7a112564
5
5
  SHA512:
6
- metadata.gz: 20cd0d4cb5a5e1f700c6eb0fec91f984519c77e55fa0ed3388a1d3be23ce9c315226ebf651301fb58e5518c07ba907519be40d305aa336a437e3f6184d282120
7
- data.tar.gz: e8128423446eab2d6615665b7d5aacaf5bf6c106e0093e2d7a2583d10e19a3ce9ca89596a5e067db0a51cc9060b952b590e6161d668740674f7e5281fccc07ef
6
+ metadata.gz: 592160621a75b39df6055bce8e7356167df48ab6eedcbf65432f17e1b42622ac2ed7dfeacb36d252d78398789406630c55af3bb741d0152562d8e39e24ff45f2
7
+ data.tar.gz: 7fc6eec4562a0cd1174cd7b60f4dc820b4dd350ea02ac5866cb2b2341af5fd47704ccfcfba10cd443f2d802211244efb57ffb19a046ec2bbdbf1aa7f4cd4e686
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- umbra-rb (0.1.1.pre)
4
+ umbra-rb (0.1.3.pre)
5
5
  multi_json (~> 1.13)
6
6
  oj (~> 3.9)
7
7
  redis (~> 4.1)
data/lib/umbra.rb CHANGED
@@ -27,6 +27,8 @@ module Umbra
27
27
 
28
28
  def configure(&block)
29
29
  @config = Config.default(&block)
30
+
31
+ test_redis_connection!
30
32
  end
31
33
 
32
34
  def publish(env, response)
@@ -34,6 +36,8 @@ module Umbra
34
36
  return unless @config
35
37
  return unless @config.request_selector.call(env, response)
36
38
 
39
+ env.merge!('umbra.request_body' => request_body(env))
40
+
37
41
  @config.publisher.call(env, response)
38
42
  rescue StandardError => e
39
43
  @config.error_handler.call(e, env, response)
@@ -47,6 +51,10 @@ module Umbra
47
51
  @config.encoder
48
52
  end
49
53
 
54
+ def logger
55
+ @config.logger
56
+ end
57
+
50
58
  def reset!
51
59
  @config = nil
52
60
  @redis = nil
@@ -54,6 +62,27 @@ module Umbra
54
62
 
55
63
  private
56
64
 
65
+ def request_body(env)
66
+ io = env.fetch('rack.input')
67
+ io.rewind
68
+ body = io.read
69
+ io.rewind
70
+
71
+ body
72
+ end
73
+
74
+ def test_redis_connection!
75
+ logger.info '[umbra] Testing redis connection...'
76
+ redis.ping
77
+ logger.info '[umbra] redis is alive!'
78
+ rescue Redis::BaseError => e
79
+ logger.warn "[umbra] redis while connecting to redis: #{e.message}"
80
+ reset!
81
+ rescue StandardError => e
82
+ logger.warn "[umbra] redis is misconfigured: #{e.message}"
83
+ reset!
84
+ end
85
+
57
86
  def umbra_request?(env)
58
87
  env[HEADER_KEY] == HEADER_VALUE
59
88
  end
data/lib/umbra/config.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Umbra
2
2
  class Config
3
- attr_accessor :publisher, :request_selector, :encoder, :error_handler, :redis_options
3
+ attr_accessor :publisher, :request_selector, :encoder, :error_handler, :redis_options, :logger
4
4
 
5
5
  def self.default(&block)
6
6
  new(
@@ -9,6 +9,7 @@ module Umbra
9
9
  encoder: Encoder,
10
10
  error_handler: SuppressErrorHandler,
11
11
  redis_options: {},
12
+ logger: Logger.new(STDOUT),
12
13
  &block
13
14
  )
14
15
  end
@@ -21,6 +22,7 @@ module Umbra
21
22
  @encoder = opts.fetch(:encoder)
22
23
  @error_handler = opts.fetch(:error_handler)
23
24
  @redis_options = opts.fetch(:redis_options)
25
+ @logger = opts.fetch(:logger)
24
26
 
25
27
  yield(self) if block_given?
26
28
  end
data/lib/umbra/encoder.rb CHANGED
@@ -40,13 +40,7 @@ module Umbra
40
40
  end
41
41
 
42
42
  def request_body
43
- io = @env.fetch('rack.input')
44
-
45
- io.rewind
46
- body = io.read
47
- io.rewind
48
-
49
- body
43
+ @env.fetch('umbra.request_body')
50
44
  end
51
45
 
52
46
  def body_string
@@ -6,7 +6,10 @@ module Umbra
6
6
  def call(env, response)
7
7
  start_once!
8
8
 
9
- return if @queue.size > MAX_QUEUE_SIZE
9
+ if @queue.size > MAX_QUEUE_SIZE
10
+ Umbra.logger.warn '[umbra] Publish queue at max - dropping items'
11
+ return
12
+ end
10
13
 
11
14
  @queue.push(proc { super(env, response) })
12
15
  end
@@ -17,6 +20,8 @@ module Umbra
17
20
  LOCK.synchronize do
18
21
  return if @started == Process.pid
19
22
 
23
+ Umbra.logger.info '[umbra] Starting publishing thread'
24
+
20
25
  @started = Process.pid
21
26
  @queue = Queue.new
22
27
 
@@ -27,6 +32,7 @@ module Umbra
27
32
  begin
28
33
  x.call
29
34
  rescue StandardError => e
35
+ Umbra.logger.warn '[umbra] Error in publishing thread'
30
36
  Umbra.config.error_handler.call(e)
31
37
  end
32
38
  end
@@ -12,7 +12,10 @@ module Umbra
12
12
  def call(env)
13
13
  start_worker!
14
14
 
15
- return if @queue.size > @max_queue_size
15
+ if @queue.size > @max_queue_size
16
+ Umbra.logger.warn '[umbra] Shadowing queue at max - dropping items'
17
+ return
18
+ end
16
19
 
17
20
  request = RequestBuilder.call(env)
18
21
 
@@ -26,15 +29,19 @@ module Umbra
26
29
  return if @started
27
30
 
28
31
  @started = true
32
+ Umbra.logger.info '[umbra] Starting shadowing threads...'
29
33
 
30
- workers = (1..@pool).map do |_|
34
+ workers = (1..@pool).map do |thread_num|
31
35
  Thread.new do
36
+ Umbra.logger.info "[umbra] shadow thread #{thread_num} waiting"
37
+
32
38
  while (request = @queue.pop)
33
39
  break if request == @stop
34
40
 
35
41
  begin
36
42
  request.run
37
43
  rescue StandardError => e
44
+ Umbra.logger.warn "[umbra] error in shadow thread #{thread_num}"
38
45
  Umbra.config.error_handler.call(e)
39
46
  end
40
47
  end
data/lib/umbra/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Umbra
4
- VERSION = '0.1.2.pre'
4
+ VERSION = '0.1.3.pre'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: umbra-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2.pre
4
+ version: 0.1.3.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - carwow Developers