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
cloudenvoy_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

Instance Method Summary collapse

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.

Parameters:

  • msg_args (Array<any>) (defaults to: nil)

    The list of payload args.



133
134
135
# File 'lib/cloudenvoy/publisher.rb', line 133

def initialize(msg_args: nil)
  @msg_args = msg_args || []
end

#loggerLogger, any

Return the Cloudenvoy logger instance.

Returns:

  • (Logger, any)

    The cloudenvoy logger.



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.

Parameters:

  • *_args (Any)

    The publisher arguments.

Returns:

  • (Hash)

    The message attributes.



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.

Returns:



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_durationFloat

Return the time taken (in seconds) to format and publish the message. This duration includes the middlewares and the actual publish method.

Returns:

  • (Float)

    The time taken in seconds as a floating point number.



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.

Parameters:

  • *_args (Any)

    The publisher arguments.

Returns:

  • (String)

    The topic name.



148
149
150
# File 'lib/cloudenvoy/publisher.rb', line 148

def topic(*_args)
  self.class.default_topic
end