protobuf-nats 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e444dd3193486c805f44d670ca6aceca10f7df3
4
- data.tar.gz: 2f0ed9b4431fc0379f41348fca8820dc57953421
3
+ metadata.gz: 6da90fadf74510112e08d690f747aa2f1dbebb1e
4
+ data.tar.gz: dde2e57a8cdc900f029e8931cd6153b6e3bf6904
5
5
  SHA512:
6
- metadata.gz: b99622db5416e0626861531c8777f9207a23b5a3afba48211db0bf312f92fee1f49e71eeca0dc5621614b6452c439c96c329546281b823c2563a41a9f167f6bd
7
- data.tar.gz: a0a52c9c909f552a2b01bfce3eb0a9fc18b2fd424f99c9c8301182526dd9ea69bac3bbdab5977f3b1f06f6efe79230ceb9fe332973d921dea86439e7b94fbdbe
6
+ metadata.gz: 4339ab9bac17df4f2e7b5f33ef5ba93fc4c6e4896c7d84fbd319df503e3790cd6927fbc2c241779fc66c1f7c77438d9b298b923038edf9bfe017ae52274f923c
7
+ data.tar.gz: 71ff1d34845258b508960d7b5ca8a6ca218d02659ecf4a8513a14d9420a09d44a190a4149822679fbd37b810e6fedde89d3b4cfec56f3880832a5713cb805e70
@@ -9,7 +9,8 @@ module Protobuf
9
9
  # may need to override to setup connection at this stage ... may also do on load of class
10
10
  super
11
11
 
12
- ::Protobuf::Nats.ensure_client_nats_connection_was_started
12
+ # This will ensure the client is started.
13
+ ::Protobuf::Nats.start_client_nats_connection
13
14
  end
14
15
 
15
16
  def close_connection
@@ -34,10 +34,7 @@ module Protobuf
34
34
  # Publish response.
35
35
  nats.publish(reply_id, response_data)
36
36
  end.on_error do |error|
37
- logger.error error.to_s
38
- if error.respond_to?(:backtrace) && error.backtrace.is_a?(::Array)
39
- logger.error error.backtrace.join("\n")
40
- end
37
+ log_error(error)
41
38
  end.execute
42
39
 
43
40
  # Publish an ACK to signal the server has picked up the work.
@@ -48,6 +45,13 @@ module Protobuf
48
45
  nil
49
46
  end
50
47
 
48
+ def log_error(error)
49
+ logger.error error.to_s
50
+ if error.respond_to?(:backtrace) && error.backtrace.is_a?(::Array)
51
+ logger.error error.backtrace.join("\n")
52
+ end
53
+ end
54
+
51
55
  def subscribe_to_services
52
56
  logger.info "Creating subscriptions:"
53
57
 
@@ -77,6 +81,14 @@ module Protobuf
77
81
  logger.warn "Disconnected from NATS server!"
78
82
  end
79
83
 
84
+ nats.on_error do |error|
85
+ log_error(error)
86
+ end
87
+
88
+ nats.on_close do
89
+ logger.warn "NATS connection was closed!"
90
+ end
91
+
80
92
  subscribe_to_services
81
93
 
82
94
  yield if block_given?
@@ -86,12 +98,14 @@ module Protobuf
86
98
  sleep 1
87
99
  end
88
100
 
101
+ logger.info "Unsubscribing from rpc routes..."
89
102
  subscriptions.each do |subscription_id|
90
103
  nats.unsubscribe(subscription_id)
91
104
  end
92
105
 
106
+ logger.info "Waiting up to 60 seconds for the thread pool to finish shutting down..."
93
107
  thread_pool.shutdown
94
- thread_pool.wait_for_termination
108
+ thread_pool.wait_for_termination(60)
95
109
  ensure
96
110
  @stopped = true
97
111
  end
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module Nats
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
data/lib/protobuf/nats.rb CHANGED
@@ -22,6 +22,8 @@ module Protobuf
22
22
  ACK = "\1".freeze
23
23
  end
24
24
 
25
+ GET_CONNECTED_MUTEX = ::Mutex.new
26
+
25
27
  def self.config
26
28
  @config ||= begin
27
29
  config = ::Protobuf::Nats::Config.new
@@ -40,17 +42,20 @@ module Protobuf
40
42
  end
41
43
 
42
44
  def self.start_client_nats_connection
43
- @client_nats_connection = ::NATS::IO::Client.new
44
- @client_nats_connection.connect(config.connection_options)
45
+ @start_client_nats_connection ||= begin
46
+ GET_CONNECTED_MUTEX.synchronize do
47
+ return if @start_client_nats_connection
45
48
 
46
- # Ensure we have a valid connection to the NATS server.
47
- @client_nats_connection.flush(5)
49
+ @client_nats_connection = ::NATS::IO::Client.new
50
+ @client_nats_connection.connect(config.connection_options)
48
51
 
49
- true
50
- end
52
+ # Ensure we have a valid connection to the NATS server.
53
+ @client_nats_connection.flush(5)
51
54
 
52
- def self.ensure_client_nats_connection_was_started
53
- @ensure_client_nats_connection_was_started ||= start_client_nats_connection
55
+ true
56
+ end
57
+ end
54
58
  end
59
+
55
60
  end
56
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-nats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dewitt