feat: add periodic background data update task
All checks were successful
Docker CI/CD / Docker Build and Push to Docker Hub (push) Successful in 2m34s
Python Package CI/CD / Publish to PyPI (push) Successful in 44s

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.
This commit is contained in:
Kumi 2024-06-17 20:36:19 +02:00
parent 556669d341
commit 7d81a65b2a
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 24 additions and 5 deletions

View file

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "structables" name = "structables"
version = "0.3.6" version = "0.3.7"
authors = [ authors = [
{ name="Private.coffee Team", email="support@private.coffee" }, { name="Private.coffee Team", email="support@private.coffee" },
] ]

View file

@ -1,6 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
from flask import Flask from flask import Flask
import threading
import time
from .config import Config from .config import Config
from .routes import init_routes from .routes import init_routes
@ -13,11 +15,28 @@ app.typesense_api_key = get_typesense_api_key()
init_routes(app) 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(): 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__": if __name__ == "__main__":
main() main()
# Initialize data when the server starts
update_data(app)