Class: SongkickQueue::Producer

Inherits:
Object
  • Object
show all
Defined in:
lib/songkick_queue/producer.rb

Instance Method Summary collapse

Constructor Details

#initializeProducer

Returns a new instance of Producer.



3
4
5
# File 'lib/songkick_queue/producer.rb', line 3

def initialize
  @client = Client.new
end

Instance Method Details

#publish(queue_name, payload, options = {}) ⇒ Object

Serializes the given message and publishes it to the default RabbitMQ exchange

Parameters:

  • queue_name (String)

    to publish to

  • message (#to_json)

    to serialize and enqueue

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

    a customizable set of options

Options Hash (options):

  • :message_id (String)

    to pass through to the consumer (will be logged)

  • :produced_at (String)

    time when the message was created, ISO8601 formatted



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/songkick_queue/producer.rb', line 14

def publish(queue_name, payload, options = {})
  message_id = options.fetch(:message_id) { SecureRandom.hex(6) }
  produced_at = options.fetch(:produced_at) { Time.now.utc.iso8601 }

  message = {
    message_id: message_id,
    produced_at: produced_at,
    payload: payload
  }

  message = JSON.generate(message)

  routing_key = [config.queue_namespace, queue_name].compact.join('.')

  client
    .default_exchange
    .publish(message, routing_key: routing_key)

  logger.info "Published message #{message_id} to '#{routing_key}' at #{produced_at}"
end