From cb5438266324e3cb9a40dc28cf1c75b9baae7798 Mon Sep 17 00:00:00 2001 From: RonanMorgan <49660557+RonanMorgan@users.noreply.github.com> Date: Wed, 29 May 2024 12:30:52 +0200 Subject: [PATCH] Limits the ressources available for the container (#203) * feat: add a script which allow to set the limits of the container according to the ressources' host * 50 -> 70 * fix: use docker-compose.override.yml * clean up --- .gitignore | 3 +++ Makefile | 2 ++ docker-compose.yml | 5 +++++ promtail/config.yml | 19 ------------------- pyroengine/core.py | 3 ++- scripts/setup-docker-compose.sh | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 45 insertions(+), 20 deletions(-) delete mode 100644 promtail/config.yml create mode 100644 scripts/setup-docker-compose.sh diff --git a/.gitignore b/.gitignore index 9de8bfb1..9fd6cd13 100644 --- a/.gitignore +++ b/.gitignore @@ -118,3 +118,6 @@ pyroengine/version.py *.onnx # Release conda-dist/ + +docker-compose.yml.bak +docker-compose.override.yml \ No newline at end of file diff --git a/Makefile b/Makefile index 52953237..ea18c156 100644 --- a/Makefile +++ b/Makefile @@ -26,8 +26,10 @@ build: # Run the engine wrapper run: + bash scripts/setup-docker-compose.sh docker build . -t pyronear/pyro-engine:latest docker compose up -d + rm docker-compose.yml.bak # Get log from engine wrapper log: diff --git a/docker-compose.yml b/docker-compose.yml index b2147df3..e32281b5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,11 @@ services: volumes: - ./data:/usr/src/app/data restart: always + deploy: + resources: + limits: + cpus: "3" + memory: "" logging: driver: "json-file" options: diff --git a/promtail/config.yml b/promtail/config.yml deleted file mode 100644 index 98dcd348..00000000 --- a/promtail/config.yml +++ /dev/null @@ -1,19 +0,0 @@ -server: - http_listen_port: 8300 - -positions: - filename: /tmp/positions.yaml - -clients: - - url: ${LOKI_URL} - -scrape_configs: -- job_name: system - static_configs: - - targets: - - localhost - labels: - job: varlogs - scope: ${PROMTAIL_DEVICE_SCOPE} - tower: ${PROMTAIL_DEVICE_NAME} - __path__: /var/lib/docker/containers/*/*log diff --git a/pyroengine/core.py b/pyroengine/core.py index 776af058..1c960e3e 100644 --- a/pyroengine/core.py +++ b/pyroengine/core.py @@ -42,8 +42,9 @@ def analyze_stream(self, idx: int) -> None: img = self.cameras[idx].capture() try: self.engine.predict(img, self.cameras[idx].ip_address) - except Exception: + except Exception as e: logging.warning(f"Unable to analyze stream from camera {self.cameras[idx]}") + logging.warning(e) except Exception: logging.warning(f"Unable to fetch stream from camera {self.cameras[idx]}") diff --git a/scripts/setup-docker-compose.sh b/scripts/setup-docker-compose.sh new file mode 100644 index 00000000..885a0c71 --- /dev/null +++ b/scripts/setup-docker-compose.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Define the percentage of host memory you want to allocate +PERCENTAGE=70 + +# Get the total memory of the host system in kilobytes +TOTAL_MEM_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}') + +# Calculate the memory limit in kilobytes +LIMIT_MEM_KB=$((TOTAL_MEM_KB * PERCENTAGE / 100)) + +# Convert the limit to a format Docker understands (e.g., "m" for megabytes) +LIMIT_MEM_MB=$((LIMIT_MEM_KB / 1024))m + +# Define the Docker Compose file to modify +DOCKER_COMPOSE_FILE="docker-compose.yml" + +# Backup the original Docker Compose file +cp $DOCKER_COMPOSE_FILE "${DOCKER_COMPOSE_FILE}.bak" + +# Use awk to update the memory limits in the Docker Compose file, preserving indentation +awk -v mem_limit="$LIMIT_MEM_MB" ' +/services:/ { in_services=1 } +in_services && /deploy:/ { in_deploy=1 } +in_deploy && /resources:/ { in_resources=1 } +in_resources && /limits:/ { in_limits=1 } +in_limits && /memory:/ { + $0 = gensub(/memory:.*/, "memory: " mem_limit, 1) +} +{ print } +' "${DOCKER_COMPOSE_FILE}.bak" > "docker-compose.override.yml" + +echo "Memory limits set to $LIMIT_MEM_MB in $DOCKER_COMPOSE_FILE"