pubcontrol 1.1.0 → 1.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: a8535c6bdbb6bfd0abeb75b7dc08a4d561bb679b
4
- data.tar.gz: 9f019cc934e4cebdd07d84b7f662503eb44d1661
3
+ metadata.gz: 36d18a43fb9eccdb7b0090e848c793d2e542832d
4
+ data.tar.gz: d7054ee29c3dc2c62042dae493d5de1ce96721ca
5
5
  SHA512:
6
- metadata.gz: f159d79255a34f0969f4ad71a9e00ec29518ec7e743298e35023b4037e91782e613fea8fc2bada8ff90475b0306841f207c39565523c8a43e58feff2bfadff00
7
- data.tar.gz: 6eef70a1cb4d6049218dc5d69347b74c85e200a9d563ca33a6cf7b44605d2db9f446d5b755dfca59767c675a94cfb0d30c1e6a785f864877ef43cfc275284adf
6
+ metadata.gz: 1c8fd3c684fde6b24183d29740f62ee5b91aebb27393a318caefd423615eef57f650b33a716a4e29dcc2de505e3db3760987b653b74dba7f88d22586dfc585ca
7
+ data.tar.gz: 51920a6f60f0e0686d7284704393c85d267fc9f11ff2dc8d1f80629e0d133b073037cfb3d9fad88208c0d166b9c5958c9660fab59247a07b0ec8c020f2e66e35
data/lib/pubcontrol.rb CHANGED
@@ -10,20 +10,34 @@ require_relative 'item.rb'
10
10
  require_relative 'pubcontrolclient.rb'
11
11
  require_relative 'pcccbhandler.rb'
12
12
 
13
+ # Register the close_pubcontrols method with at_exit to ensure that it is
14
+ # called on exit.
15
+ at_exit do
16
+ PubControl.close_pubcontrols
17
+ end
18
+
13
19
  # The PubControl class allows a consumer to manage a set of publishing
14
20
  # endpoints and to publish to all of those endpoints via a single publish
15
21
  # or publish_async method call. A PubControl instance can be configured
16
22
  # either using a hash or array of hashes containing configuration information
17
23
  # or by manually adding PubControlClient instances.
18
24
  class PubControl
25
+ # The global list of PubControl instances used to ensure that each instance
26
+ # is properly closed on exit.
27
+ @@pubcontrols = Array.new
28
+ @@lock = Mutex.new
19
29
 
20
30
  # Initialize with or without a configuration. A configuration can be applied
21
31
  # after initialization via the apply_config method.
22
32
  def initialize(config=nil)
23
33
  @clients = Array.new
34
+ @closed = false
24
35
  if !config.nil?
25
36
  apply_config(config)
26
37
  end
38
+ @@lock.synchronize do
39
+ @@pubcontrols.push(self)
40
+ end
27
41
  end
28
42
 
29
43
  # Remove all of the configured PubControlClient instances.
@@ -54,15 +68,40 @@ class PubControl
54
68
  end
55
69
  end
56
70
 
57
- # The finish method is a blocking method that ensures that all asynchronous
58
- # publishing is complete for all of the configured PubControlClient
59
- # instances prior to returning and allowing the consumer to proceed.
60
- def finish
71
+ # The close method is a blocking call that closes all ZMQ sockets and
72
+ # ensures that all PubControlClient async publishing is completed prior
73
+ # to returning and allowing the consumer to proceed. Note that the
74
+ # PubControl instance cannot be used after calling this method.
75
+ def close
76
+ close_clients
77
+ unregister_pubcontrol
78
+ end
79
+
80
+ # Internal close method, used during shutdown while class lock is held.
81
+ def close_locked
82
+ close_clients
83
+ unregister_pubcontrol_locked
84
+ end
85
+
86
+ # This method is a blocking method that ensures that all asynchronous
87
+ # publishing is complete for all of the configured client instances prior
88
+ # to returning and allowing the consumer to proceed.
89
+ # NOTE: This only applies to PubControlClient and not ZmqPubControlClient
90
+ # since all ZMQ socket operations are non-blocking.
91
+ def wait_all_sent
92
+ verify_not_closed
61
93
  @clients.each do |pub|
62
- pub.finish
94
+ pub.wait_all_sent
63
95
  end
64
96
  end
65
97
 
98
+ # DEPRECATED: The finish method is now deprecated in favor of the more
99
+ # descriptive wait_all_sent method.
100
+ def finish
101
+ verify_not_closed
102
+ wait_all_sent
103
+ end
104
+
66
105
  # The synchronous publish method for publishing the specified item to the
67
106
  # specified channels for all of the configured PubControlClient instances.
68
107
  def publish(channels, item)
@@ -87,4 +126,47 @@ class PubControl
87
126
  pub.publish_async(channels, item, cb)
88
127
  end
89
128
  end
129
+
130
+ # An internal method used for closing all existing PubControl instances.
131
+ def self.close_pubcontrols
132
+ @@lock.synchronize do
133
+ pubcontrols = Array.new(@@pubcontrols)
134
+ pubcontrols.each do |pub|
135
+ pub.close_locked
136
+ end
137
+ end
138
+ end
139
+
140
+ private
141
+
142
+ # An internal method for verifying that the PubControl instance has
143
+ # not been closed via the close() method. If it has then an error
144
+ # is raised.
145
+ def verify_not_closed
146
+ if @closed
147
+ raise 'pubcontrol instance is closed'
148
+ end
149
+ end
150
+
151
+ # Internal method to close clients.
152
+ def close_clients
153
+ verify_not_closed
154
+ @clients.each do |pub|
155
+ pub.close()
156
+ end
157
+ @closed = true
158
+ end
159
+
160
+ # Internal method to unregister from the list of pubcontrols.
161
+ def unregister_pubcontrol
162
+ @@lock.synchronize do
163
+ @@pubcontrols.delete(self)
164
+ end
165
+ end
166
+
167
+ # Internal method to unregister from the list of pubcontrols, used during
168
+ # shutdown while class lock is held.
169
+ def unregister_pubcontrol_locked
170
+ @@pubcontrols.delete(self)
171
+ end
90
172
  end
@@ -10,7 +10,7 @@ require 'thread'
10
10
  require 'base64'
11
11
  require 'jwt'
12
12
  require 'json'
13
- require 'net/http'
13
+ require 'net/http/persistent'
14
14
  require_relative 'item.rb'
15
15
 
16
16
  # The PubControlClient class allows consumers to publish either synchronously
@@ -34,6 +34,7 @@ class PubControlClient
34
34
  @auth_basic_pass = nil
35
35
  @auth_jwt_claim = nil
36
36
  @auth_jwt_key = nil
37
+ @http = Net::HTTP::Persistent.new @object_id.to_s
37
38
  end
38
39
 
39
40
  # Call this method and pass a username and password to use basic
@@ -91,10 +92,10 @@ class PubControlClient
91
92
  queue_req(['pub', uri, auth, exports, callback])
92
93
  end
93
94
 
94
- # The finish method is a blocking method that ensures that all asynchronous
95
+ # This method is a blocking method that ensures that all asynchronous
95
96
  # publishing is complete prior to returning and allowing the consumer to
96
97
  # proceed.
97
- def finish
98
+ def wait_all_sent
98
99
  @lock.synchronize do
99
100
  if !@thread.nil?
100
101
  queue_req(['stop'])
@@ -104,6 +105,19 @@ class PubControlClient
104
105
  end
105
106
  end
106
107
 
108
+ # DEPRECATED: The finish method is now deprecated in favor of the more
109
+ # descriptive wait_all_sent method.
110
+ def finish
111
+ wait_all_sent
112
+ end
113
+
114
+ # This method closes the PubControlClient instance by ensuring all pending
115
+ # data is sent and any open connections are closed.
116
+ def close
117
+ wait_all_sent
118
+ @http.shutdown
119
+ end
120
+
107
121
  # A helper method for returning the current UNIX UTC timestamp.
108
122
  def self.timestamp_utcnow
109
123
  return Time.now.utc.to_i
@@ -124,8 +138,7 @@ class PubControlClient
124
138
  request['Authorization'] = auth_header
125
139
  end
126
140
  request['Content-Type'] = 'application/json'
127
- use_ssl = uri.scheme == 'https'
128
- response = make_http_request(uri, use_ssl, request)
141
+ response = make_http_request(uri, request)
129
142
  if !response.kind_of? Net::HTTPSuccess
130
143
  raise 'failed to publish: ' + response.class.to_s + ' ' +
131
144
  response.message
@@ -133,12 +146,9 @@ class PubControlClient
133
146
  end
134
147
 
135
148
  # An internal method for making the specified HTTP request to the
136
- # specified URI with an option that determines whether to use
137
- # SSL.
138
- def make_http_request(uri, use_ssl, request)
139
- response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
140
- http.request(request)
141
- end
149
+ # specified URI.
150
+ def make_http_request(uri, request)
151
+ response = @http.request uri, request
142
152
  return response
143
153
  end
144
154
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pubcontrol
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Bokarius
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: net-http-persistent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.9'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.9'
41
55
  description: A Ruby convenience library for publishing messages using the EPCP protocol
42
56
  email: bokarius@comcast.net
43
57
  executables: []
@@ -69,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
83
  version: '0'
70
84
  requirements: []
71
85
  rubyforge_project:
72
- rubygems_version: 2.2.2
86
+ rubygems_version: 2.4.5
73
87
  signing_key:
74
88
  specification_version: 4
75
89
  summary: Ruby EPCP library