Scheduling tasks in a deployed Flask web app

Question:
I’m working on a Flask web app that retrieves data from Google Sheets and generates some visualizations. I’m using Replit to host and deploy my app.

My app currently updates historical data by saving it to JSON files. I want to automate this process so that the historical data is updated at regular intervals (e.g., daily) without requiring manual intervention. However, I’m facing challenges with scheduling tasks in my deployed app.

I have tried using libraries like apscheduler and schedule to schedule the data update task, but it seems to work only in the development environment and not in the deployed version of my app.

Here’s a simplified version of my code structure:

# Flask app setup and routes

def update_history():
    # Update historical data and save to JSON files
    pass

if __name__ == '__main__':
    # Schedule the update_history function to run daily
    scheduler = BackgroundScheduler()
    scheduler.add_job(update_history, 'cron', hour=0)
    scheduler.start()

    # Run the Flask app
    app.run(host='0.0.0.0', port=8080)

I’ve also tried using Replit’s built-in @replit.every() decorator, but it doesn’t seem to work in the deployed app either.

My questions are:

  1. Is there a reliable way to schedule tasks in a deployed Flask app on Replit?
  2. Are there any Replit-specific solutions or best practices for scheduling tasks in a deployed web app?
  3. If scheduling isn’t feasible within Replit, are there any alternative approaches I can consider to automate the data update process without incurring additional costs?

I appreciate any insights, suggestions, or workarounds that the community can provide. Thank you in advance for your help!

First of all, deployments don’t have persistent filesystems, so every time your app is restarted, you will lose the data in your JSON files.
Second of all, what deployment type are you using? Autoscale deployments shut off when they aren’t used, so you may want to look into Reserved VM or Scheduled deployments.

2 Likes

Hi, thanks for the reply

I am using Reserved VM. Even so, I was having trouble using scheduled updates in the json files. As for the persistent filesystem issue, does that mean i would need to use a database instead?

Thanks again!

So Replit is actually developing and testing this out, but it may be a while before it is publicly released.
As for databases, you can use Replit DB (works with Python and Flask). Look under the Database tab under Tools.

3 Likes

Does ReplitDB exactly work in deployments (I forgot this information a bit)?

Yes, the latest version of the Python Replit DB works in deployments. I believe the NodeJS version always worked in deployments.

2 Likes