From 7d81a65b2afa375ed9df1c27aea9fe80b2f4c6e6 Mon Sep 17 00:00:00 2001 From: Kumi Date: Mon, 17 Jun 2024 20:36:19 +0200 Subject: [PATCH] feat: add periodic background data update task Replaced the cron job for updating data with a background thread that runs every 5 minutes within the Flask app. Updated version to 0.3.7. This change ensures data is refreshed regularly without external dependencies like cron, improving deployment simplicity and reliability. --- pyproject.toml | 2 +- src/structables/main.py | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index af0ddf7..b6cc162 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "structables" -version = "0.3.6" +version = "0.3.7" authors = [ { name="Private.coffee Team", email="support@private.coffee" }, ] diff --git a/src/structables/main.py b/src/structables/main.py index 7126b6e..f1a1949 100644 --- a/src/structables/main.py +++ b/src/structables/main.py @@ -1,6 +1,8 @@ #!/usr/bin/env python from flask import Flask +import threading +import time from .config import Config from .routes import init_routes @@ -13,11 +15,28 @@ app.typesense_api_key = get_typesense_api_key() init_routes(app) + +def background_update_data(app): + """Runs the update_data function every 5 minutes. + + This replaces the need for a cron job to update the data. + + Args: + app (Flask): The Flask app instance. + """ + while True: + update_data(app) + time.sleep(300) + + def main(): - app.run(port=app.config['PORT'], host=app.config['LISTEN_HOST'], debug=app.config['DEBUG']) + threading.Thread(target=background_update_data, args=(app,), daemon=True).start() + app.run( + port=app.config["PORT"], + host=app.config["LISTEN_HOST"], + debug=app.config["DEBUG"], + ) + if __name__ == "__main__": main() - -# Initialize data when the server starts -update_data(app) \ No newline at end of file