Module: Cloudenvoy::Publisher::ClassMethods

Defined in:
lib/cloudenvoy/publisher.rb

Overview

Module class methods

Instance Method Summary collapse

Instance Method Details

#cloudenvoy_options(opts = {}) ⇒ Hash

Set the publisher runtime options.

Parameters:

  • opts (Hash) (defaults to: {})

    The publisher options.

Returns:

  • (Hash)

    The options set.



47
48
49
50
# File 'lib/cloudenvoy/publisher.rb', line 47

def cloudenvoy_options(opts = {})
  opt_list = opts&.map { |k, v| [k.to_sym, v] } || [] # symbolize
  @cloudenvoy_options_hash = Hash[opt_list]
end

#cloudenvoy_options_hashHash

Return the publisher runtime options.

Returns:

  • (Hash)

    The publisher runtime options.



57
58
59
# File 'lib/cloudenvoy/publisher.rb', line 57

def cloudenvoy_options_hash
  @cloudenvoy_options_hash || {}
end

#default_topicString

Return the default topic this publisher publishes to. Raises an error if no default topic has been defined.

Returns:

  • (String)

    The default topic.



67
68
69
# File 'lib/cloudenvoy/publisher.rb', line 67

def default_topic
  cloudenvoy_options_hash[:topic]
end

#publish(*args) ⇒ Cloudenvoy::Message

Format and publish objects to Pub/Sub.

Parameters:

  • *args (Any)

    The publisher arguments

Returns:



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.

Parameters:

  • The (Array<Any>)

    list of publisher arguments.

Returns:



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].message = 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

#setupCloudenvoy::Topic

Setup the default topic for this publisher.

Returns:



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