From cf3ad1385503f2e5483dbe564a318c065398b169 Mon Sep 17 00:00:00 2001 From: noO0ob Date: Fri, 28 Jun 2024 17:44:27 +0800 Subject: [PATCH] add sqlite db reset threshold --- lyrebird/db/database_server.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lyrebird/db/database_server.py b/lyrebird/db/database_server.py index 7a3db972..77a64a85 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,10 +164,15 @@ 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() + else: + session.rollback() except Exception: logger.error(f'Save event failed. {traceback.format_exc()}')