redis-stream 0.4.4 → 0.4.5
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 +4 -4
- data/lib/redis/stream/client.rb +47 -45
- data/lib/redis/stream/data_cache.rb +53 -1
- data/lib/redis/stream/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83de723fa0769050b81548f00ce3d662b9053b1d89974b314f188756af759e1d
|
4
|
+
data.tar.gz: 83f878051c67908db42aa52928f2f3ba7746d9d7fd40a888dc9412f40db1fc82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd2618538cb98cd88861e76d3024e5bd54d241669e056763d2986989f6b50cfc7fb24a921aba4d08a6b2ecce005031ac53e9f8639411f991cbe7774fbfec34cc
|
7
|
+
data.tar.gz: e781755937ba2901eeafa7273bf7eea5ceb3bfa8a1c6f2011b196b3ae0695b80413342e3fa1b4ebd147eb08fae4f36e6c618685f29c37379f2844419684e2288
|
data/lib/redis/stream/client.rb
CHANGED
@@ -219,8 +219,8 @@ class Redis
|
|
219
219
|
payload = nil
|
220
220
|
|
221
221
|
unless @cache.nil?
|
222
|
-
if options.include?("cache_key")
|
223
|
-
cache_key = @cache.build_key(data)
|
222
|
+
if options.include?("cache_key") && not(options['cache_key'].nil?)
|
223
|
+
cache_key = options["cache_key"] # @cache.build_key(data)
|
224
224
|
if @cache.include?(cache_key)
|
225
225
|
if data && data.include?('from_cache') && data['from_cache'].eql?(0)
|
226
226
|
@cache.delete(cache_key)
|
@@ -237,14 +237,16 @@ class Redis
|
|
237
237
|
}
|
238
238
|
@logger.info("#{@consumer_id} - fetching from cache with key #{cache_key}")
|
239
239
|
end
|
240
|
-
|
240
|
+
else
|
241
|
+
#cache_key = @cache.build_key(data)
|
242
|
+
unless cache_key.nil? || cache_key.empty?
|
243
|
+
@logger.info("#{@consumer_id} - caching with key #{cache_key}")
|
244
|
+
@cache[cache_key] = data
|
245
|
+
end
|
241
246
|
end
|
242
|
-
else
|
243
|
-
@logger.info("#{@consumer_id} - fetching from cache with key #{cache_key}")
|
244
|
-
@cache[options["cache_key"]] = data
|
245
247
|
end
|
246
|
-
|
247
248
|
end
|
249
|
+
|
248
250
|
if payload.nil?
|
249
251
|
payload = {
|
250
252
|
type: type,
|
@@ -303,53 +305,53 @@ class Redis
|
|
303
305
|
# @param [Boolean] async return the message if synchronous else call handle_incoming
|
304
306
|
# @param [Boolean] passthrough Receive all messages also the ones intended for other consumers
|
305
307
|
def read_next_message_from_stream(async = true, passthrough = false)
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
begin
|
322
|
-
data_out["payload"] = JSON.parse(data_out["payload"])
|
323
|
-
rescue Exception => e
|
324
|
-
@logger.error("#{@consumer_id} error parsing payload: #{e.message}")
|
325
|
-
end
|
308
|
+
if @state == Redis::Stream::State::RUNNING
|
309
|
+
result = @redis.xread(@stream, @lastid, block: 1000, count: 1) if @group.nil?
|
310
|
+
result = @redis.xreadgroup(@group, @consumer_id, @stream, '>', block: 1000, count: 1) if @group
|
311
|
+
|
312
|
+
unless result.empty?
|
313
|
+
id, data_out = result[@stream][0]
|
314
|
+
ack_count = @redis.xack(@stream, @group, id) if @group
|
315
|
+
|
316
|
+
tracer_data = JSON.parse(data_out["tracer"])
|
317
|
+
unless tracer_data.nil? || tracer_data.empty?
|
318
|
+
#trace_scope.span.log_kv('has_tracer' => true)
|
319
|
+
tracer_span = OpenTracing.extract(OpenTracing::FORMAT_TEXT_MAP, tracer_data)
|
320
|
+
data_out["tracer"] = OpenTracing.start_span(@consumer_id, child_of: tracer_span)
|
321
|
+
end
|
326
322
|
|
327
|
-
|
328
|
-
|
329
|
-
|
323
|
+
begin
|
324
|
+
data_out["payload"] = JSON.parse(data_out["payload"])
|
325
|
+
rescue Exception => e
|
326
|
+
@logger.error("#{@consumer_id} error parsing payload: #{e.message}")
|
327
|
+
end
|
330
328
|
|
331
|
-
|
332
|
-
|
333
|
-
|
329
|
+
if data_out["from"].eql?(@consumer_id)
|
330
|
+
return false
|
331
|
+
end
|
334
332
|
|
335
|
-
|
336
|
-
|
333
|
+
unless (data_out["to"].nil? || data_out["to"].eql?('') || data_out["to"].eql?('*') || data_out["to"].eql?(@consumer_id)) &&
|
334
|
+
(data_out["to_group"].nil? || data_out["to_group"].eql?('') || data_out["to_group"].eql?('*') || data_out["to_group"].eql?(@group))
|
335
|
+
@logger.info("#{@consumer_id} - ignoring message from '#{data_out["from"]}' to '#{data_out["to"]}-#{data_out["to_group"]}'")
|
337
336
|
|
338
|
-
|
337
|
+
return false
|
338
|
+
end
|
339
339
|
|
340
|
-
|
341
|
-
add(data_out["payload"].to_s, "to" => data_out["from"], "group" => "*", "type" => Redis::Stream::Type::PONG)
|
342
|
-
return false
|
343
|
-
end
|
340
|
+
@logger.info("#{@consumer_id} - received from '#{data_out["from"]}' of type '#{data_out['type']}' to '#{data_out["to"]}' in group '#{data_out["to_group"]}' with message id '#{id}' - with ack #{ack_count}")
|
344
341
|
|
345
|
-
|
346
|
-
|
347
|
-
|
342
|
+
if data_out["type"].eql?(Redis::Stream::Type::PING)
|
343
|
+
add(data_out["payload"].to_s, "to" => data_out["from"], "group" => "*", "type" => Redis::Stream::Type::PONG)
|
344
|
+
return false
|
345
|
+
end
|
348
346
|
|
349
|
-
|
350
|
-
|
347
|
+
if data_out["type"].eql?(Redis::Stream::Type::PONG)
|
348
|
+
return false
|
351
349
|
end
|
350
|
+
|
351
|
+
return data_out unless async
|
352
|
+
handle_incoming(data_out)
|
352
353
|
end
|
354
|
+
end
|
353
355
|
rescue Exception => e
|
354
356
|
return false
|
355
357
|
end
|
@@ -5,11 +5,13 @@ require 'redis/stream/config'
|
|
5
5
|
class Redis
|
6
6
|
module Stream
|
7
7
|
class DataCache
|
8
|
-
def initialize
|
8
|
+
def initialize(logger = Logger.new(STDOUT))
|
9
|
+
@logger = logger
|
9
10
|
@cache = Moneta.new(:HashFile, dir: Redis::Stream::Config[:data_cache] || "/tmp/cache", serializer: :json)
|
10
11
|
end
|
11
12
|
|
12
13
|
def []=(key, value)
|
14
|
+
@logger.info("CACHE - #{File.basename(__FILE__)}:#{__LINE__} - caching with key #{key}")
|
13
15
|
@cache.store(key, value)
|
14
16
|
end
|
15
17
|
|
@@ -18,6 +20,10 @@ class Redis
|
|
18
20
|
end
|
19
21
|
|
20
22
|
def include?(key)
|
23
|
+
key?(key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def key?(key)
|
21
27
|
@cache.key?(key)
|
22
28
|
end
|
23
29
|
|
@@ -38,9 +44,55 @@ class Redis
|
|
38
44
|
id = payload_data["id"].downcase
|
39
45
|
key = "#{action}_#{id}"
|
40
46
|
end
|
47
|
+
raise "Empty cache key" if key.nil? || key&.empty?
|
41
48
|
key
|
42
49
|
end
|
43
50
|
|
51
|
+
def resolve_by_message(pid, payload, &block)
|
52
|
+
data = nil
|
53
|
+
cache_key = build_key(payload)
|
54
|
+
invalidate_cache(cache_key, payload)
|
55
|
+
data = load_from_cache(cache_key)
|
56
|
+
data = load_from_service(pid, cache_key, &block) if data.nil?
|
57
|
+
|
58
|
+
data
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def invalidate_cache(cache_key, payload)
|
63
|
+
if payload&.include?('from_cache') && payload['from_cache'].eql?('0')
|
64
|
+
delete(cache_key)
|
65
|
+
@logger.warn("CACHE - #{File.basename(__FILE__)}:#{__LINE__} - invalidating key #{cache_key}")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def load_from_cache(cache_key)
|
70
|
+
data = nil
|
71
|
+
if key?(cache_key)
|
72
|
+
@logger.info("CACHE - #{File.basename(__FILE__)}:#{__LINE__} - fetching with key #{cache_key}")
|
73
|
+
data = get_from_cache(cache_key)
|
74
|
+
end
|
75
|
+
data
|
76
|
+
end
|
77
|
+
|
78
|
+
def load_from_service(pid, cache_key)
|
79
|
+
data = nil
|
80
|
+
data = yield pid, cache_key if block_given?
|
81
|
+
|
82
|
+
if data.nil? || data&.empty?
|
83
|
+
@logger.warn("CACHE - #{File.basename(__FILE__)}:#{__LINE__} - empty result for key #{cache_key} and id #{pid}")
|
84
|
+
end
|
85
|
+
|
86
|
+
data
|
87
|
+
end
|
88
|
+
|
89
|
+
def get_from_cache(cache_key)
|
90
|
+
include?(cache_key)
|
91
|
+
|
92
|
+
data = self[cache_key]
|
93
|
+
data = get_from_cache(data['parent']) if data.key?('parent')
|
94
|
+
data
|
95
|
+
end
|
44
96
|
end
|
45
97
|
end
|
46
98
|
end
|
data/lib/redis/stream/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-stream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mehmet Celik
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|