Skip to content

Commit 1d866f6

Browse files
committed
Implement optional database VACUUM on shutdown
Disabled by default. Closes #80.
1 parent 787a8e1 commit 1d866f6

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

docs/config.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
378388
Example usage:
379389

380390
.. code-block:: python

logstash_async/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

5255
constants = Constants() # pylint: disable=invalid-name

logstash_async/database.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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;")

logstash_async/worker.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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')

0 commit comments

Comments
 (0)