diff --git a/Dockerfile b/Dockerfile index e34a177..f632bbf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,4 +28,4 @@ ENV GIT_CONFIG_VALUE_0=/repo EXPOSE 8000 ENTRYPOINT ["/app/entrypoint.sh"] -CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "2", "--timeout", "120", "--preload", "wsgi:app"] +CMD ["gunicorn", "-c", "gunicorn.conf.py", "wsgi:app"] diff --git a/app/__init__.py b/app/__init__.py index 98f34b4..516027b 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -30,6 +30,7 @@ def create_app(setup_database=True): "postgresql://photohost:photohost_secret@localhost:5432/photohost", ) app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False + app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"pool_pre_ping": True} app.config["UPLOAD_FOLDER"] = os.getenv("UPLOAD_FOLDER", "uploads") app.config["MAX_CONTENT_LENGTH"] = int(os.getenv("MAX_UPLOAD_MB", "10")) * 1024 * 1024 app.config["ALLOWED_EXTENSIONS"] = {"png", "jpg", "jpeg", "gif", "webp", "bmp"} @@ -68,25 +69,26 @@ def create_app(setup_database=True): except Exception: return {"site_banners": {}} - with app.app_context(): - from app.models import ( # noqa: F401 - AdBanner, - Folder, - FolderInvite, - FolderMember, - PasswordResetToken, - Photo, - SiteSettings, - User, - UserGroup, - ) + if setup_database: + with app.app_context(): + from app.models import ( # noqa: F401 + AdBanner, + Folder, + FolderInvite, + FolderMember, + PasswordResetToken, + Photo, + SiteSettings, + User, + UserGroup, + ) - db.create_all() + db.create_all() - if setup_database and os.getenv("SKIP_DB_INIT") != "1": - from app.bootstrap import run_database_setup + if os.getenv("SKIP_DB_INIT") != "1": + from app.bootstrap import run_database_setup - run_database_setup(app) + run_database_setup(app) return app diff --git a/entrypoint.sh b/entrypoint.sh index a779aac..075c2bc 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -12,7 +12,7 @@ if [ "$ALLOW_GIT_DEPLOY" = "true" ] || [ "$ALLOW_GIT_DEPLOY" = "1" ] || [ "$ALLO fi # Run DB migrations once before gunicorn workers start. -gosu appuser python /app/init_db.py +gosu appuser python /app/init_db.py || exit 1 export SKIP_DB_INIT=1 exec gosu appuser "$@" diff --git a/gunicorn.conf.py b/gunicorn.conf.py new file mode 100644 index 0000000..331eada --- /dev/null +++ b/gunicorn.conf.py @@ -0,0 +1,12 @@ +bind = "0.0.0.0:8000" +workers = 2 +timeout = 120 +accesslog = "-" +errorlog = "-" +capture_output = True + + +def post_fork(server, worker): + from app import db + + db.engine.dispose()