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.
This commit is contained in:
Ana Custura 2024-12-02 14:21:39 +00:00
parent 45823b02e4
commit 0867b13f8f
3 changed files with 17 additions and 7 deletions

View file

@ -4,14 +4,16 @@ ENV APP="bc"
ENV APP_BASE="/srv" ENV APP_BASE="/srv"
ENV SHELL="/bin/bash" ENV SHELL="/bin/bash"
ENV FLASK_APP="${FLASK_APP:-app}" ENV FLASK_APP="${FLASK_APP:-app}"
ENV FLASK_RUN_HOST="${FLASK_RUN_HOST:-0.0.0.0}" ENV GUNICORN_RUN_HOST="${GUNICORN_RUN_HOST:-0.0.0.0}"
ENV FLASK_RUN_PORT="${FLASK_RUN_PORT:-5000}" ENV GUNICORN_RUN_PORT="${GUNICORN_RUN_PORT:-5000}"
ENV PYTHONPATH="${APP_BASE}/env/lib/python3.11/site-packages" 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" 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_UID="${CONTAINER_UID:-1000}"
ARG CONTAINER_GID="${CONTAINER_GID:-1000}" ARG CONTAINER_GID="${CONTAINER_GID:-1000}"
ENV GUNICORN_WORKERS="${GUNICORN_WORKERS:-4}"
RUN apt-get update && \ RUN apt-get update && \
apt-get install --no-install-recommends -y \ apt-get install --no-install-recommends -y \
curl \ curl \
@ -47,10 +49,11 @@ USER ${APP}
WORKDIR ${APP_BASE}/${APP} WORKDIR ${APP_BASE}/${APP}
COPY --chown=${APP}:${APP} . ${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 && \ 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 -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 && \ RUN rm -rf frontend && \
git clone https://gitlab.com/guardianproject-ops/bypass-censorship/portal-frontend.git 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 && \ mkdir -p ${APP_BASE}/${APP}/app/static/ui && \
cp -r dist/* ${APP_BASE}/${APP}/app/static/ui && \ cp -r dist/* ${APP_BASE}/${APP}/app/static/ui && \
rm -rf frontend /tmp/* /var/tmp/* rm -rf frontend /tmp/* /var/tmp/*
ENTRYPOINT ["./run_gunicorn.sh"]

View file

@ -5,7 +5,7 @@ from typing import Iterator
import yaml import yaml
from flask import Flask, redirect, url_for, send_from_directory from flask import Flask, redirect, url_for, send_from_directory
from flask.typing import ResponseReturnValue 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.metrics_core import GaugeMetricFamily, CounterMetricFamily
from prometheus_client.registry import Collector from prometheus_client.registry import Collector
from sqlalchemy import text from sqlalchemy import text
@ -106,9 +106,10 @@ class AutomationCollector(Collector):
if not_migrating() and 'DISABLE_METRICS' not in os.environ: if not_migrating() and 'DISABLE_METRICS' not in os.environ:
REGISTRY.register(DefinedProxiesCollector()) registry = CollectorRegistry()
REGISTRY.register(BlockedProxiesCollector()) registry.register(DefinedProxiesCollector())
REGISTRY.register(AutomationCollector()) registry.register(BlockedProxiesCollector())
registry.register(AutomationCollector())
@app.route('/ui') @app.route('/ui')

4
run_gunicorn.sh Normal file
View file

@ -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"