Class: Hyperloop::Connection

Inherits:
ActiveRecord::Base show all
Extended by:
AutoCreate
Defined in:
lib/hyper-operation/transport/connection.rb

Defined Under Namespace

Classes: QueuedMessage

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutoCreate

create_table, needs_init?, table_exists?

Methods inherited from ActiveRecord::Base

do_not_synchronize, do_not_synchronize?

Class Attribute Details

.transportObject

Returns the value of attribute transport.



102
103
104
# File 'lib/hyper-operation/transport/connection.rb', line 102

def transport
  @transport
end

Class Method Details

.activeObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hyper-operation/transport/connection.rb', line 104

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
end

.build_tablesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hyper-operation/transport/connection.rb', line 56

def self.build_tables
  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: :cascade) do |t|
    t.text    :data
    t.integer :connection_id
  end
end

.connect_to_transport(channel, session, root_path) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/hyper-operation/transport/connection.rb', line 135

def connect_to_transport(channel, session, root_path)
  self.root_path = root_path
  if (connection = find_by(channel: channel, session: session))
    messages = connection.messages.pluck(:data)
    connection.destroy
  else
    messages = []
  end
  open(channel)
  messages
end

.disconnect(channel) ⇒ Object



147
148
149
# File 'lib/hyper-operation/transport/connection.rb', line 147

def disconnect(channel)
  find_by(channel: channel, session: nil).destroy
end

.needs_refresh?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/hyper-operation/transport/connection.rb', line 85

def self.needs_refresh?
  exists?(['refresh_at IS NOT NULL AND refresh_at < ?', Time.zone.now])
end

.open(channel, session = nil, root_path = nil) ⇒ Object



116
117
118
119
# File 'lib/hyper-operation/transport/connection.rb', line 116

def open(channel, session = nil, root_path = nil)
  self.root_path = root_path
  find_or_create_by(channel: channel, session: session)
end

.read(session, root_path) ⇒ Object



128
129
130
131
132
133
# File 'lib/hyper-operation/transport/connection.rb', line 128

def read(session, root_path)
  self.root_path = root_path
  where(session: session)
    .update_all(expires_at: Time.now + transport.expire_polled_connection_in)
  QueuedMessage.for_session(session).destroy_all.pluck(:data)
end

.refresh_connectionsObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/hyper-operation/transport/connection.rb', line 162

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|
    connection = find_by(channel: channel, session: nil)
    connection.update(refresh_at: next_refresh) if connection
  end
  inactive.delete_all
end

.root_pathObject



155
156
157
158
159
160
# File 'lib/hyper-operation/transport/connection.rb', line 155

def root_path
  # 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

.root_path=(path) ⇒ Object



151
152
153
# File 'lib/hyper-operation/transport/connection.rb', line 151

def root_path=(path)
  QueuedMessage.root_path = path if path
end

.send_to_channel(channel, data) ⇒ Object



121
122
123
124
125
126
# File 'lib/hyper-operation/transport/connection.rb', line 121

def send_to_channel(channel, data)
  pending_for(channel).each do |connection|
    QueuedMessage.create(data: data, hyperloop_connection: connection)
  end
  transport.send_data(channel, data) if exists?(channel: channel, session: nil)
end

Instance Method Details

#transportObject



89
90
91
# File 'lib/hyper-operation/transport/connection.rb', line 89

def transport
  self.class.transport
end