Module: Cloudenvoy::Publisher::ClassMethods
- Defined in:
- lib/cloudenvoy/publisher.rb
Overview
Module class methods
Instance Method Summary collapse
-
#cloudenvoy_options(opts = {}) ⇒ Hash
Set the publisher runtime options.
-
#cloudenvoy_options_hash ⇒ Hash
Return the publisher runtime options.
-
#default_topic ⇒ String
Return the default topic this publisher publishes to.
-
#publish(*args) ⇒ Cloudenvoy::Message
Format and publish objects to Pub/Sub.
-
#publish_all(arg_list) ⇒ Array<Cloudenvoy::Message>
Publish all messages in one batch.
-
#setup ⇒ Cloudenvoy::Topic
Setup the default topic for this publisher.
Instance Method Details
#cloudenvoy_options(opts = {}) ⇒ Hash
Set the publisher runtime options.
47 48 49 50 |
# File 'lib/cloudenvoy/publisher.rb', line 47 def (opts = {}) opt_list = opts&.map { |k, v| [k.to_sym, v] } || [] # symbolize @cloudenvoy_options_hash = Hash[opt_list] end |
#cloudenvoy_options_hash ⇒ Hash
Return the publisher runtime options.
57 58 59 |
# File 'lib/cloudenvoy/publisher.rb', line 57 def @cloudenvoy_options_hash || {} end |
#default_topic ⇒ String
Return the default topic this publisher publishes to. Raises an error if no default topic has been defined.
67 68 69 |
# File 'lib/cloudenvoy/publisher.rb', line 67 def default_topic [:topic] end |
#publish(*args) ⇒ Cloudenvoy::Message
Format and publish objects to Pub/Sub.
78 79 80 |
# File 'lib/cloudenvoy/publisher.rb', line 78 def publish(*args) new(msg_args: args).publish end |
#publish_all(arg_list) ⇒ Array<Cloudenvoy::Message>
Publish all messages in one batch.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/cloudenvoy/publisher.rb', line 100 def publish_all(arg_list) # Build the list of publishers publishers = arg_list.map { |e| new(msg_args: [e].flatten(1)) } # Batches are topic specific. We must send one batch of messages per topic. publishers.group_by { |e| e.topic(*e.msg_args) }.map do |topic, topic_publishers| # Chain publish calls. The very last call (when the chain becomes empty) # is the actual batch publishing of messages to pub/sub chain = topic_publishers.dup traverse_chain = lambda do if chain.empty? # Send the messages in one batch and retrospectively attach them # to each publisher arg_list = topic_publishers.map { |e| [e.payload(*e.msg_args), e.(*e.msg_args)] } PubSubClient.publish_all(topic, arg_list).each_with_index do |msg, pub_index| topic_publishers[pub_index]. = msg end else # Defer message publishing to the next element # in the chain until the chain is empty chain.shift.publish(&traverse_chain) end end traverse_chain.call end.flatten end |
#setup ⇒ Cloudenvoy::Topic
Setup the default topic for this publisher.
87 88 89 90 91 |
# File 'lib/cloudenvoy/publisher.rb', line 87 def setup return nil unless default_topic PubSubClient.upsert_topic(default_topic) end |