Class: Uc3DmpEventBridge::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/uc3-dmp-event-bridge/publisher.rb

Overview

CLass to manage publication of messages on an EventBridge’s EventBus

Constant Summary collapse

SOURCE =
'Uc3DmpEventBridge::Publisher'
DEFAULT_EVENT_TYPE =
'DMP change'
MSG_BUS_ERROR =
'EventBus Error - %{msg} - %{trace}'
MSG_INVALID_KEY =
'Invalid message specified. Expecting Hash containing at least a `PK` and `SK`'
MSG_MISSING_BUS =
'No EventBus defined! Looking for `ENV[\'EVENT_BUS_NAME\']`'
MSG_MISSING_DOMAIN =
'No domain name defined! Looking for `ENV[\'DOMAIN\']`'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**_args) ⇒ Publisher

Returns a new instance of Publisher.

Raises:



19
20
21
22
23
24
25
26
# File 'lib/uc3-dmp-event-bridge/publisher.rb', line 19

def initialize(**_args)
  @bus = ENV.fetch('EVENT_BUS_NAME', nil)
  @domain = ENV.fetch('DOMAIN', nil)
  raise PublisherError, MSG_MISSING_BUS if @bus.nil?
  raise PublisherError, MSG_MISSING_DOMAIN if @domain.nil?

  @client = Aws::EventBridge::Client.new(region: ENV.fetch('AWS_REGION', 'us-west-2'))
end

Instance Attribute Details

#busObject

Returns the value of attribute bus.



17
18
19
# File 'lib/uc3-dmp-event-bridge/publisher.rb', line 17

def bus
  @bus
end

#clientObject

Returns the value of attribute client.



17
18
19
# File 'lib/uc3-dmp-event-bridge/publisher.rb', line 17

def client
  @client
end

#domainObject

Returns the value of attribute domain.



17
18
19
# File 'lib/uc3-dmp-event-bridge/publisher.rb', line 17

def domain
  @domain
end

Instance Method Details

#publish(source:, dmp:, event_type: DEFAULT_EVENT_TYPE, detail: nil, logger: nil) ⇒ Object

Publish an event to the EventBus so that other Lambdas can do their thing rubocop:disable Metrics/AbcSize, Metrics/MethodLength



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/uc3-dmp-event-bridge/publisher.rb', line 30

def publish(source:, dmp:, event_type: DEFAULT_EVENT_TYPE, detail: nil, logger: nil)
  source = "#{source} -> #{SOURCE}.publish"
  detail = if detail.nil?
             _generate_detail(dmp:).to_json
           else
             (detail.is_a?(Hash) ? detail.to_json : detail.to_s)
           end

  message = {
    entries: [{
      time: Time.now.utc.iso8601,
      source: "#{ENV.fetch('DOMAIN', nil)}:lambda:event_publisher",
      detail_type: event_type.to_s,
      detail:,
      event_bus_name: ENV.fetch('EVENT_BUS_NAME', nil)
    }]
  }
  logger.debug(message: "#{SOURCE} published event", details: message) if logger.respond_to?(:debug)
  resp = client.put_events(message)
  return true unless resp.failed_entry_count.nil? || resp.failed_entry_count.positive?

  # The EventBridge returned errors, so log the error
  raise PublisherError, _generate_failure(source:, response: resp, payload:)
rescue Aws::Errors::ServiceError => e
  logger.error(message: "#{SOURCE} #{e.message}", details: e.backtrace) if logger.respond_to?(:debug)
  raise PublisherError, format(MSG_BUS_ERROR, msg: e.message)
end