Question

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.date.today()+ datetime.timedelta(days=1)  #set start and end time
    end= start + datetime.timedelta(days=10)

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

    port = int(os.environ.get('PORT', 5000))
    app.run(debug=True, host='0.0.0.0', 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 app.py for Flask app and worker.py for the background tasks).
  3. Worker Service Configuration:

    • In your worker.py, 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
    ENV SERVICE_TYPE web
    
    # Run app.py or worker.py when the container launches
    CMD ["sh", "-c", "if [ $SERVICE_TYPE = 'worker' ]; then python worker.py; else gunicorn -w 4 -b 0.0.0.0:$PORT app:app; fi"]
    
    • This Dockerfile uses an environment variable SERVICE_TYPE to decide whether to start the web service (app.py with Gunicorn) or the worker service (worker.py).
  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!

Best,

Bobby

Try DigitalOcean for free

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

Sign up

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.