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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd21afb5b92c9e7c0e712459ca3f3af0ce3fe823
4
- data.tar.gz: 1c5868fa247da36d91d14b176586620e6c7a1298
3
+ metadata.gz: e4861900561edde1a7a43bbb8319ec89c980e428
4
+ data.tar.gz: 732c4291171910e6d85041533906978f81737319
5
5
  SHA512:
6
- metadata.gz: 3fe0b6acbf267039e172a49f8022e9d1a90ea8f33b7e472f7fb442b645a9bfe8105bc5151943fc7fb309fd6abcbf0ce6c9d066658190a7d590cdb5e2f49bdead
7
- data.tar.gz: 07cc6ca0d115cf69cd8e189756583dbe2325a74ac90bfc7cc27c9326df41c1f079f73a267e4f2b7d637a9e870000a5d7a683a72f54efccaf8e3231193de015e5
6
+ metadata.gz: 3f96fcd1767372afbb7148322391588db0d88de704896a7fcc832ed4d630ebdd5992ede361817d4c9c959d227ad84733d6ddf784853c16722e7478fa5c773f6b
7
+ data.tar.gz: a72191d83e658f7c98f32fdc17deab84f5dfc8f1ab157bddc71c7ba453ca7e3c3d0c13fa23ce038d9c886da7060d6469dafcab0f79efe117b27fa6cacf1af0a1
@@ -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 [MalformedMessage] if the message from Rafka cannot be parsed
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
- msg = @redis.blpop(@topic, timeout: timeout)
41
+ raised = false
42
+ msg = nil
42
43
 
43
- return if !msg
44
+ Rafka.wrap_errors do
45
+ msg = @redis.blpop(@topic, timeout: timeout)
46
+ end
44
47
 
45
- msg = Message.new(msg)
48
+ return if !msg
46
49
 
47
50
  begin
48
- raised = false
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
- @redis.rpush("acks", "#{msg.topic}:#{msg.partition}:#{msg.offset}")
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 MalformedMessage < Error
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
@@ -6,8 +6,10 @@ module Rafka
6
6
  @redis.ping
7
7
  end
8
8
 
9
+ # Closes the connection.
10
+ #
9
11
  # @see https://redis.io/commands/quit
10
- def quit
12
+ def close
11
13
  @redis.quit
12
14
  end
13
15
  end
data/lib/rafka/message.rb CHANGED
@@ -5,7 +5,7 @@ module Rafka
5
5
 
6
6
  def initialize(msg)
7
7
  if !msg.is_a?(Array) || msg.size != 8
8
- raise MalformedMessage.new(msg)
8
+ raise MalformedMessageError.new(msg)
9
9
  end
10
10
 
11
11
  @topic = msg[1]
@@ -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 a non-blocking operation.
23
+ # Produce a message. This is an asynchronous operation.
22
24
  #
23
25
  # @param topic [String]
24
- # @param message [#to_s]
26
+ # @param msg [#to_s]
25
27
  #
26
28
  # @example
27
29
  # produce("greetings", "Hello there!")
28
- def produce(topic, message)
29
- @redis.rpushx("topics:#{topic}", message.to_s)
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
- @redis.dump(timeout_ms.to_s)
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
- options = DEFAULTS.dup.merge(opts).merge(opts[:redis_opts])
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
@@ -1,3 +1,3 @@
1
1
  module Rafka
2
- VERSION = "0.0.1".freeze
2
+ VERSION = "0.0.2".freeze
3
3
  end
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.1
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-26 00:00:00.000000000 Z
11
+ date: 2017-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis