pubsub_client 0.1.0 → 1.0.0
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 +4 -4
- data/CHANGELOG.txt +7 -0
- data/README.md +4 -11
- data/lib/pubsub_client.rb +3 -26
- data/lib/pubsub_client/null_publisher_factory.rb +1 -1
- data/lib/pubsub_client/publisher_factory.rb +12 -11
- data/lib/pubsub_client/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13c10d53daf5538c3aca168f2cee4ba8926bc6796bf77653fb9b9a87d2b26f72
|
4
|
+
data.tar.gz: eb7a89953c274694319285cb9350803d7b13f31a99e2bd1d365e074460ab06ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81e5cbdad71787603ab5d5a6d2ac7802a7cc8e9bb40fe4414d5c87a4935ca266ad0afa0efb0fa48b03b2f8ccdaefe3963054614e6b25db359381758849e82e60
|
7
|
+
data.tar.gz: 289d015b198b809a9683b5d7594ffcbd98a70ce964a2ab4471ab42bba3f83e0de1dbe0a3bd29cbd19ea5498e6f4e85a171658a7a60f326393d019361125dd7f6
|
data/CHANGELOG.txt
ADDED
data/README.md
CHANGED
@@ -20,30 +20,23 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Configuration
|
22
22
|
|
23
|
-
In order to use this gem, the environment variable `GOOGLE_APPLICATION_CREDENTIALS` must be set and point to the credentials JSON file.
|
24
|
-
- `topic_name` (required unless stubbed) - name of the Google Cloud Pub/Sub topic to publish messages to.
|
23
|
+
In order to use this gem, the environment variable `GOOGLE_APPLICATION_CREDENTIALS` must be set and point to the credentials JSON file.
|
25
24
|
|
26
|
-
If there are environments where setting up credentials is too burdensome and/or publishing messages is not desired, `PubsubClient` can be stubbed out with `PubsubClient.stub
|
27
|
-
|
28
|
-
E.g.
|
25
|
+
If there are environments where setting up credentials is too burdensome and/or publishing messages is not desired, `PubsubClient` can be stubbed out with `PubsubClient.stub!`, e.g.
|
29
26
|
|
30
27
|
```ruby
|
31
28
|
if test_env?
|
32
29
|
PubsubClient.stub!
|
33
|
-
else
|
34
|
-
PubsubClient.configure do |config|
|
35
|
-
config.topic_name = 'some-topic'
|
36
|
-
end
|
37
30
|
end
|
38
31
|
```
|
39
32
|
|
40
33
|
## Usage
|
41
34
|
|
42
|
-
To publish a message to Pub/Sub, call `PubsubClient.publish(message)`. This method takes any serializable object as an argument and yields a result object to a block. The `result` object has a method `#succeeded?` that returns `true` if the message was successfully published, otherwise `false`. In the latter case, there is a method `#error` that returns the error.
|
35
|
+
To publish a message to Pub/Sub, call `PubsubClient.publish(message, 'the-topic')`. This method takes any serializable object as an argument and yields a result object to a block. The `result` object has a method `#succeeded?` that returns `true` if the message was successfully published, otherwise `false`. In the latter case, there is a method `#error` that returns the error.
|
43
36
|
|
44
37
|
### Example
|
45
38
|
```ruby
|
46
|
-
PubsubClient.publish(message) do |result|
|
39
|
+
PubsubClient.publish(message, 'some-topic') do |result|
|
47
40
|
if result.succeeded?
|
48
41
|
puts 'yay!'
|
49
42
|
else
|
data/lib/pubsub_client.rb
CHANGED
@@ -4,40 +4,17 @@ require 'pubsub_client/publisher_factory'
|
|
4
4
|
|
5
5
|
module PubsubClient
|
6
6
|
Error = Class.new(StandardError)
|
7
|
-
CredentialsError = Class.new(Error)
|
8
7
|
ConfigurationError = Class.new(Error)
|
9
8
|
|
10
|
-
Config = Struct.new(:topic_name)
|
11
|
-
|
12
9
|
class << self
|
13
|
-
def configure(&block)
|
14
|
-
raise ConfigurationError, 'PubsubClient is already configured' if @publisher_factory
|
15
|
-
|
16
|
-
unless ENV['GOOGLE_APPLICATION_CREDENTIALS']
|
17
|
-
raise CredentialsError, 'GOOGLE_APPLICATION_CREDENTIALS must be set.'
|
18
|
-
end
|
19
|
-
|
20
|
-
config = Config.new
|
21
|
-
yield config
|
22
|
-
|
23
|
-
unless config.topic_name
|
24
|
-
raise ConfigurationError, 'The topic_name must be configured.'
|
25
|
-
end
|
26
|
-
|
27
|
-
@publisher_factory = PublisherFactory.new(config.topic_name)
|
28
|
-
end
|
29
|
-
|
30
10
|
def stub!
|
31
11
|
raise ConfigurationError, 'PubsubClient is already configured' if @publisher_factory
|
32
12
|
@publisher_factory = NullPublisherFactory.new
|
33
13
|
end
|
34
14
|
|
35
|
-
def publish(message, &block)
|
36
|
-
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
@publisher_factory.build.publish(message, &block)
|
15
|
+
def publish(message, topic, &block)
|
16
|
+
@publisher_factory ||= PublisherFactory.new
|
17
|
+
@publisher_factory.build(topic).publish(message, &block)
|
41
18
|
end
|
42
19
|
end
|
43
20
|
end
|
@@ -5,12 +5,12 @@ require_relative 'publisher'
|
|
5
5
|
module PubsubClient
|
6
6
|
# Build and memoize the Publisher, accounting for GRPC's requirements around forking.
|
7
7
|
class PublisherFactory
|
8
|
-
def initialize
|
9
|
-
@topic_name = topic_name
|
8
|
+
def initialize
|
10
9
|
@mutex = Mutex.new
|
10
|
+
@publishers = {}
|
11
11
|
end
|
12
12
|
|
13
|
-
def build
|
13
|
+
def build(topic_name)
|
14
14
|
# GRPC fails when attempting to use a connection created in a process that gets
|
15
15
|
# forked with the message
|
16
16
|
#
|
@@ -20,27 +20,28 @@ module PubsubClient
|
|
20
20
|
# PubSub.
|
21
21
|
#
|
22
22
|
# To prevent incurring overhead, memoize the publisher per process.
|
23
|
-
return
|
23
|
+
return publishers[topic_name].publisher if publishers[topic_name]&.pid == current_pid
|
24
24
|
|
25
25
|
# We are in a multi-threaded world and need to be careful not to build the publisher
|
26
26
|
# in multiple threads. Lock the mutex so that only one thread can enter this block
|
27
27
|
# at a time.
|
28
|
-
mutex.synchronize do
|
28
|
+
@mutex.synchronize do
|
29
29
|
# It's possible two threads made it to this point, but since we have a lock we
|
30
30
|
# know that one will have built the publisher before the second is able to enter.
|
31
31
|
# If we detect that case, then bail out so as to not rebuild the publisher.
|
32
|
-
unless
|
33
|
-
|
34
|
-
@publisher_pid = Process.pid
|
32
|
+
unless publishers[topic_name]&.pid == current_pid
|
33
|
+
publishers[topic_name] = Memo.new(build_publisher(topic_name), Process.pid)
|
35
34
|
end
|
36
35
|
end
|
37
36
|
|
38
|
-
|
37
|
+
publishers[topic_name].publisher
|
39
38
|
end
|
40
39
|
|
41
40
|
private
|
42
41
|
|
43
|
-
|
42
|
+
attr_accessor :publishers
|
43
|
+
|
44
|
+
Memo = Struct.new(:publisher, :pid)
|
44
45
|
|
45
46
|
# Used for testing to simulate when a process is forked. In those cases,
|
46
47
|
# this helps us test that the `.build` method creates different publishers.
|
@@ -48,7 +49,7 @@ module PubsubClient
|
|
48
49
|
Process.pid
|
49
50
|
end
|
50
51
|
|
51
|
-
def build_publisher
|
52
|
+
def build_publisher(topic_name)
|
52
53
|
pubsub = Google::Cloud::PubSub.new
|
53
54
|
topic = pubsub.topic(topic_name)
|
54
55
|
publisher = Publisher.new(topic)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pubsub_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Apartment List Platforms
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- ".ci"
|
77
77
|
- ".gitignore"
|
78
78
|
- ".rspec"
|
79
|
+
- CHANGELOG.txt
|
79
80
|
- CODEOWNERS
|
80
81
|
- CODE_OF_CONDUCT.md
|
81
82
|
- Gemfile
|
@@ -113,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
114
|
- !ruby/object:Gem::Version
|
114
115
|
version: '0'
|
115
116
|
requirements: []
|
116
|
-
rubygems_version: 3.0.
|
117
|
+
rubygems_version: 3.0.8
|
117
118
|
signing_key:
|
118
119
|
specification_version: 4
|
119
120
|
summary: Google Pub/Sub Wrapper
|