Close database connection when command is finished instead of waiting for GC

This commit is contained in:
2021-12-13 20:01:45 +01:00
parent 71e8769e75
commit 75a4a27dcd

View File

@ -1,4 +1,4 @@
from typing import Optional from contextlib import closing
import typer import typer
from dhooks import Webhook from dhooks import Webhook
@ -6,9 +6,8 @@ from reader import FeedExistsError, make_reader
from discord_rss_bot.settings import Settings from discord_rss_bot.settings import Settings
app = typer.Typer() # For CLI (https://typer.tiangolo.com/) app = typer.Typer()
hook = Webhook(Settings.webhook_url) # For Webhooks (https://github.com/kyb3r/dhooks) hook = Webhook(Settings.webhook_url)
reader = make_reader(Settings.db_file) # For RSS (https://github.com/lemon24/reader)
@app.command() @app.command()
@ -20,7 +19,9 @@ def add(
Args: Args:
feed_url (str): The url of the feed to add feed_url (str): The url of the feed to add
notify_discord (bool): Whether to send a message to Discord when the feed is added
""" """
with closing(make_reader(Settings.db_file)) as reader:
try: try:
# Add the feed to the database # Add the feed to the database
reader.add_feed(feed_url) reader.add_feed(feed_url)
@ -51,6 +52,7 @@ def add(
@app.command() @app.command()
def check() -> None: def check() -> None:
"""Check new entries for every feed""" """Check new entries for every feed"""
with closing(make_reader(Settings.db_file)) as reader:
feed_count = reader.get_feed_counts() feed_count = reader.get_feed_counts()
entry_count = reader.get_entry_counts() entry_count = reader.get_entry_counts()