Server Stats: incapsulate collection code in try-catch

This commit is contained in:
albertosottile 2018-07-26 16:03:56 +02:00
parent 6564f22d3a
commit 6012a2b109

View File

@ -195,15 +195,21 @@ class SyncFactory(Factory):
class StatsRecorder(object): class StatsRecorder(object):
def __init__(self, dbpath, roomManager, delay): def __init__(self, dbpath, roomManager, delay):
self._dbPath = dbpath
self._roomManagerHandle = roomManager self._roomManagerHandle = roomManager
self._dbpool = self._initDatabase(dbpath) try:
reactor.callLater(delay, self._scheduleClientSnapshot) self._dbPool = self._initDatabase()
reactor.callLater(delay, self._scheduleClientSnapshot)
except:
self._dbPool = None
print("--- Error in initializing the stats database. Server Stats not enabled. ---")
def __del__(self): def __del__(self):
self._dbpool.close() if self._dbPool is not None:
self._dbPool.close()
def _initDatabase(self, dbPath): def _initDatabase(self):
dbpool = adbapi.ConnectionPool("sqlite3", dbPath) dbpool = adbapi.ConnectionPool("sqlite3", self._dbPath, check_same_thread=False)
query = 'create table if not exists clients_snapshots (snapshot_time integer, version string)' query = 'create table if not exists clients_snapshots (snapshot_time integer, version string)'
dbpool.runQuery(query) dbpool.runQuery(query)
return dbpool return dbpool
@ -213,12 +219,15 @@ class StatsRecorder(object):
self._clientSnapshotTimer.start(constants.SERVER_STATS_SNAPSHOT_INTERVAL) self._clientSnapshotTimer.start(constants.SERVER_STATS_SNAPSHOT_INTERVAL)
def _runClientSnapshot(self): def _runClientSnapshot(self):
snapshotTime = int(time.time()) try:
rooms = self._roomManagerHandle.exportRooms() snapshotTime = int(time.time())
for room in rooms.values(): rooms = self._roomManagerHandle.exportRooms()
for watcher in room.getWatchers(): for room in rooms.values():
content = (snapshotTime, watcher.getVersion(), ) for watcher in room.getWatchers():
self._dbpool.runQuery("INSERT INTO clients_snapshots VALUES (?, ?)", content) content = (snapshotTime, watcher.getVersion(), )
self._dbPool.runQuery("INSERT INTO clients_snapshots VALUES (?, ?)", content)
except:
pass
class RoomManager(object): class RoomManager(object):