Investigating sync edge cases.

pull/527/head
Rick Carlino 2017-11-05 11:36:24 -06:00
parent 5451b3564d
commit 7f3b6efe3b
6 changed files with 24 additions and 9 deletions

View File

@ -93,7 +93,7 @@ private
@current_device
else
@current_device = (current_user.try(:device) || no_device)
Thread.current[:device] = @current_device # Mutable state eww
Device.current = @current_device # Mutable state eww
@current_device
end
end

View File

@ -1,7 +1,7 @@
class CreateAttachmentFromUrlJob < ApplicationJob
queue_as :default
def perform(image:,attachment_url:)
def perform(image:, attachment_url:)
image.set_attachment_by_url(attachment_url)
image.save!
rescue => e

View File

@ -45,7 +45,15 @@ class Device < ApplicationRecord
Thread.current[:device]
end
def self.current=(dev)
Thread.current[:device] = dev
end
def self.current_jwt
Thread.current[:jwt]
end
def self.mine # For development mode debugging.
User.find_by(email: "admin@admin.com").device
end
end

View File

@ -44,6 +44,7 @@ class Image < ApplicationRecord
def set_attachment_by_url(url)
# Image.new.set_attachment_by_url("http://i.imgur.com/OhLresv.png").save!
Device.current = self.device
self.attachment = open(url)
self.attachment_processed_at = Time.now
self

View File

@ -21,4 +21,8 @@ class Log < ApplicationRecord
self.meta ||= {}
self.meta[:type] ||= "info"
end
def broadcast? # Logs get their own special channel. Don't echo twice!
Device.current && (destroyed? || notable_changes?)
end
end

View File

@ -2,14 +2,16 @@
# change protocols
module Transport
AMQP_URL = ENV['CLOUDAMQP_URL'] ||
ENV['RABBITMQ_URL'] ||
"amqp://guest:guest@localhost:5672"
AMQP_URL = ENV['CLOUDAMQP_URL'] ||
ENV['RABBITMQ_URL'] ||
"amqp://guest:guest@localhost:5672"
AMQP_OPTIONS = { read_timeout: 10,
heartbeat: 10,
log_level: 'info' }
def self.connection
@connection ||= Bunny
.new(AMQP_URL, read_timeout: 10, heartbeat: 10)
.start
@connection ||= Bunny.new(AMQP_URL, AMQP_OPTIONS).start
end
def self.topic
@ -20,6 +22,6 @@ module Transport
end
def self.send(message, id, channel)
topic.publish(message, routing_key: "bot.device_#{id}.#{channel}")
topic.publish(message, routing_key: "bot.device_#{id}.#{channel}")
end
end