pubsub_client 1.2.0 → 1.3.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
  SHA256:
3
- metadata.gz: f957900bdf24e83c57067bb4e1d21c0010bf0e7d6aacc45da639c2250c7fa1b4
4
- data.tar.gz: 036d770cee6fa93d718bb5aa91236825eb8bfe510293a0e352c611c46645bed0
3
+ metadata.gz: a8c6a6eb0e136b4bb41d2a66b41f456ef0a86d7aa8668f22dfb8ea5092d54585
4
+ data.tar.gz: f9400d0ff957f6f6e1e54cc76478cf9a1e186d0ea5410fd6e5335d192f163427
5
5
  SHA512:
6
- metadata.gz: 6f52651a2b7b2f7279266a96850a84e4ca3639d454ce5f6fa437276b545c8c79e5d7fa99cddd760f9b0987c18739003eca20efbeadebe17e9fa794c042f8c3b5
7
- data.tar.gz: 8157eaf01bef5317607e77b687f107fa3579a1a4b251bc8e6d4922a600d177274436a9302130642de2f6f8d86f9f79ca6f42bed5b3a973b92f52874194e68d92
6
+ metadata.gz: 161710fca1c0efab48a708e6b61a3b619528b6f024eb4ae1b2fa063e484d0975400acb9b3cc300d032142cf535e5e5648aa3a7d64c8ba6557aabe75912231ed4
7
+ data.tar.gz: dbe4e34c082e49c4ac1843e733ff51fb848cc256e1242a074d9ce7367f0279613397c28bcad9fdea46b519d722841484bfd4ea2402c5f447c0eff8a8296125e2
@@ -1,3 +1,14 @@
1
+ Version 1.3.0 2020-10-23
2
+ ------------------------
3
+ e9e87f7 This adds the ability to subscribe to a Pub/Sub topic. Of note, we expose two additional configuration params:
4
+ - `concurrency`: the number of threads the subscriber will run to process messages (defaults to 8 threads)
5
+ - `auto_ack`: flag to auto ack messages (default is `true` and _will_ ack messages)
6
+
7
+ These changes come with a handful of useful checks:
8
+ - ensures credentials are configured prior to subscribing
9
+ - raises an error if the target subscription does not exist
10
+ - raises an error if attempting to subscribe to a topic that has already been subscribed to
11
+
1
12
  Version 1.2.0 2020-09-25
2
13
  ------------------------
3
14
  aede3ab Raise custom error if GOOGLE_APPLICATION_CREDENTIALS not set
data/README.md CHANGED
@@ -32,9 +32,11 @@ end
32
32
 
33
33
  ## Usage
34
34
 
35
+ ### Publishing
36
+
35
37
  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.
36
38
 
37
- ### Example
39
+ #### Example
38
40
  ```ruby
39
41
  PubsubClient.publish(message, 'some-topic') do |result|
40
42
  if result.succeeded?
@@ -45,6 +47,36 @@ PubsubClient.publish(message, 'some-topic') do |result|
45
47
  end
46
48
  ```
47
49
 
50
+ ### Subscribing
51
+
52
+ To subscribe to a topic, a client must first get a handle to the subscriber object. After doing so, a call to `subscriber.listener` will yield two arguments: the data (most clients will only need this) and the full Pub/Sub message (for anything more robust). Documentation for the full message can be found [here](https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub/ReceivedMessage.html).
53
+
54
+ Optionally, a client can choose to handle exceptions raised by the subscriber. If a client chooses to do so, the listener **must** be configured before `on_error` since the latter needs a handler to the underlying listener.
55
+
56
+ #### Example
57
+ ```ruby
58
+ subscriber = PubsubClient.subscriber('some-topic')
59
+
60
+ subscriber.listener(concurrency: 4, auto_ack: false) do |data, received_message|
61
+ # Most clients will only need the first yielded arg.
62
+ # It is the same as calling received_message.data
63
+ end
64
+
65
+ # Optional
66
+ subscriber.on_error do |ex|
67
+ # Do something with the exception.
68
+ end
69
+
70
+ subscriber.subscribe # This will sleep
71
+ ```
72
+
73
+ By default, the underlying subscriber will use a concurrency of `8` threads and will acknowledge all messages. If these defaults are acceptable to the client, no arguments need to be passed in the call to `listener`.
74
+ ```ruby
75
+ subscriber.listener do |data, received_message|
76
+ # Do something
77
+ end
78
+ ```
79
+
48
80
  ## Development
49
81
 
50
82
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,26 +1,46 @@
1
1
  require 'pubsub_client/version'
2
2
  require 'pubsub_client/null_publisher_factory'
3
+ require 'pubsub_client/null_subscriber_factory'
3
4
  require 'pubsub_client/publisher_factory'
5
+ require 'pubsub_client/subscriber_factory'
4
6
 
5
7
  module PubsubClient
6
8
  Error = Class.new(StandardError)
7
9
  ConfigurationError = Class.new(Error)
8
10
  CredentialsError = Class.new(Error)
9
11
  InvalidTopicError = Class.new(Error)
12
+ InvalidSubscriptionError = Class.new(Error)
10
13
 
11
14
  class << self
12
15
  def stub!
13
- raise ConfigurationError, 'PubsubClient is already configured' if @publisher_factory
16
+ raise ConfigurationError, 'PubsubClient is already configured' if @publisher_factory || @subscriber_factory
14
17
  @publisher_factory = NullPublisherFactory.new
18
+ @subscriber_factory = NullSubscriberFactory.new
15
19
  end
16
20
 
21
+ # @param message [String] The message to publish.
22
+ # @param topic [String] The name of the topic to publish to.
17
23
  def publish(message, topic, &block)
18
- unless ENV['GOOGLE_APPLICATION_CREDENTIALS']
19
- raise CredentialsError, 'GOOGLE_APPLICATION_CREDENTIALS must be set'
20
- end
24
+ ensure_credentials!
21
25
 
22
26
  @publisher_factory ||= PublisherFactory.new
23
27
  @publisher_factory.build(topic).publish(message, &block)
24
28
  end
29
+
30
+ # @param subscription [String] - The name of the topic to subscribe to.
31
+ def subscriber(subscription)
32
+ ensure_credentials!
33
+
34
+ @subscriber_factory ||= SubscriberFactory.new
35
+ @subscriber_factory.build(subscription)
36
+ end
37
+
38
+ private
39
+
40
+ def ensure_credentials!
41
+ unless ENV['GOOGLE_APPLICATION_CREDENTIALS']
42
+ raise CredentialsError, 'GOOGLE_APPLICATION_CREDENTIALS must be set'
43
+ end
44
+ end
25
45
  end
26
46
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PubsubClient
4
+ # A null object to act as a subscriber when clients are in dev or test
5
+ class NullSubscriber
6
+ # This adds a subset of the available methods on the
7
+ # Google::Cloud::PubSub::ReceivedMessage, which is what
8
+ # gets yielded by the subscription when configuring the listener.
9
+ # For a list of methods, see the following link:
10
+ # https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub/ReceivedMessage.html
11
+ NullResult = Struct.new(:acknowledge!) do
12
+ def data
13
+ '{"key":"value"}'
14
+ end
15
+
16
+ def published_at
17
+ Time.now
18
+ end
19
+ end
20
+
21
+ def listener(*, &block)
22
+ res = NullResult.new
23
+ yield res.data, res
24
+ end
25
+
26
+ def subscribe
27
+ # no-op
28
+ end
29
+
30
+ def on_error(&block)
31
+ # no-op
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'null_subscriber'
4
+
5
+ module PubsubClient
6
+ # A null object to act as a subscriber factory when clients are in dev or test
7
+ class NullSubscriberFactory
8
+ def build(*)
9
+ NullSubscriber.new
10
+ end
11
+ end
12
+ end
@@ -4,6 +4,7 @@ require 'google/cloud/pubsub'
4
4
 
5
5
  module PubsubClient
6
6
  class Publisher
7
+ # @param topic [Google::Cloud::PubSub::Topic]
7
8
  def initialize(topic)
8
9
  @topic = topic
9
10
  end
@@ -10,6 +10,8 @@ module PubsubClient
10
10
  @publishers = {}
11
11
  end
12
12
 
13
+ # @param topic_name [String]
14
+ # @return [Publisher]
13
15
  def build(topic_name)
14
16
  # GRPC fails when attempting to use a connection created in a process that gets
15
17
  # forked with the message
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'google/cloud/pubsub'
4
+
5
+ module PubsubClient
6
+ class Subscriber
7
+ DEFAULT_CONCURRENCY = 8
8
+
9
+ # @param subscription [Google::Cloud::PubSub::Subscription]
10
+ def initialize(subscription)
11
+ @subscription = subscription
12
+ end
13
+
14
+ # @param concurrency [Integer] - The number of threads to run the subscriber with. Default is 8.
15
+ # @param auto_ack [Boolean] - Flag to acknowledge the Pub/Sub message. A message must be acked
16
+ # to remove it from the topic. Default is `true`.
17
+ #
18
+ # @return [Google::Cloud::PubSub::Subscriber]
19
+ def listener(concurrency: DEFAULT_CONCURRENCY, auto_ack: true, &block)
20
+ @listener ||= begin
21
+ @subscription.listen(threads: { callback: concurrency }) do |received_message|
22
+ yield received_message.data, received_message
23
+ received_message.acknowledge! if auto_ack
24
+ end
25
+ end
26
+ end
27
+
28
+ def subscribe
29
+ raise ConfigurationError, 'A listener must be configured' unless @listener
30
+
31
+ begin
32
+ @listener.start
33
+
34
+ sleep
35
+ rescue SignalException
36
+ @listener.stop.wait!
37
+ end
38
+ end
39
+
40
+ def on_error(&block)
41
+ raise ConfigurationError, 'A listener must be configured' unless @listener
42
+
43
+ @listener.on_error do |exception|
44
+ yield exception
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'subscriber'
4
+
5
+ module PubsubClient
6
+ class SubscriberFactory
7
+ def initialize
8
+ @subscribers = {}
9
+ end
10
+
11
+ # @param subscription_name [String]
12
+ # @retrun [Subscriber]
13
+ def build(subscription_name)
14
+ if @subscribers.key?(subscription_name)
15
+ raise ConfigurationError, "PubsubClient already subscribed to #{subscription_name}"
16
+ end
17
+
18
+ @subscribers[subscription_name] = build_subscriber(subscription_name)
19
+ end
20
+
21
+ private
22
+
23
+ def build_subscriber(subscription_name)
24
+ pubsub = Google::Cloud::PubSub.new
25
+ subscription = pubsub.subscription(subscription_name)
26
+ raise InvalidSubscriptionError, "The subscription #{subscription_name} does not exist" unless subscription
27
+ Subscriber.new(subscription)
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module PubsubClient
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
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: 1.2.0
4
+ version: 1.3.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-09-29 00:00:00.000000000 Z
11
+ date: 2020-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -88,8 +88,12 @@ files:
88
88
  - lib/pubsub_client.rb
89
89
  - lib/pubsub_client/null_publisher.rb
90
90
  - lib/pubsub_client/null_publisher_factory.rb
91
+ - lib/pubsub_client/null_subscriber.rb
92
+ - lib/pubsub_client/null_subscriber_factory.rb
91
93
  - lib/pubsub_client/publisher.rb
92
94
  - lib/pubsub_client/publisher_factory.rb
95
+ - lib/pubsub_client/subscriber.rb
96
+ - lib/pubsub_client/subscriber_factory.rb
93
97
  - lib/pubsub_client/version.rb
94
98
  - pubsub_client.gemspec
95
99
  homepage: https://github.com/apartmentlist/pubsub_client