Class: Hyperloop::Connection
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?
do_not_synchronize, do_not_synchronize?
Class Attribute Details
.transport ⇒ Object
Returns the value of attribute transport.
106
107
108
|
# File 'lib/hyper-operation/transport/connection.rb', line 106
def transport
@transport
end
|
Class Method Details
108
109
110
111
112
113
114
|
# File 'lib/hyper-operation/transport/connection.rb', line 108
def active
if Hyperloop.on_server?
expired.delete_all
refresh_connections if needs_refresh?
end
all.pluck(:channel).uniq
end
|
.build_tables ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/hyper-operation/transport/connection.rb', line 60
def self.build_tables
create_table(force: true) 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|
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
89
90
91
|
# File 'lib/hyper-operation/transport/connection.rb', line 89
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_connections ⇒ Object
161
162
163
164
165
166
167
168
169
|
# File 'lib/hyper-operation/transport/connection.rb', line 161
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)
end
inactive.delete_all
end
|
.root_path ⇒ Object
155
156
157
158
159
|
# File 'lib/hyper-operation/transport/connection.rb', line 155
def root_path
QueuedMessage.root_path
rescue
nil
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
#transport ⇒ Object
93
94
95
|
# File 'lib/hyper-operation/transport/connection.rb', line 93
def transport
self.class.transport
end
|