Cache bunny connection
parent
5588f76d1b
commit
72d94c6eb0
|
@ -1,6 +1,7 @@
|
|||
class ApplicationRecord < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
after_save :maybe_broadcast, on: [:create, :update, :destroy]
|
||||
after_save :maybe_broadcast, on: [:create, :update]
|
||||
after_destroy :maybe_broadcast
|
||||
|
||||
DONT_BROADCAST = [ "current_sign_in_at",
|
||||
"last_sign_in_at",
|
||||
|
@ -8,17 +9,22 @@ class ApplicationRecord < ActiveRecord::Base
|
|||
"updated_at",
|
||||
"created_at" ]
|
||||
|
||||
def broadcast?
|
||||
Device.current &&
|
||||
!self.saved_changes.except(*self.class::DONT_BROADCAST).empty?
|
||||
# Determine if the changes to the model are worth broadcasting or not.
|
||||
# Reduces network noise.
|
||||
def notable_changes?
|
||||
!self.saved_changes.except(*self.class::DONT_BROADCAST).empty?
|
||||
end
|
||||
|
||||
def maybe_broadcast(*args_)
|
||||
def broadcast?
|
||||
Device.current && (destroyed? || notable_changes?)
|
||||
end
|
||||
|
||||
def maybe_broadcast
|
||||
self.broadcast! if broadcast?
|
||||
end
|
||||
|
||||
def broadcast_payload
|
||||
{ body: self.as_json }.to_json
|
||||
{ body: destroyed? ? "null" : self.as_json }.to_json
|
||||
end
|
||||
|
||||
def chan_name
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
# A wrapper around AMQP to stay DRY. Will make life easier if we ever need to
|
||||
# change protocols
|
||||
module Transport
|
||||
def self.topic
|
||||
@topic ||= Bunny
|
||||
.new(read_timeout: 10, heartbeat: 10)
|
||||
.start
|
||||
.create_channel
|
||||
.topic("amq.topic", auto_delete: true)
|
||||
end
|
||||
|
||||
def self.send(message, id, channel)
|
||||
Bunny
|
||||
.new
|
||||
.start
|
||||
.create_channel
|
||||
.topic("amq.topic", auto_delete: true)
|
||||
.publish(message, routing_key: "bot.device_#{id}.#{channel}")
|
||||
topic.publish(message, routing_key: "bot.device_#{id}.#{channel}")
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue