repeat job run backend of flask app

I have a flask app launched. however,the repeat function to update databse was not triggered. How can I let that function keep running backend and updating database? This is my code below.

My design was that I have a function to keep updating database , then webpage in flask app can show records in database. I am using APschedule to call function - “DataBaseUpdate”. However, it doesn’t run.

Someone told me create another non-web worker service in App platform. My questions are #1. update database, the worker service should connect internet, is non-web service will work? #2. Is the code base for this service independent than the flask-app? If so, can I use one dockerfile to deploy both this service and flask-app in future for convenience?

if __name__ == "__main__":
    with app.app_context():
        db.create_all() # <--- create db object.
    start = datetime.timedelta(days=1)  #set start and end time
    end= start + datetime.timedelta(days=10)

    scheduler = BackgroundScheduler()
    scheduler.add_job(DataBaseUpdate, 'interval', seconds=30)

    port = int(os.environ.get('PORT', 5000)), host='', port=port)

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
December 28, 2023

Hi there,

In the context of the DigitalOcean App Platform, setting up a background task to regularly update the database in a Flask application requires a slightly different approach. Let’s address your concerns and outline the steps for implementing a reliable solution:

  1. Independent Worker Service:

    • Create a dedicated worker service for background tasks. This service should be entirely separate from your Flask web application service.
    • The worker service will run its own process, independent of the web server, and is responsible for tasks like database updates.
  2. Shared Codebase Considerations:

    • While the worker and web services can share the same codebase, they should be executed as separate processes. This means having separate entry points (like for Flask app and for the background tasks).
  3. Worker Service Configuration:

    • In your, configure APScheduler or any other background task manager to run the database update tasks.
    • Ensure that the worker service has access to all necessary environment variables and configurations it needs to perform its tasks, especially database connections.

Regarding your Dockerfile setup:

  1. Dockerfile:

    • Your Dockerfile should be capable of handling both the web and worker services, but with clear distinctions on how to start each service. You can control this with environment variables or command-line arguments.
  2. DigitalOcean App Platform Setup:

    • When setting up your application in the DigitalOcean App Platform, define two separate services: one for the Flask web application and another for the worker.
    • Configure the deployment such that the web service runs your Flask app (e.g., using Gunicorn), and the worker service runs the background task script.
  3. Example Dockerfile:

    # Use an official Python runtime as a parent image
    FROM python:3.8
    # Set the working directory in the container
    WORKDIR /app
    # Copy the current directory contents into the container at /app
    COPY . /app
    # Install any needed packages specified in requirements.txt
    RUN pip install --no-cache-dir -r requirements.txt
    # Define environment variable
    # Run or when the container launches
    CMD ["sh", "-c", "if [ $SERVICE_TYPE = 'worker' ]; then python; else gunicorn -w 4 -b$PORT app:app; fi"]
    • This Dockerfile uses an environment variable SERVICE_TYPE to decide whether to start the web service ( with Gunicorn) or the worker service (
  4. Environment Variables:

    • Set SERVICE_TYPE to worker for the worker service and leave it as web (or set it explicitly) for the web service in the DigitalOcean App Platform configuration.

After deployment, thoroughly test both the web and worker services to ensure they are functioning as expected. The worker service should run the database update tasks at the specified intervals, and the web service should remain responsive to user requests.

Let me know how it goes!



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel