From 6012a2b109b551b9cd8e9df31c4ba07e1ab24921 Mon Sep 17 00:00:00 2001 From: albertosottile Date: Thu, 26 Jul 2018 16:03:56 +0200 Subject: [PATCH] Server Stats: incapsulate collection code in try-catch --- syncplay/server.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/syncplay/server.py b/syncplay/server.py index ee2b2f0..32da0b0 100755 --- a/syncplay/server.py +++ b/syncplay/server.py @@ -195,15 +195,21 @@ class SyncFactory(Factory): class StatsRecorder(object): def __init__(self, dbpath, roomManager, delay): + self._dbPath = dbpath self._roomManagerHandle = roomManager - self._dbpool = self._initDatabase(dbpath) - reactor.callLater(delay, self._scheduleClientSnapshot) + try: + 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): - self._dbpool.close() + if self._dbPool is not None: + self._dbPool.close() - def _initDatabase(self, dbPath): - dbpool = adbapi.ConnectionPool("sqlite3", dbPath) + def _initDatabase(self): + dbpool = adbapi.ConnectionPool("sqlite3", self._dbPath, check_same_thread=False) query = 'create table if not exists clients_snapshots (snapshot_time integer, version string)' dbpool.runQuery(query) return dbpool @@ -213,12 +219,15 @@ class StatsRecorder(object): self._clientSnapshotTimer.start(constants.SERVER_STATS_SNAPSHOT_INTERVAL) def _runClientSnapshot(self): - snapshotTime = int(time.time()) - rooms = self._roomManagerHandle.exportRooms() - for room in rooms.values(): - for watcher in room.getWatchers(): - content = (snapshotTime, watcher.getVersion(), ) - self._dbpool.runQuery("INSERT INTO clients_snapshots VALUES (?, ?)", content) + try: + snapshotTime = int(time.time()) + rooms = self._roomManagerHandle.exportRooms() + for room in rooms.values(): + for watcher in room.getWatchers(): + content = (snapshotTime, watcher.getVersion(), ) + self._dbPool.runQuery("INSERT INTO clients_snapshots VALUES (?, ?)", content) + except: + pass class RoomManager(object):