Merge branch 'master' of bitbucket.org:jamkazam/jam-ruby

This commit is contained in:
Seth Call 2013-03-01 07:35:58 -06:00
commit 6a9b43c7de
4 changed files with 41 additions and 19 deletions

View File

@ -26,6 +26,27 @@ module JamRuby
#TODO
end
def reconnect(conn)
sql =<<SQL
UPDATE connections SET (aasm_state,updated_at) = ('#{Connection::CONNECT_STATE.to_s}', NOW())
WHERE
client_id = '#{conn.client_id}'
SQL
# @log.info("*** reconnect: client_id = #{conn.client_id}")
self.pg_conn.exec(sql)
end
def flag_connection_stale_with_client_id(client_id)
sql =<<SQL
UPDATE connections SET aasm_state = '#{Connection::STALE_STATE.to_s}'
WHERE
client_id = '#{client_id}' AND
aasm_state = '#{Connection::CONNECT_STATE.to_s}'
SQL
# @log.info("*** flag_connection_stale_with_client_id: client_id = #{client_id}; sql = #{sql}")
self.pg_conn.exec(sql)
end
# flag connections as stale
def flag_stale_connections(max_seconds)
ConnectionManager.active_record_transaction do |connection_manager|
@ -39,8 +60,9 @@ WHERE
SQL
conn.exec(sql) do |result|
count = result.getvalue(0, 0)
# @log.info("flag_stale_connections: flagging #{count} stale connections")
if 0 < count.to_i
@log.info("flag_stale_connections: flagging #{count.length} stale connections")
# @log.info("flag_stale_connections: flagging #{count} stale connections")
sql =<<SQL
UPDATE connections SET aasm_state = '#{Connection::STALE_STATE.to_s}'
WHERE
@ -53,12 +75,16 @@ SQL
end
end
# expiring connections in stale state, which deletes them
# NOTE this is only used for testing purposes; actual deletes will be processed in the websocket context which cleans up dependencies
def expire_stale_connections(max_seconds)
self.stale_connection_client_ids(max_seconds).each { |cid| self.delete_connection(cid) }
end
# expiring connections in stale state, which deletes them
def stale_connection_client_ids(max_seconds)
client_ids = []
ConnectionManager.active_record_transaction do |connection_manager|
conn = connection_manager.pg_conn
stale_clients = []
sql =<<SQL
SELECT client_id FROM connections
WHERE
@ -66,18 +92,11 @@ WHERE
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
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
result.each { |row| client_ids << row['client_id'] }
# @log.debug("*** stale_connection_client_ids: client_ids = #{client_ids.inspect}")
end
end
client_ids
end

View File

@ -6,7 +6,7 @@ module Limits
# recording genres
MIN_GENRES_PER_RECORDING = 1
MAX_GENRES_PER_RECORDING = 3
MAX_GENRES_PER_RECORDING = 1
# instruments
MIN_INSTRUMENTS_PER_MUSICIAN = 1

View File

@ -199,7 +199,11 @@ describe ConnectionManager do
num.should == 1
assert_num_connections(client_id, 1)
@connman.expire_stale_connections(1)
cids = @connman.stale_connection_client_ids(1)
cids.size.should == 1
cids[0].should == client_id
cids.each { |cid| @connman.delete_connection(cid) }
sleep(1)
assert_num_connections(client_id, 0)
end
@ -218,7 +222,6 @@ describe ConnectionManager do
assert_num_connections(client_id, 1)
sleep(1)
# this should delete the stale connection
@connman.expire_stale_connections(1)
assert_num_connections(client_id, 0)

View File

@ -86,10 +86,10 @@ describe Recording do
@genre2 = FactoryGirl.create(:genre)
@genre3 = FactoryGirl.create(:genre)
@recording.update_fields :name => "name1", :description => "description", :genres => [@genre1, @genre2], :is_downloadable => true, :is_public => true
@recording.update_fields :name => "name1", :description => "description", :genres => [@genre1], :is_downloadable => true, :is_public => true
@recording.name.should == "name1"
@recording.description.should == "description"
@recording.genres.length.should == 2
@recording.genres.length.should == 1
@recording.is_public.should == true
@recording.is_downloadable.should == true