urabbit 0.1.1 → 0.2.0

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: 75cd5cf7a7a2c13f4f848c30649f2c6af97af9eb
4
- data.tar.gz: 07941cff03f8c709c0b61f16d4de5e9b4e9283ad
3
+ metadata.gz: d799ad09ff9148e6dbaaeeef2081354dd3c080e6
4
+ data.tar.gz: 5c2dc55aea39d26681aa5bdda6512befc66dc008
5
5
  SHA512:
6
- metadata.gz: c77b3fa6a8322cf117d9b991ffe12cdd9d45f303c054773a29ed0203255cc145832d36e0791c7675bcfdfc963ac18b93d853f8c1f0ac9b411ab1cac201ee8243
7
- data.tar.gz: 3d769d50b61b9024d6e31ccd7f4f441a87e940e88b62def4533bf4f716cdd758d724cd6c82124cfd474e832d1a203d758eb01fdf790b922c16f55305a4d5b70a
6
+ metadata.gz: 3298896032c0fef9b2c4a6aae4055338b986448832f5d625e81ea2da8b6156f2f2e29bbf7bbb1b69299ed52103a434c77cb7ca844d0da96f82b3e7de28a94d27
7
+ data.tar.gz: 2db306043158f167e9494cd02ac5c371862715b18e4ed8e6cb360febf93c909fda27c6f4bb20ddacdb793a5c8239b0620ad55738440c2ff277a790e0944e3c3a
data/lib/urabbit.rb CHANGED
@@ -3,13 +3,9 @@ require "logger"
3
3
  require "json"
4
4
  require "securerandom"
5
5
 
6
- require "urabbit/version"
7
- require "urabbit/rpc"
8
- require "urabbit/rpc/server"
9
- require "urabbit/rpc/client"
10
- require "urabbit/publisher"
11
-
12
6
  module Urabbit
7
+ class Error < Exception; end
8
+
13
9
  def self.logger
14
10
  @logger ||= if defined?(Rails)
15
11
  Rails.logger
@@ -21,4 +17,23 @@ module Urabbit
21
17
  def self.logger=(logger)
22
18
  @logger = logger
23
19
  end
20
+
21
+ # A single connection shared between threads.
22
+ def self.connect(cloudamqp_url = ENV["CLOUDAMQP_URL"])
23
+ @connection = Bunny.new(cloudamqp_url, logger: logger)
24
+ @connection.start
25
+ @connection
26
+ rescue Bunny::Exception
27
+ raise Error.new("Error connecting to RabbitMQ")
28
+ end
29
+
30
+ def self.create_channel
31
+ @connection.create_channel
32
+ end
24
33
  end
34
+
35
+ require "urabbit/version"
36
+ require "urabbit/rpc"
37
+ require "urabbit/rpc/server"
38
+ require "urabbit/rpc/client"
39
+ require "urabbit/publisher"
@@ -13,8 +13,6 @@
13
13
  # Message is usually a JSON.
14
14
  # Exception can contain a cause raised from Bunny.
15
15
  class Urabbit::Publisher
16
- class Error < Exception; end
17
-
18
16
  def initialize(opts)
19
17
  cloudamqp_url = opts[:cloudamqp_url] || ENV["CLOUDAMQP_URL"]
20
18
  exchange_type = opts[:exchange_type] || :topic
@@ -23,9 +21,7 @@ class Urabbit::Publisher
23
21
  @routing_key = opts[:routing_key] ||
24
22
  raise(Error.new("Please provide a 'routing_key'"))
25
23
 
26
- @connection = Bunny.new(cloudamqp_url, logger: Urabbit.logger)
27
- @connection.start
28
- @channel = @connection.create_channel
24
+ @channel = Urabbit.create_channel
29
25
  @exchange = Bunny::Exchange.new(
30
26
  @channel,
31
27
  exchange_type,
@@ -18,14 +18,10 @@
18
18
  # can be thrown during initialization and method calls.
19
19
  # It can also contain a cause raised from Bunny itself.
20
20
  class Urabbit::RPC::Client
21
- class Error < Exception; end
22
- class ServerError < Error; end
21
+ class ServerError < Urabbit::Error; end
23
22
 
24
- def initialize(cloudamqp_url = ENV["CLOUDAMQP_URL"])
25
- @connection = Bunny.new(cloudamqp_url, logger: Urabbit.logger)
26
- @connection.start
27
-
28
- @channel = @connection.create_channel
23
+ def initialize
24
+ @channel = Urabbit.create_channel
29
25
  @exchange = @channel.default_exchange
30
26
  @reply_queue = @channel.queue("amq.rabbitmq.reply-to")
31
27
 
@@ -45,7 +41,7 @@ class Urabbit::RPC::Client
45
41
  end
46
42
  end
47
43
  rescue Bunny::Exception
48
- raise Error.new("Error connecting to queue")
44
+ raise Urabbit::Error.new("Error connecting to queue")
49
45
  end
50
46
 
51
47
  def call(routing_key, message, timeout = 10)
@@ -60,7 +56,7 @@ class Urabbit::RPC::Client
60
56
  @lock.synchronize{@condition.wait(@lock, timeout)}
61
57
 
62
58
  if @error.nil? && @result.nil?
63
- raise Error.new("Timed out waiting for reply. "\
59
+ raise Urabbit::Error.new("Timed out waiting for reply. "\
64
60
  "Make sure the RPC queue name is correct.")
65
61
  end
66
62
 
@@ -70,6 +66,6 @@ class Urabbit::RPC::Client
70
66
  @result
71
67
  end
72
68
  rescue Bunny::Exception
73
- raise Error.new("Error communicating with queue")
69
+ raise Urabbit::Error.new("Error communicating with queue")
74
70
  end
75
71
  end
@@ -1,17 +1,10 @@
1
1
  module Urabbit::RPC::Server
2
- def initialize(cloudamqp_url = ENV["CLOUDAMQP_URL"])
3
- @connection = Bunny.new(cloudamqp_url, logger: logger)
4
- @connection.start
5
-
6
- @channel = @connection.create_channel
7
-
8
- # TODO: Test which setting is the best
9
- # @channel.prefetch(1)
10
- end
2
+ SleepInterval = 1 # How often to check if server should stop.
11
3
 
12
4
  # TODO: Currently this is called directly, but it should
13
5
  # be called using server_engine.
14
6
  def start
7
+ @channel = Urabbit.create_channel
15
8
  logger.info("Starting RPC server for #{self.class.name}")
16
9
 
17
10
  @queue = @channel.queue(self.class.queue_name)
@@ -41,16 +34,16 @@ module Urabbit::RPC::Server
41
34
 
42
35
  # Subscribing in blocking mode above disables auto-reconnection feature.
43
36
  # It's better to just sleep.
44
- sleep
37
+ until(@should_stop) do
38
+ sleep(SleepInterval)
39
+ end
45
40
 
46
41
  logger.info("Stopped responding to RPC calls for #{self.class.name}")
47
42
  end
48
43
 
49
44
  # TODO: Use this method when server_engine is implemented.
50
45
  def stop
51
- @channel.close
52
- @connection.close
53
-
46
+ @should_stop = true
54
47
  logger.info("Stopped RPC server for #{self.class.name}")
55
48
  end
56
49
 
@@ -1,3 +1,3 @@
1
1
  module Urabbit
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: urabbit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacek Becela
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-26 00:00:00.000000000 Z
11
+ date: 2016-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler