lib/pwwka/transmitter.rb in pwwka-0.0.2 vs lib/pwwka/transmitter.rb in pwwka-0.1.0
- old
+ new
@@ -1,38 +1,57 @@
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 SuckerPunch::Job
+ 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.async.send_message!(payload, routing_key)
- info "BACKGROUND AFTER Transmitting Message on #{routing_key} -> #{payload}"
+ 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)
- begin
- send_message!(payload, routing_key)
- rescue => e
- error "Error Transmitting Message on #{routing_key} -> #{payload}: #{e}"
- return false
- end
+ send_message!(payload, routing_key)
+ rescue => e
+ error "ERROR Transmitting Message on #{routing_key} -> #{payload}: #{e}"
+ false
end
- # send message asynchronously using sucker_punch
- # call async.send_message!
def send_message!(payload, routing_key)
- self.class.info "BACKGROUND START Transmitting Message on #{routing_key} -> #{payload}"
+ 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
- self.class.info "BACKGROUND END Transmitting Message on #{routing_key} -> #{payload}"
- return true
+ info "END Transmitting Message on #{routing_key} -> #{payload}"
+ true
end
-
end
-
end