Skip to content

Commit

Permalink
add sqlite db reset threshold (#868)
Browse files Browse the repository at this point in the history
* add sqlite db reset threshold

* refresh session

---------

Co-authored-by: noO0ob <[email protected]>
  • Loading branch information
noO0oOo0ob and noO0ob authored Jul 4, 2024
1 parent ec1633e commit 76791cc
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 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,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()
Expand Down

0 comments on commit 76791cc

Please sign in to comment.