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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/umbra.rb +29 -0
- data/lib/umbra/config.rb +3 -1
- data/lib/umbra/encoder.rb +1 -7
- data/lib/umbra/publisher.rb +7 -1
- data/lib/umbra/shadow_requester.rb +9 -2
- data/lib/umbra/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f02fb8bd32e28add55e66431d5d04b94586e66245c31a75a6e6d391781680d11
|
4
|
+
data.tar.gz: 65b2eeed6b27b8e72966102e534e7d29cb46bc639acacff46f63b79e7a112564
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 592160621a75b39df6055bce8e7356167df48ab6eedcbf65432f17e1b42622ac2ed7dfeacb36d252d78398789406630c55af3bb741d0152562d8e39e24ff45f2
|
7
|
+
data.tar.gz: 7fc6eec4562a0cd1174cd7b60f4dc820b4dd350ea02ac5866cb2b2341af5fd47704ccfcfba10cd443f2d802211244efb57ffb19a046ec2bbdbf1aa7f4cd4e686
|
data/Gemfile.lock
CHANGED
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
data/lib/umbra/publisher.rb
CHANGED
@@ -6,7 +6,10 @@ module Umbra
|
|
6
6
|
def call(env, response)
|
7
7
|
start_once!
|
8
8
|
|
9
|
-
|
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
|
-
|
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