Module: Cloudenvoy::Publisher
- Defined in:
- lib/cloudenvoy/publisher.rb
Overview
Use this module to define publishers. The module provides a simple DSL for transforming and publishing data objects to Pub/Sub.
Publishers must at least implement the `payload` method, which defines how arguments are mapped to a Hash or String message.
E.g.
class UserPublisher
include Cloudenvoy::Publisher
# Specify publishing options
topic: 'my-topic'
# Format message objects
def payload(user)
{
id: user.id,
name: user.name
}
end
end
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
Add class method to including class.
Instance Method Summary collapse
-
#initialize(msg_args: nil) ⇒ Object
Build a new publisher instance.
-
#logger ⇒ Logger, any
Return the Cloudenvoy logger instance.
-
#metadata(*_args) ⇒ Hash
Publisher can optionally define message attributes (metadata).
-
#publish(&block) ⇒ Cloudenvoy::Message
Send the instantiated Publisher (message) to Pub/Sub.
-
#publishing_duration ⇒ Float
Return the time taken (in seconds) to format and publish the message.
-
#topic(*_args) ⇒ String
Return the topic to publish to.
Class Method Details
.included(base) ⇒ Object
Add class method to including class
30 31 32 33 34 35 36 |
# File 'lib/cloudenvoy/publisher.rb', line 30 def self.included(base) base.extend(ClassMethods) base.attr_accessor :msg_args, :message, :publishing_started_at, :publishing_ended_at # Register subscriber Cloudenvoy.publishers.add(base) end |
Instance Method Details
#initialize(msg_args: nil) ⇒ Object
Build a new publisher instance.
133 134 135 |
# File 'lib/cloudenvoy/publisher.rb', line 133 def initialize(msg_args: nil) @msg_args = msg_args || [] end |
#logger ⇒ Logger, any
Return the Cloudenvoy logger instance.
170 171 172 |
# File 'lib/cloudenvoy/publisher.rb', line 170 def logger @logger ||= PublisherLogger.new(self) end |
#metadata(*_args) ⇒ Hash
Publisher can optionally define message attributes (metadata). Message attributes are sent to Pub/Sub and can be used for filtering.
161 162 163 |
# File 'lib/cloudenvoy/publisher.rb', line 161 def (*_args) {} end |
#publish(&block) ⇒ Cloudenvoy::Message
Send the instantiated Publisher (message) to Pub/Sub.
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/cloudenvoy/publisher.rb', line 192 def publish(&block) # Format and publish message resp = execute_middleware_chain(&block) # Log job completion and return result logger.info("Published message in #{publishing_duration}s") { { duration: publishing_duration } } resp rescue StandardError => e logger.info("Publishing failed after #{publishing_duration}s") { { duration: publishing_duration } } raise(e) end |
#publishing_duration ⇒ Float
Return the time taken (in seconds) to format and publish the message. This duration includes the middlewares and the actual publish method.
180 181 182 183 184 |
# File 'lib/cloudenvoy/publisher.rb', line 180 def publishing_duration return 0.0 unless publishing_ended_at && publishing_started_at (publishing_ended_at - publishing_started_at).ceil(3) end |
#topic(*_args) ⇒ String
Return the topic to publish to. The topic name can be dynamically evaluated at runtime based on publishing arguments.
Defaults to the topic specified via cloudenvoy_options.
148 149 150 |
# File 'lib/cloudenvoy/publisher.rb', line 148 def topic(*_args) self.class.default_topic end |