merged gemfile
This commit is contained in:
commit
1b1ac040a6
1
Gemfile
1
Gemfile
|
|
@ -20,6 +20,7 @@ gem 'actionmailer'
|
|||
gem 'sendgrid'
|
||||
gem 'aws-sdk', '1.8.0'
|
||||
gem 'carrierwave'
|
||||
gem 'aasm', '3.0.16'
|
||||
|
||||
if devenv
|
||||
gem 'jam_db', :path=> "#{workspace}/jam-db/target/ruby_package"
|
||||
|
|
|
|||
|
|
@ -26,22 +26,56 @@ module JamRuby
|
|||
#TODO
|
||||
end
|
||||
|
||||
# remove stale connections
|
||||
def remove_stale_connections(max_seconds)
|
||||
# flag connections as stale
|
||||
def flag_stale_connections(max_seconds)
|
||||
ConnectionManager.active_record_transaction do |connection_manager|
|
||||
conn = connection_manager.pg_conn
|
||||
|
||||
sql =<<SQL
|
||||
SELECT count(user_id) FROM connections
|
||||
WHERE
|
||||
updated_at < (NOW() - interval '#{max_seconds} second') AND
|
||||
aasm_state = '#{Connection::CONNECT_STATE.to_s}'
|
||||
SQL
|
||||
conn.exec(sql) do |result|
|
||||
count = result.getvalue(0, 0)
|
||||
if 0 < count.to_i
|
||||
@log.info("flag_stale_connections: flagging #{count.length} stale connections")
|
||||
sql =<<SQL
|
||||
UPDATE connections SET aasm_state = '#{Connection::STALE_STATE.to_s}'
|
||||
WHERE
|
||||
updated_at < (NOW() - interval '#{max_seconds} second') AND
|
||||
aasm_state = '#{Connection::CONNECT_STATE.to_s}'
|
||||
SQL
|
||||
conn.exec(sql)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# expiring connections in stale state, which deletes them
|
||||
def expire_stale_connections(max_seconds)
|
||||
ConnectionManager.active_record_transaction do |connection_manager|
|
||||
conn = connection_manager.pg_conn
|
||||
|
||||
stale_clients = []
|
||||
conn.exec("SELECT client_id FROM connections WHERE updated_at < (NOW() - interval '#{max_seconds} second')") do |result|
|
||||
sql =<<SQL
|
||||
SELECT client_id FROM connections
|
||||
WHERE
|
||||
updated_at < (NOW() - interval '#{max_seconds} second') AND
|
||||
aasm_state = '#{Connection::STALE_STATE.to_s}'
|
||||
SQL
|
||||
conn.exec(sql) do |result|
|
||||
result.each do |row|
|
||||
stale_clients.push(row['client_id'])
|
||||
end
|
||||
end
|
||||
|
||||
@log.debug("deleting #{stale_clients.length} stale connections")
|
||||
|
||||
stale_clients.each do |client_id|
|
||||
delete_connection(client_id)
|
||||
if 0 < stale_clients.size
|
||||
@log.info("expire_stale_connections: expiring:#{stale_clients.length} stale connections")
|
||||
stale_clients.each do |client_id|
|
||||
delete_connection(client_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -53,7 +87,8 @@ module JamRuby
|
|||
|
||||
lock_connections(conn)
|
||||
|
||||
conn.exec("INSERT INTO connections (user_id, client_id, ip_address) VALUES ($1, $2, $3)", [user_id, client_id, ip_address]).clear
|
||||
conn.exec("INSERT INTO connections (user_id, client_id, ip_address, aasm_state) VALUES ($1, $2, $3, $4)",
|
||||
[user_id, client_id, ip_address, Connection::CONNECT_STATE.to_s]).clear
|
||||
|
||||
# we just created a new connection-if this is the first time the user has shown up, we need to send out a message to his friends
|
||||
conn.exec("SELECT count(user_id) FROM connections WHERE user_id = $1", [user_id]) do |result|
|
||||
|
|
@ -303,4 +338,4 @@ module JamRuby
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -37,8 +37,8 @@
|
|||
end
|
||||
|
||||
# create a login ack (login was successful)
|
||||
def login_ack(public_ip, client_id, token, heartbeat_interval)
|
||||
login_ack = Jampb::LoginAck.new(:public_ip => public_ip, :client_id => client_id, :token => token, :heartbeat_interval => heartbeat_interval)
|
||||
def login_ack(public_ip, client_id, token, heartbeat_interval, music_session_id)
|
||||
login_ack = Jampb::LoginAck.new(:public_ip => public_ip, :client_id => client_id, :token => token, :heartbeat_interval => heartbeat_interval, :music_session_id => music_session_id)
|
||||
return Jampb::ClientMessage.new(:type => ClientMessage::Type::LOGIN_ACK, :route_to => CLIENT_TARGET, :login_ack => login_ack)
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
require 'aasm'
|
||||
|
||||
module JamRuby
|
||||
class Connection < ActiveRecord::Base
|
||||
|
||||
|
|
@ -20,6 +22,48 @@ module JamRuby
|
|||
validate :can_join_music_session, :if => :joining_session?
|
||||
after_save :require_at_least_one_track_when_in_session, :if => :joining_session?
|
||||
|
||||
include AASM
|
||||
IDLE_STATE = :idle
|
||||
CONNECT_STATE = :connected
|
||||
STALE_STATE = :stale
|
||||
EXPIRED_STATE = :expired
|
||||
|
||||
aasm do
|
||||
state IDLE_STATE, :initial => true
|
||||
state CONNECT_STATE
|
||||
state STALE_STATE
|
||||
state EXPIRED_STATE
|
||||
|
||||
event :connect do
|
||||
transitions :from => IDLE_STATE, :to => CONNECT_STATE
|
||||
transitions :from => STALE_STATE, :to => CONNECT_STATE
|
||||
end
|
||||
event :stale do
|
||||
transitions :from => CONNECT_STATE, :to => STALE_STATE
|
||||
transitions :from => IDLE_STATE, :to => STALE_STATE
|
||||
end
|
||||
event :expire, :after => :did_expire do
|
||||
transitions :from => CONNECT_STATE, :to => EXPIRED_STATE
|
||||
transitions :from => STALE_STATE, :to => EXPIRED_STATE
|
||||
transitions :from => IDLE_STATE, :to => EXPIRED_STATE
|
||||
end
|
||||
end
|
||||
|
||||
def state_message
|
||||
case self.aasm_state.to_sym
|
||||
when CONNECT_STATE
|
||||
'Connected'
|
||||
when STALE_STATE
|
||||
'Stale'
|
||||
else
|
||||
'Idle'
|
||||
end
|
||||
end
|
||||
|
||||
def did_expire
|
||||
self.destroy
|
||||
end
|
||||
|
||||
def joining_session?
|
||||
return joining_session
|
||||
end
|
||||
|
|
@ -76,4 +120,4 @@ module JamRuby
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -70,6 +70,9 @@ describe ConnectionManager do
|
|||
result.getvalue(0, 0).should == "1"
|
||||
end
|
||||
|
||||
cc = Connection.find_by_client_id!(client_id)
|
||||
cc.connected?.should be_true
|
||||
|
||||
@connman.delete_connection(client_id)
|
||||
|
||||
@conn.exec("SELECT count(*) FROM connections where user_id = $1", [user_id]) do |result|
|
||||
|
|
@ -171,23 +174,53 @@ describe ConnectionManager do
|
|||
@connman.gather_friends(@conn, user_id3).should == [user_id1]
|
||||
end
|
||||
|
||||
it "remove stale connection" do
|
||||
|
||||
it "flag stale connection" do
|
||||
client_id = "client_id8"
|
||||
|
||||
user_id = create_user("test", "user8", "user8@jamkazam.com")
|
||||
|
||||
@connman.create_connection(user_id, client_id, "1.1.1.1")
|
||||
|
||||
@connman.remove_stale_connections(60)
|
||||
num = JamRuby::Connection.count(:conditions => ['aasm_state = ?','connected'])
|
||||
num.should == 1
|
||||
assert_num_connections(client_id, num)
|
||||
@connman.flag_stale_connections(60)
|
||||
assert_num_connections(client_id, num)
|
||||
|
||||
sleep(1)
|
||||
|
||||
num = JamRuby::Connection.count(:conditions => ["updated_at < (NOW() - interval '#{1} second') AND aasm_state = 'connected'"])
|
||||
num.should == 1
|
||||
# this should change the aasm_state to stale
|
||||
@connman.flag_stale_connections(1)
|
||||
|
||||
num = JamRuby::Connection.count(:conditions => ["updated_at < (NOW() - interval '#{1} second') AND aasm_state = 'connected'"])
|
||||
num.should == 0
|
||||
|
||||
num = JamRuby::Connection.count(:conditions => ["updated_at < (NOW() - interval '#{1} second') AND aasm_state = 'stale'"])
|
||||
num.should == 1
|
||||
assert_num_connections(client_id, 1)
|
||||
|
||||
@connman.expire_stale_connections(1)
|
||||
sleep(1)
|
||||
assert_num_connections(client_id, 0)
|
||||
end
|
||||
|
||||
it "expires stale connection" do
|
||||
client_id = "client_id8"
|
||||
user_id = create_user("test", "user8", "user8@jamkazam.com")
|
||||
@connman.create_connection(user_id, client_id, "1.1.1.1")
|
||||
|
||||
sleep(1)
|
||||
@connman.flag_stale_connections(1)
|
||||
assert_num_connections(client_id, 1)
|
||||
# assert_num_connections(client_id, JamRuby::Connection.count(:conditions => ['aasm_state = ?','stale']))
|
||||
|
||||
@connman.expire_stale_connections(60)
|
||||
assert_num_connections(client_id, 1)
|
||||
|
||||
sleep(1)
|
||||
|
||||
# this should remove the stale connection
|
||||
@connman.remove_stale_connections(1)
|
||||
|
||||
# this should delete the stale connection
|
||||
@connman.expire_stale_connections(1)
|
||||
assert_num_connections(client_id, 0)
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,36 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Connection do
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
let (:music_session) { FactoryGirl.create(:music_session, :creator => user) }
|
||||
|
||||
end
|
||||
it 'starts in the correct state' do
|
||||
connection = FactoryGirl.create(:connection,
|
||||
:user => user,
|
||||
:music_session => music_session,
|
||||
:ip_address => "1.1.1.1",
|
||||
:client_id => "1")
|
||||
|
||||
connection.idle?.should be_true
|
||||
end
|
||||
|
||||
it 'transitions properly' do
|
||||
connection = FactoryGirl.create(:connection,
|
||||
:user => user,
|
||||
:music_session => music_session,
|
||||
:ip_address => "1.1.1.1",
|
||||
:client_id => "1")
|
||||
|
||||
connection.connect!
|
||||
connection.connected?.should be_true
|
||||
connection.state_message.should == 'Connected'
|
||||
|
||||
connection.stale!
|
||||
connection.stale?.should be_true
|
||||
connection.state_message.should == 'Stale'
|
||||
|
||||
connection.expire!
|
||||
connection.destroyed?.should be_true
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue