From 76791cc4bd4a3713310dad0d792738cce69aedd0 Mon Sep 17 00:00:00 2001 From: noO0oOo0ob <38344038+noO0oOo0ob@users.noreply.github.com> Date: Thu, 4 Jul 2024 16:38:53 +0800 Subject: [PATCH] add sqlite db reset threshold (#868) * add sqlite db reset threshold * refresh session --------- Co-authored-by: noO0ob --- lyrebird/db/database_server.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lyrebird/db/database_server.py b/lyrebird/db/database_server.py index 7a3db972..e27f9ac7 100644 --- a/lyrebird/db/database_server.py +++ b/lyrebird/db/database_server.py @@ -34,6 +34,8 @@ class LyrebirdDatabaseServer(ThreadServer): def __init__(self, path=None): self.database_uri = None + self.error_log = [] + self.error_log_threshold = application.config.get('event.db_connection_recover_threshold', 0) super().__init__() if not path or path.isspace(): @@ -162,12 +164,19 @@ def run(self): session.add(event) session.commit() context.emit('db_action', 'add event log') - except OperationalError: + except OperationalError as e: logger.error(f'Save event failed. {traceback.format_exc()}') - logger.warning(f'DB would be reset: {self.database_uri}') - self.reset() + self.error_log.append(e) + if len(self.error_log) > self.error_log_threshold: + logger.warning(f'DB would be reset: {self.database_uri}') + self.error_log = [] + self.reset() + session = self._scoped_session() + else: + session.rollback() except Exception: logger.error(f'Save event failed. {traceback.format_exc()}') + session.rollback() def stop(self): super().stop()