File tree Expand file tree Collapse file tree 4 files changed +30
-0
lines changed
Expand file tree Collapse file tree 4 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -375,6 +375,16 @@ for easy modification.
375375 *Default *: None
376376
377377
378+ ``constants.DATABASE_VACUUM_ON_SHUTDOWN ``
379+
380+ Vacuum SQLite database on shutdown - when enabled, the database will be vacuumed on shutdown
381+ to reduce its size on disk.
382+
383+ *Type *: ``boolean ``
384+
385+ *Default *: False
386+
387+
378388Example usage:
379389
380390.. code-block :: python
Original file line number Diff line number Diff line change @@ -47,6 +47,9 @@ class Constants:
4747 # Use a string like '5 per minute' or None to disable (default), for details see
4848 # http://limits.readthedocs.io/en/stable/string-notation.html
4949 ERROR_LOG_RATE_LIMIT = None
50+ # Vacuum SQLite database on shutdown - when enabled, the database will be vacuumed on shutdown
51+ # to reduce its size on disk
52+ DATABASE_VACUUM_ON_SHUTDOWN = False
5053
5154
5255constants = Constants () # pylint: disable=invalid-name
Original file line number Diff line number Diff line change @@ -152,3 +152,9 @@ def expire_events(self):
152152 with self ._connect () as connection :
153153 cursor = connection .cursor ()
154154 cursor .execute (query_delete )
155+
156+ # ----------------------------------------------------------------------
157+ def vacuum (self ):
158+ with self ._connect () as connection :
159+ cursor = connection .cursor ()
160+ cursor .execute ("VACUUM;" )
Original file line number Diff line number Diff line change @@ -97,6 +97,7 @@ def run(self):
9797 self ._log_general_error (exc )
9898 # check for empty queue and report if not
9999 self ._warn_about_non_empty_queue_on_shutdown ()
100+ self ._vaccum_database ()
100101
101102 # ----------------------------------------------------------------------
102103 def force_flush_queued_events (self ):
@@ -357,3 +358,13 @@ def _warn_about_non_empty_queue_on_shutdown(self):
357358 f'Non-empty queue while shutting down ({ queue_size } events pending). '
358359 'This indicates a previous error.' ,
359360 extra = dict (queue_size = queue_size ))
361+
362+ # ----------------------------------------------------------------------
363+ def _vaccum_database (self ):
364+ if not constants .DATABASE_VACUUM_ON_SHUTDOWN :
365+ return
366+
367+ try :
368+ self ._database .vacuum ()
369+ except DatabaseLockedError :
370+ self ._safe_log ('debug' , 'Database is locked, ignore vacuuming database' )
You can’t perform that action at this time.
0 commit comments