Sha256: fbc26e8817cd0401587a119e9c479463aba640a5ae3fce41e2a7d79be40d0461

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require 'proletariat/concerns/logging'

module Proletariat
  # Public: Receives messages and publishes them to a RabbitMQ topic exchange.
  class Publisher
    include Concerns::Logging

    # Public: Creates a new Publisher instance.
    def initialize
      @channel  = Proletariat.connection.create_channel
      @exchange = channel.topic(Proletariat.exchange_name, durable: true)
    end

    # Public: Logs the 'online' status of the publisher.
    #
    # Returns nil.
    def started
      log_info 'Now online'

      nil
    end

    # Public: Logs the 'offline' status of the publisher.
    #
    # Returns nil.
    def stopped
      log_info 'Now offline'

      nil
    end

    # Public: Logs the 'shutting down' status of the publisher.
    #
    # Returns nil.
    def stopping
      log_info 'Attempting graceful shutdown.'

      nil
    end

    # Public: Push a message to a RabbitMQ topic exchange.
    #
    # to      - The routing key for the message to as a String. In accordance
    #           with the RabbitMQ convention you can use the '*' character to
    #           replace one word and the '#' to replace many words.
    # message - The message as a String.
    #
    # Returns nil.
    def work(to, message)
      exchange.publish message, routing_key: to, persistent: true

      nil
    end

    private

    # Internal: Returns the Bunny::Channel in use.
    attr_reader :channel

    # Internal: Returns the Bunny::Exchange in use.
    attr_reader :exchange
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proletariat-0.0.4 lib/proletariat/publisher.rb
proletariat-0.0.3 lib/proletariat/publisher.rb