Sha256: bcf30154edd376a01e4475d7bbde10fe86e1faa325b447bd64ae0639a1549bd2

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

module Pwwka
  # Primary interface for sending messages.
  #
  # Example:
  #
  #     # Send a message, blowing up if there's any problem
  #     Pwwka::Transmitter.send_message!({ user_id: @user.id }, "users.user.activated")
  #
  #     # Send a message, logging if there's any problem
  #     Pwwka::Transmitter.send_message_safely({ user_id: @user.id }, "users.user.activated")
  class Transmitter

    extend Pwwka::Logging
    include Pwwka::Logging

    # Send an important message that must go through.  This method allows any raised exception 
    # to pass through.
    #
    # payload:: Hash of what you'd like to include in your message
    # routing_key:: String routing key for the message
    #
    # Returns true
    #
    # Raises any exception generated by the innerworkings of this library.
    def self.send_message!(payload, routing_key)
      new.send_message!(payload, routing_key)
      info "AFTER Transmitting Message on #{routing_key} -> #{payload}"
    end

    # Send a less important message that doesn't have to go through. This eats
    # any `StandardError` and logs it, returning false rather than blowing up.
    #
    # payload:: Hash of what you'd like to include in your message
    # routing_key:: String routing key for the message
    #
    # Returns true if the message was sent, false otherwise
    def self.send_message_safely(payload, routing_key)
      send_message!(payload, routing_key)
    rescue => e
      error "ERROR Transmitting Message on #{routing_key} -> #{payload}: #{e}"
      false
    end

    def send_message!(payload, routing_key)
      info "START Transmitting Message on #{routing_key} -> #{payload}"
      channel_connector = ChannelConnector.new 
      channel_connector.topic_exchange.publish(
        payload.to_json,
        routing_key: routing_key,
        persistent: true)
      channel_connector.connection_close
      # if it gets this far it has succeeded
      info "END Transmitting Message on #{routing_key} -> #{payload}"
      true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pwwka-0.2.0 lib/pwwka/transmitter.rb
pwwka-0.1.0 lib/pwwka/transmitter.rb