I have the following stack in my project:
Cronjobs are running every 15 minutes to populate the MySQL database with data from the API. At the same moment, the flask app renders this date. So, everytime an user visits the website, the table containing a big quantity of data is loaded. The request is made to the database to load the data. The data will be presented in the bootstrap table as a response.
I'd want users to be able to refresh the table data (by clicking on the refresh
button) and always view the most recent data (rather than having to wait for the cronjob to restart and update the database).
If I trigger to import the data from the API again whenever the user clicks on the refresh
button, the operation will be extremely time consuming, and the entire data will be loaded for a number of minutes before the page can be shown again. What is the best way to refresh table data that has been loaded from the database?
This problem can be easily solved with a task queue that supports locking, e.g. huey (if you want something simple).
Let's call the task that fetches the API and updates the local database: fetch_api()
. It can be run by cron, or by a user.
fetch_api()
task can run concurrently.This way when an users clicks on the refresh button, a fetch_api()
task can be just enqueued, the task runner will handle the rest. The key point is the locking, so only one task will fetch the API.