lib/hyper-operation/transport/connection.rb in hyper-operation-0.5.12 vs lib/hyper-operation/transport/connection.rb in hyper-operation-0.99.0
- old
+ new
@@ -1,24 +1,20 @@
module Hyperloop
module AutoCreate
- def needs_init?
- return false if Hyperloop.transport == :none
- return true if connection.respond_to?(:data_sources) && !connection.data_sources.include?(table_name)
- return true if !connection.respond_to?(:data_sources) && !connection.tables.include?(table_name)
- return false unless Hyperloop.on_server?
- return true if defined?(Rails::Server)
- return true unless Connection.root_path
- uri = URI("#{Connection.root_path}server_up")
- http = Net::HTTP.new(uri.host, uri.port)
- request = Net::HTTP::Get.new(uri.path)
- if uri.scheme == 'https'
- http.use_ssl = true
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ def table_exists?
+ # works with both rails 4 and 5 without deprecation warnings
+ if connection.respond_to?(:data_sources)
+ connection.data_sources.include?(table_name)
+ else
+ connection.tables.include?(table_name)
end
- http.request(request) && return rescue true
end
+ def needs_init?
+ Hyperloop.transport != :none && Hyperloop.on_server? && !table_exists?
+ end
+
def create_table(*args, &block)
connection.create_table(table_name, *args, &block) if needs_init?
end
end
@@ -56,18 +52,18 @@
end
extend AutoCreate
def self.build_tables
- create_table(force: true) do |t|
+ create_table(force: :cascade) do |t|
t.string :channel
t.string :session
t.datetime :created_at
t.datetime :expires_at
t.datetime :refresh_at
end
- QueuedMessage.create_table(force: true) do |t|
+ QueuedMessage.create_table(force: :cascade) do |t|
t.text :data
t.integer :connection_id
end
end
@@ -104,10 +100,14 @@
class << self
attr_accessor :transport
def active
+ # if table doesn't exist then we are either calling from within
+ # a migration or from a console before the server has ever started
+ # in these cases there are no channels so we return nothing
+ return [] unless table_exists?
if Hyperloop.on_server?
expired.delete_all
refresh_connections if needs_refresh?
end
all.pluck(:channel).uniq
@@ -151,20 +151,22 @@
def root_path=(path)
QueuedMessage.root_path = path if path
end
def root_path
- QueuedMessage.root_path
- rescue
- nil
+ # if the QueuedMessage table doesn't exist then we are either calling from within
+ # a migration or from a console before the server has ever started
+ # in these cases there is no root path to the server
+ QueuedMessage.root_path if QueuedMessage.table_exists?
end
def refresh_connections
refresh_started_at = Time.zone.now
channels = transport.refresh_channels
next_refresh = refresh_started_at + transport.refresh_channels_every
channels.each do |channel|
- find_by(channel: channel, session: nil).update(refresh_at: next_refresh)
+ connection = find_by(channel: channel, session: nil)
+ connection.update(refresh_at: next_refresh) if connection
end
inactive.delete_all
end
end
end