class ActivePubsub::Publisher

Attributes

publishable_model_count[RW]
started[RW]
connection[RW]

Public Class Methods

increment_publishable_model_count!() click to toggle source
# File lib/active_pubsub/publisher.rb, line 18
def self.increment_publishable_model_count!
  self.publishable_model_count += 1
end
new() click to toggle source

Instance Methods ###

# File lib/active_pubsub/publisher.rb, line 33
def initialize
  connection
end
start() click to toggle source
# File lib/active_pubsub/publisher.rb, line 22
def self.start
  supervise_as :rabbit_publisher

  self.started = true
end
started?() click to toggle source
# File lib/active_pubsub/publisher.rb, line 28
def self.started?
  self.started
end

Public Instance Methods

channel() click to toggle source
# File lib/active_pubsub/publisher.rb, line 46
def channel
  connection.channel
end
clear_connections!() click to toggle source
# File lib/active_pubsub/publisher.rb, line 41
def clear_connections!
  channel.close
  connection.close
end
exchanges() click to toggle source
# File lib/active_pubsub/publisher.rb, line 50
def exchanges
  @exchanges ||= {}
end
options_for_publish(event) click to toggle source
# File lib/active_pubsub/publisher.rb, line 54
def options_for_publish(event)
  {
    :routing_key => event.routing_key,
    :persistent => ::ActivePubsub.config.durable
  }
end
publish_event(event) click to toggle source
# File lib/active_pubsub/publisher.rb, line 61
def publish_event(event)
  return if ::ActivePubsub.publisher_disabled?

  ::ActiveRecord::Base.connection_pool.with_connection do
    ::ActivePubsub.logger.info("Publishing event: #{event.id} to #{event.routing_key}")

    exchanges[event.exchange].publish(serialize_event(event), options_for_publish(event))
  end
end
register_exchange(exchange_name) click to toggle source
# File lib/active_pubsub/publisher.rb, line 75
def register_exchange(exchange_name)
  exchanges[exchange_name] ||= channel.topic(exchange_name, exchange_settings)
end
serialize_event(event) click to toggle source
# File lib/active_pubsub/publisher.rb, line 71
def serialize_event(event)
  ::Marshal.dump(event)
end