From d80fb60afdc0a4c7eeb076c5093af92b55435821 Mon Sep 17 00:00:00 2001 From: jedi Date: Sun, 9 Mar 2025 18:47:59 +0100 Subject: [PATCH] fix bug in initial db creation caused by prometheus lib rtying to access tables at load time --- core/.local/.forgit_fordocker | 0 core/core/metrics.py | 27 ++++++++++++++++----------- deploy/dev/Dockerfile.backend | 9 ++------- deploy/dev/Dockerfile.frontend | 2 +- deploy/dev/docker-compose.yml | 14 +++++++++----- deploy/testing/Dockerfile.backend | 8 ++++---- deploy/testing/Dockerfile.frontend | 2 +- deploy/testing/docker-compose.yml | 12 ++++++++---- 8 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 core/.local/.forgit_fordocker diff --git a/core/.local/.forgit_fordocker b/core/.local/.forgit_fordocker new file mode 100644 index 0000000..e69de29 diff --git a/core/core/metrics.py b/core/core/metrics.py index 149829c..d973b0d 100644 --- a/core/core/metrics.py +++ b/core/core/metrics.py @@ -3,18 +3,20 @@ from prometheus_client.core import CounterMetricFamily, REGISTRY from django.db.models import Case, Value, When, BooleanField, Count from inventory.models import Item + class ItemCountCollector(object): def collect(self): - counter = CounterMetricFamily("item_count", "Current number of items", labels=['event', 'returned_state']) + try: + counter = CounterMetricFamily("item_count", "Current number of items", labels=['event', 'returned_state']) - yield counter + yield counter - if not apps.models_ready or not apps.apps_ready: - return + if not apps.models_ready or not apps.apps_ready: + return - queryset = ( - Item.all_objects + queryset = ( + Item.all_objects .annotate( returned=Case( When(returned_at__isnull=True, then=Value(False)), @@ -25,11 +27,14 @@ class ItemCountCollector(object): .values('event__slug', 'returned', 'event_id') .annotate(amount=Count('id')) .order_by('event__slug', 'returned') # Optional: order by slug and returned - ) + ) - for e in queryset: - counter.add_metric([e["event__slug"].lower(), str(e["returned"])], e["amount"]) + for e in queryset: + counter.add_metric([e["event__slug"].lower(), str(e["returned"])], e["amount"]) - yield counter + yield counter + except: + pass -REGISTRY.register(ItemCountCollector()) \ No newline at end of file + +REGISTRY.register(ItemCountCollector()) diff --git a/deploy/dev/Dockerfile.backend b/deploy/dev/Dockerfile.backend index 19c2efd..57ab856 100644 --- a/deploy/dev/Dockerfile.backend +++ b/deploy/dev/Dockerfile.backend @@ -1,13 +1,8 @@ -FROM python:3.11-bookworm +FROM python:3.11-slim-bookworm LABEL authors="lagertonne" ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code COPY requirements.dev.txt /code/ -COPY requirements.prod.txt /code/ -RUN apt update && apt install -y mariadb-client -RUN pip install -r requirements.dev.txt -RUN pip install -r requirements.prod.txt -RUN pip install mysqlclient -COPY .. /code/ \ No newline at end of file +RUN pip install -r requirements.dev.txt \ No newline at end of file diff --git a/deploy/dev/Dockerfile.frontend b/deploy/dev/Dockerfile.frontend index 0a41d1a..a8fd652 100644 --- a/deploy/dev/Dockerfile.frontend +++ b/deploy/dev/Dockerfile.frontend @@ -1,4 +1,4 @@ -FROM docker.io/node:22 +FROM node:22-alpine RUN mkdir /web WORKDIR /web diff --git a/deploy/dev/docker-compose.yml b/deploy/dev/docker-compose.yml index 8580127..cf1bfdc 100644 --- a/deploy/dev/docker-compose.yml +++ b/deploy/dev/docker-compose.yml @@ -1,3 +1,4 @@ +name: c3lf-sys3-dev services: core: build: @@ -6,11 +7,12 @@ services: command: bash -c 'python manage.py migrate && python testdata.py && python manage.py runserver 0.0.0.0:8000' environment: - HTTP_HOST=core - - DB_FILE=dev.db + - DB_FILE=.local/dev.db - DEBUG_MODE_ACTIVE=true volumes: - - ../../core:/code - - ../testdata.py:/code/testdata.py + - ../../core:/code:ro + - ../testdata.py:/code/testdata.py:ro + - backend_context:/code/.local ports: - "8000:8000" @@ -20,10 +22,12 @@ services: dockerfile: ../deploy/dev/Dockerfile.frontend command: npm run serve volumes: - - ../../web:/web:ro - - /web/node_modules + - ../../web/src:/web/src - ./vue.config.js:/web/vue.config.js ports: - "8080:8080" depends_on: - core + +volumes: + backend_context: \ No newline at end of file diff --git a/deploy/testing/Dockerfile.backend b/deploy/testing/Dockerfile.backend index c968994..06e494f 100644 --- a/deploy/testing/Dockerfile.backend +++ b/deploy/testing/Dockerfile.backend @@ -1,11 +1,11 @@ -FROM python:3.11-bookworm +FROM python:3.11-slim-bookworm LABEL authors="lagertonne" ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code -COPY requirements.prod.txt /code/ -RUN apt update && apt install -y mariadb-client -RUN pip install -r requirements.prod.txt +RUN apt update && apt install -y pkg-config mariadb-client default-libmysqlclient-dev build-essential RUN pip install mysqlclient +COPY requirements.prod.txt /code/ +RUN pip install -r requirements.prod.txt COPY .. /code/ \ No newline at end of file diff --git a/deploy/testing/Dockerfile.frontend b/deploy/testing/Dockerfile.frontend index 0a41d1a..a8fd652 100644 --- a/deploy/testing/Dockerfile.frontend +++ b/deploy/testing/Dockerfile.frontend @@ -1,4 +1,4 @@ -FROM docker.io/node:22 +FROM node:22-alpine RUN mkdir /web WORKDIR /web diff --git a/deploy/testing/docker-compose.yml b/deploy/testing/docker-compose.yml index b41dd63..4a82289 100644 --- a/deploy/testing/docker-compose.yml +++ b/deploy/testing/docker-compose.yml @@ -1,3 +1,4 @@ +name: c3lf-sys3-testing services: redis: image: redis @@ -31,8 +32,9 @@ services: - DB_PASSWORD=system3 - MAIL_DOMAIN=mail:1025 volumes: - - ../../core:/code - - ../testdata.py:/code/testdata.py + - ../../core:/code:ro + - ../testdata.py:/code/testdata.py:ro + - backend_context:/code ports: - "8000:8000" depends_on: @@ -47,8 +49,8 @@ services: command: npm run serve volumes: - ../../web:/web:ro - - /web/node_modules - - ./vue.config.js:/web/vue.config.js + - ./vue.config.js:/web/vue.config.js:ro + - frontend_context:/web ports: - "8080:8080" depends_on: @@ -70,3 +72,5 @@ services: volumes: mariadb_data: mailpit_data: + frontend_context: + backend_context: