Class: ActivePubsub::Publisher

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/active_pubsub/publisher.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePublisher

Instance Methods ###



34
35
36
# File 'lib/active_pubsub/publisher.rb', line 34

def initialize
  connection
end

Class Attribute Details

.publishable_model_countObject

Returns the value of attribute publishable_model_count.



13
14
15
# File 'lib/active_pubsub/publisher.rb', line 13

def publishable_model_count
  @publishable_model_count
end

.startedObject

Returns the value of attribute started.



12
13
14
# File 'lib/active_pubsub/publisher.rb', line 12

def started
  @started
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/active_pubsub/publisher.rb', line 7

def connection
  @connection
end

Class Method Details

.increment_publishable_model_count!Object



19
20
21
# File 'lib/active_pubsub/publisher.rb', line 19

def self.increment_publishable_model_count!
  self.publishable_model_count += 1
end

.startObject



23
24
25
26
27
# File 'lib/active_pubsub/publisher.rb', line 23

def self.start
  supervise_as :rabbit_publisher

  self.started = true
end

.started?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/active_pubsub/publisher.rb', line 29

def self.started?
  self.started
end

Instance Method Details

#channelObject



47
48
49
# File 'lib/active_pubsub/publisher.rb', line 47

def channel
  connection.channel
end

#clear_connections!Object



42
43
44
45
# File 'lib/active_pubsub/publisher.rb', line 42

def clear_connections!
  channel.close
  connection.close
end

#exchangesObject



51
52
53
# File 'lib/active_pubsub/publisher.rb', line 51

def exchanges
  @exchanges ||= {}
end

#publish_event(event) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/active_pubsub/publisher.rb', line 55

def publish_event(event)
  ::ActiveRecord::Base.connection_pool.with_connection do
    puts event.inspect

    exchanges[event.exchange].publish(serialize_event(event), :routing_key => event.routing_key)
  end
end

#register_exchange(exchange_name) ⇒ Object



67
68
69
# File 'lib/active_pubsub/publisher.rb', line 67

def register_exchange(exchange_name)
  exchanges[exchange_name] ||= channel.topic(exchange_name, :auto_delete => true)
end

#serialize_event(event) ⇒ Object



63
64
65
# File 'lib/active_pubsub/publisher.rb', line 63

def serialize_event(event)
  ::Marshal.dump(event)
end