Skip to content

Commit

Permalink
Read pending events from database on startup for immediate flushing
Browse files Browse the repository at this point in the history
Closes #85.
  • Loading branch information
eht16 committed Aug 27, 2023
1 parent aea33fb commit b9a12a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions logstash_async/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,12 @@ def vacuum(self):
with self._connect() as connection:
cursor = connection.cursor()
cursor.execute("VACUUM;")

# ----------------------------------------------------------------------
def get_non_flushed_event_count(self):
query_fetch = '''SELECT count(*) FROM `event` WHERE `pending_delete` = 0;'''
with self._connect() as connection:
cursor = connection.cursor()
cursor.execute(query_fetch)
count = cursor.fetchone()[0]
return count
2 changes: 2 additions & 0 deletions logstash_async/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ def _setup_database(self):
else:
self._database = MemoryCache(cache=self._memory_cache, event_ttl=self._event_ttl)

self._non_flushed_event_count = self._database.get_non_flushed_event_count()

# ----------------------------------------------------------------------
def _fetch_events(self):
while True:
Expand Down

2 comments on commit b9a12a7

@Cthulhuisreal
Copy link

@Cthulhuisreal Cthulhuisreal commented on b9a12a7 Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eht16 What if we have self._database = MemoryCache() type ? Then we get AttributeError if we try self._database.get_non_flushed_event_count()

@eht16
Copy link
Owner Author

@eht16 eht16 commented on b9a12a7 Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I've overseen this.
Fixed in f40e846 and released as 2.7.2.

Please sign in to comment.