rafka 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rafka/consumer.rb +11 -6
- data/lib/rafka/errors.rb +11 -1
- data/lib/rafka/generic_commands.rb +3 -1
- data/lib/rafka/message.rb +1 -1
- data/lib/rafka/producer.rb +13 -7
- data/lib/rafka/version.rb +1 -1
- data/lib/rafka.rb +13 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4861900561edde1a7a43bbb8319ec89c980e428
|
4
|
+
data.tar.gz: 732c4291171910e6d85041533906978f81737319
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f96fcd1767372afbb7148322391588db0d88de704896a7fcc832ed4d630ebdd5992ede361817d4c9c959d227ad84733d6ddf784853c16722e7478fa5c773f6b
|
7
|
+
data.tar.gz: a72191d83e658f7c98f32fdc17deab84f5dfc8f1ab157bddc71c7ba453ca7e3c3d0c13fa23ce038d9c886da7060d6469dafcab0f79efe117b27fa6cacf1af0a1
|
data/lib/rafka/consumer.rb
CHANGED
@@ -31,21 +31,24 @@ module Rafka
|
|
31
31
|
#
|
32
32
|
# @param timeout [Fixnum] the time in seconds to wait for a message
|
33
33
|
#
|
34
|
-
# @raise [
|
34
|
+
# @raise [MalformedMessageError] if the message cannot be parsed
|
35
35
|
#
|
36
36
|
# @return [nil, Message] the message, if any
|
37
37
|
#
|
38
38
|
# @example
|
39
39
|
# consume(5) { |msg| puts "I received #{msg.value}" }
|
40
40
|
def consume(timeout=5)
|
41
|
-
|
41
|
+
raised = false
|
42
|
+
msg = nil
|
42
43
|
|
43
|
-
|
44
|
+
Rafka.wrap_errors do
|
45
|
+
msg = @redis.blpop(@topic, timeout: timeout)
|
46
|
+
end
|
44
47
|
|
45
|
-
|
48
|
+
return if !msg
|
46
49
|
|
47
50
|
begin
|
48
|
-
|
51
|
+
msg = Message.new(msg)
|
49
52
|
yield(msg) if block_given?
|
50
53
|
rescue => e
|
51
54
|
raised = true
|
@@ -55,7 +58,9 @@ module Rafka
|
|
55
58
|
msg
|
56
59
|
ensure
|
57
60
|
if msg && !raised
|
58
|
-
|
61
|
+
Rafka.wrap_errors do
|
62
|
+
@redis.rpush("acks", "#{msg.topic}:#{msg.partition}:#{msg.offset}")
|
63
|
+
end
|
59
64
|
end
|
60
65
|
end
|
61
66
|
|
data/lib/rafka/errors.rb
CHANGED
@@ -2,7 +2,7 @@ module Rafka
|
|
2
2
|
class Error < StandardError
|
3
3
|
end
|
4
4
|
|
5
|
-
class
|
5
|
+
class MalformedMessageError < Error
|
6
6
|
def initialize(msg)
|
7
7
|
@msg = msg
|
8
8
|
end
|
@@ -11,4 +11,14 @@ module Rafka
|
|
11
11
|
"The message #{@msg.inspect} could not be parsed"
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
# Generic command error
|
16
|
+
class CommandError < Error
|
17
|
+
end
|
18
|
+
|
19
|
+
class ProduceError < CommandError
|
20
|
+
end
|
21
|
+
|
22
|
+
class ConsumeError < CommandError
|
23
|
+
end
|
14
24
|
end
|
data/lib/rafka/message.rb
CHANGED
data/lib/rafka/producer.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Rafka
|
2
2
|
class Producer
|
3
|
+
include GenericCommands
|
4
|
+
|
3
5
|
# Access the underlying Redis client object
|
4
6
|
attr_reader :redis
|
5
7
|
|
@@ -18,15 +20,17 @@ module Rafka
|
|
18
20
|
@redis = Redis.new(host: opts[:host], port: opts[:port])
|
19
21
|
end
|
20
22
|
|
21
|
-
# Produce a message. This is
|
23
|
+
# Produce a message. This is an asynchronous operation.
|
22
24
|
#
|
23
25
|
# @param topic [String]
|
24
|
-
# @param
|
26
|
+
# @param msg [#to_s]
|
25
27
|
#
|
26
28
|
# @example
|
27
29
|
# produce("greetings", "Hello there!")
|
28
|
-
def produce(topic,
|
29
|
-
|
30
|
+
def produce(topic, msg)
|
31
|
+
Rafka.wrap_errors do
|
32
|
+
@redis.rpushx("topics:#{topic}", msg.to_s)
|
33
|
+
end
|
30
34
|
end
|
31
35
|
|
32
36
|
# Flush any buffered messages. Blocks until all messages are flushed or
|
@@ -36,14 +40,16 @@ module Rafka
|
|
36
40
|
#
|
37
41
|
# @return [Fixnum] The number of unflushed messages
|
38
42
|
def flush(timeout_ms=5000)
|
39
|
-
|
43
|
+
Rafka.wrap_errors do
|
44
|
+
@redis.dump(timeout_ms.to_s)
|
45
|
+
end
|
40
46
|
end
|
41
47
|
|
42
48
|
private
|
43
49
|
|
50
|
+
# @return [Hash]
|
44
51
|
def parse_opts(opts)
|
45
|
-
|
46
|
-
options
|
52
|
+
DEFAULTS.dup.merge(opts).merge(opts[:redis_opts])
|
47
53
|
end
|
48
54
|
end
|
49
55
|
end
|
data/lib/rafka/version.rb
CHANGED
data/lib/rafka.rb
CHANGED
@@ -14,5 +14,18 @@ module Rafka
|
|
14
14
|
port: 6380,
|
15
15
|
reconnect_attempts: 0,
|
16
16
|
}
|
17
|
+
|
18
|
+
def self.wrap_errors
|
19
|
+
yield
|
20
|
+
rescue Redis::CommandError => e
|
21
|
+
case
|
22
|
+
when e.message.start_with?("PROD ")
|
23
|
+
raise ProduceError, e.message[5..-1]
|
24
|
+
when e.message.start_with?("CONS ")
|
25
|
+
raise ConsumeError, e.message[5..-1]
|
26
|
+
else
|
27
|
+
raise CommandError, e.message
|
28
|
+
end
|
29
|
+
end
|
17
30
|
end
|
18
31
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rafka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Agis Anastasopoulos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06
|
11
|
+
date: 2017-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|