Fix(db): Ensure Cloud SQL Connector is fork-safe with Gunicorn
Created by: Edouard-Legoupil
The FastAPI application was crashing during login when deployed with Gunicorn and multiple workers. This was caused by the Google Cloud SQL Connector being initialized before Gunicorn forked its worker processes. The connector is not fork-safe, leading to unstable connections and crashes in the child processes.
This commit refactors the database initialization logic to be lazy. The Cloud SQL Connector is now initialized once per worker process upon its first database connection attempt. This is the recommended pattern for using non-fork-safe resources with pre-forking servers like Gunicorn.
Additionally, this commit improves the application's logging infrastructure:
-
supervisordis configured to forward application logs to the container's stdout/stderr. - The FastAPI application's logging is configured to stream to stdout instead of a file.
-
printstatements in the authentication API have been replaced with properloggingcalls.
These changes ensure application stability and improve diagnostics in a containerized environment.
- Does my code meet the quality standards for releasing packages?
- Does the reviewer have all the information to validate the features/issues without too much research?
- Does the customer who will validate the associated tickets have the information to do so without wasting time?
Issues to validate to close :
-
issue #
Processed issues to keep open or in progress:
-
issue #
Checklist:
-
Does the package check go local? -
Does the CI pass? -
Are the added / fixed features documented, tested? -
Are the added features / solved problems briefly presented in the PR message? -
Are the changes related to tickets / issues that I have listed in the commits and in the PR itself? -
Are the tickets in "review" mode in the Project Tracking Board? -
Does each ticket, if it is to be closed after acceptance of the PR, contain a comment that tells how to validate it?