dockergoogle-cloud-platformfastapigunicorngoogle-cloud-run

How do I keep a long-running background task from stopping on Google Cloud Run?


I have a FastAPI endpoint which starts some long-running background tasks:

background_tasks.add_task(fn())

These tasks can take up to 10 hours to complete.

I am running this inside a Docker container using gunicorn:

# Start the application
CMD exec gunicorn --bind :$PORT --workers 1 -k uvicorn.workers.UvicornWorker --threads 8 --timeout 0 main:app

When I start the container and hit the endpoint from Postman on my machine, the tasks start and when left running, they complete fine.

I'm now using Google Cloud Run to host my container. When I call the endpoint (XXX.run.app) the tasks also seem to start based upon the logs, but they seem to stop and later on, the service will restart.

I've tried increasing the request timeout to the maximum but this didn't help. Is there something else I can try?


Solution

  • Your design is just not optimal for your expectation. You mix "real time API" with FastAPI, and long running batches in backgrounds.

    2 Behaviors, 2 type of services -> Separation of concern.