Skip to content

Commit

Permalink
add sqlite db reset threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
noO0ob committed Jun 28, 2024
1 parent 73d35cb commit cf3ad13
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lyrebird/db/database_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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()}')

Expand Down

0 comments on commit cf3ad13

Please sign in to comment.