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