Skip to content

Commit

Permalink
Enforce prune policy at poll step
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelrdev committed Feb 14, 2025
1 parent a565c33 commit 040b4c7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions rss/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from __future__ import annotations

from typing import Any, Iterable
from datetime import datetime
from datetime import datetime, timedelta
from string import Template
from time import mktime, time
from types import FrameType
Expand Down Expand Up @@ -116,11 +116,18 @@ async def poll_feeds(self) -> None:
except Exception:
self.log.exception("Fatal error while polling feeds")

def _valid_prune_entries(self) -> int | bool:
prune_entries = self.config["prune_entries"]
if prune_entries and type(prune_entries) is int and prune_entries > 0:
return prune_entries
else:
return False

async def prune_entries(self) -> None:
try:
while True:
prune_entries = self.config["prune_entries"]
if prune_entries and type(prune_entries) is int and prune_entries > 0:
if prune_entries := self._valid_prune_entries():
self.log.info(f"Pruning entries older than {prune_entries} days.")
await self.dbm.prune_entries(prune_entries)
await asyncio.sleep(86400) # prune_entries daily. 1 day = 86400s.
except asyncio.CancelledError:
Expand Down Expand Up @@ -197,6 +204,7 @@ async def _poll_once(self) -> None:
tasks = [self.try_parse_feed(feed=feed) for feed in subs if feed.next_retry < now]
feed: Feed
entries: Iterable[Entry]
prune_entries = self._valid_prune_entries()
self.log.info(f"Polling {len(tasks)} feeds")
for res in asyncio.as_completed(tasks):
feed, entries = await res
Expand All @@ -220,6 +228,12 @@ async def _poll_once(self) -> None:
except Exception:
self.log.exception(f"Weird error in items of {feed.url}")
continue
if prune_entries:
cutoff = datetime.now() - timedelta(days=prune_entries)
for old_del_entry in [
entry for entry in new_entries.values() if entry.date < cutoff
]:
new_entries.pop(old_del_entry.id, None)
for old_entry in await self.dbm.get_entries(feed.id):
new_entries.pop(old_entry.id, None)
self.log.trace(f"Feed {feed.id} had {len(new_entries)} new entries")
Expand Down
2 changes: 1 addition & 1 deletion rss/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,5 @@ async def update_feed_filter(self, feed_id: int, room_id: RoomID, feed_filter: s
await self.db.execute(q, feed_id, room_id, feed_filter)

async def prune_entries(self, num_days: int) -> None:
q = "DELETE FROM entry WHERE date < DATETIME('NOW', '-' || $1 || ' days')"
q = "DELETE FROM entry WHERE date < DATETIME('NOW', 'localtime', '-' || $1 || ' days')"
await self.db.execute(q, num_days)

0 comments on commit 040b4c7

Please sign in to comment.