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.
104
105
106
|
# File 'lib/hyper-operation/transport/connection.rb', line 104
def transport
@transport
end
|
Class Method Details
106
107
108
109
110
111
112
|
# File 'lib/hyper-operation/transport/connection.rb', line 106
def active
if Hyperloop.on_server?
expired.delete_all
refresh_connections if needs_refresh?
end
all.pluck(:channel).uniq
end
|
.build_tables ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/hyper-operation/transport/connection.rb', line 58
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
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/hyper-operation/transport/connection.rb', line 133
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
145
146
147
|
# File 'lib/hyper-operation/transport/connection.rb', line 145
def disconnect(channel)
find_by(channel: channel, session: nil).destroy
end
|
.needs_refresh? ⇒ Boolean
87
88
89
|
# File 'lib/hyper-operation/transport/connection.rb', line 87
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
114
115
116
117
|
# File 'lib/hyper-operation/transport/connection.rb', line 114
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
126
127
128
129
130
131
|
# File 'lib/hyper-operation/transport/connection.rb', line 126
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
159
160
161
162
163
164
165
166
167
|
# File 'lib/hyper-operation/transport/connection.rb', line 159
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
153
154
155
156
157
|
# File 'lib/hyper-operation/transport/connection.rb', line 153
def root_path
QueuedMessage.root_path
rescue
nil
end
|
.root_path=(path) ⇒ Object
149
150
151
|
# File 'lib/hyper-operation/transport/connection.rb', line 149
def root_path=(path)
QueuedMessage.root_path = path if path
end
|
.send_to_channel(channel, data) ⇒ Object
119
120
121
122
123
124
|
# File 'lib/hyper-operation/transport/connection.rb', line 119
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
91
92
93
|
# File 'lib/hyper-operation/transport/connection.rb', line 91
def transport
self.class.transport
end
|