made code layout more readable; tuned logging for connection state changes

This commit is contained in:
Jonathan Kolyer 2013-02-11 00:26:46 -06:00
parent 44d2aba3c6
commit ddd402ba6b
1 changed files with 15 additions and 10 deletions

View File

@ -26,7 +26,7 @@ module JamRuby
#TODO
end
# flag connections as stale (disconnected)
# flag connections as stale
def flag_stale_connections(max_seconds)
ConnectionManager.active_record_transaction do |connection_manager|
conn = connection_manager.pg_conn
@ -40,7 +40,7 @@ SQL
conn.exec(sql) do |result|
count = result.getvalue(0, 0)
if 0 < count.to_i
@log.debug("flagging #{count} stale connections (#{max_seconds})")
@log.info("flag_stale_connections: flagging #{count.length} stale connections")
sql =<<SQL
UPDATE connections SET aasm_state = '#{Connection::STALE_STATE.to_s}'
WHERE
@ -50,27 +50,32 @@ SQL
conn.exec(sql)
end
end
end
end
# remove stale connections
# 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') AND aasm_state = '#{Connection::STALE_STATE.to_s}'") 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 (#{max_seconds})")
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