Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limits the ressources available for the container #203

Merged
merged 5 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,6 @@ pyroengine/version.py
*.onnx
# Release
conda-dist/

docker-compose.yml.bak
docker-compose.override.yml
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ services:
volumes:
- ./data:/usr/src/app/data
restart: always
deploy:
resources:
limits:
cpus: "3"
memory: ""
logging:
driver: "json-file"
options:
Expand Down
19 changes: 0 additions & 19 deletions promtail/config.yml

This file was deleted.

3 changes: 2 additions & 1 deletion pyroengine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
img = self.cameras[idx].capture()
try:
self.engine.predict(img, self.cameras[idx].ip_address)
except Exception:
except Exception as e:

Check warning on line 45 in pyroengine/core.py

View check run for this annotation

Codecov / codecov/patch

pyroengine/core.py#L45

Added line #L45 was not covered by tests
logging.warning(f"Unable to analyze stream from camera {self.cameras[idx]}")
logging.warning(e)

Check warning on line 47 in pyroengine/core.py

View check run for this annotation

Codecov / codecov/patch

pyroengine/core.py#L47

Added line #L47 was not covered by tests
except Exception:
logging.warning(f"Unable to fetch stream from camera {self.cameras[idx]}")

Expand Down
33 changes: 33 additions & 0 deletions scripts/setup-docker-compose.sh
Original file line number Diff line number Diff line change
@@ -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"
Loading