From 0867b13f8f5f7683e745f14a5046b9a01096f10b Mon Sep 17 00:00:00 2001 From: Ana Custura Date: Mon, 2 Dec 2024 14:21:39 +0000 Subject: [PATCH] feat: run with gunicorn instead of flask Each worker now uses its own registry instead of the global REGISTRY to avoid duplicate metric registration, since metrics are served from the database. --- Dockerfile | 11 ++++++++--- app/__init__.py | 9 +++++---- run_gunicorn.sh | 4 ++++ 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 run_gunicorn.sh diff --git a/Dockerfile b/Dockerfile index c615f63..a335fe2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,14 +4,16 @@ ENV APP="bc" ENV APP_BASE="/srv" ENV SHELL="/bin/bash" ENV FLASK_APP="${FLASK_APP:-app}" -ENV FLASK_RUN_HOST="${FLASK_RUN_HOST:-0.0.0.0}" -ENV FLASK_RUN_PORT="${FLASK_RUN_PORT:-5000}" +ENV GUNICORN_RUN_HOST="${GUNICORN_RUN_HOST:-0.0.0.0}" +ENV GUNICORN_RUN_PORT="${GUNICORN_RUN_PORT:-5000}" ENV PYTHONPATH="${APP_BASE}/env/lib/python3.11/site-packages" ENV PATH="${APP_BASE}/env/bin:/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin:/home/${APP}/.local/bin" ARG CONTAINER_UID="${CONTAINER_UID:-1000}" ARG CONTAINER_GID="${CONTAINER_GID:-1000}" +ENV GUNICORN_WORKERS="${GUNICORN_WORKERS:-4}" + RUN apt-get update && \ apt-get install --no-install-recommends -y \ curl \ @@ -47,10 +49,11 @@ USER ${APP} WORKDIR ${APP_BASE}/${APP} COPY --chown=${APP}:${APP} . ${APP_BASE}/${APP} +RUN chmod +x ${APP_BASE}/${APP}/run_gunicorn.sh RUN python3 -m venv ${APP_BASE}/env && \ ${APP_BASE}/env/bin/pip install --no-cache-dir -r requirements.txt && \ - ${APP_BASE}/env/bin/pip install --no-cache-dir psycopg2-binary + ${APP_BASE}/env/bin/pip install --no-cache-dir psycopg2-binary gunicorn RUN rm -rf frontend && \ git clone https://gitlab.com/guardianproject-ops/bypass-censorship/portal-frontend.git frontend && \ @@ -58,3 +61,5 @@ RUN rm -rf frontend && \ mkdir -p ${APP_BASE}/${APP}/app/static/ui && \ cp -r dist/* ${APP_BASE}/${APP}/app/static/ui && \ rm -rf frontend /tmp/* /var/tmp/* + +ENTRYPOINT ["./run_gunicorn.sh"] \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py index 7ad99aa..74b6db2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,7 +5,7 @@ from typing import Iterator import yaml from flask import Flask, redirect, url_for, send_from_directory from flask.typing import ResponseReturnValue -from prometheus_client import make_wsgi_app, REGISTRY, Metric +from prometheus_client import make_wsgi_app, Metric, CollectorRegistry from prometheus_client.metrics_core import GaugeMetricFamily, CounterMetricFamily from prometheus_client.registry import Collector from sqlalchemy import text @@ -106,9 +106,10 @@ class AutomationCollector(Collector): if not_migrating() and 'DISABLE_METRICS' not in os.environ: - REGISTRY.register(DefinedProxiesCollector()) - REGISTRY.register(BlockedProxiesCollector()) - REGISTRY.register(AutomationCollector()) + registry = CollectorRegistry() + registry.register(DefinedProxiesCollector()) + registry.register(BlockedProxiesCollector()) + registry.register(AutomationCollector()) @app.route('/ui') diff --git a/run_gunicorn.sh b/run_gunicorn.sh new file mode 100644 index 0000000..d3d7252 --- /dev/null +++ b/run_gunicorn.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +flask db upgrade +gunicorn "$FLASK_APP".__init__:app -w "$GUNICORN_WORKERS" --access-logfile=- -b "$GUNICORN_RUN_HOST:$GUNICORN_RUN_PORT" \ No newline at end of file