Skip to content

Commit

Permalink
Slow worker restart
Browse files Browse the repository at this point in the history
  • Loading branch information
aequitas committed Oct 23, 2024
1 parent e2d0ae4 commit 75dde4c
Showing 1 changed file with 83 additions and 12 deletions.
95 changes: 83 additions & 12 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ services:
# cron daemon with access to Docker socket but no networking
cron-docker:
image: alpinelinux/docker-cli:latest
build:
context: ..
dockerfile: docker/util.Dockerfile
command: crond -f -d7
configs:
# restart worker to prevent memory issues
Expand All @@ -738,10 +741,18 @@ services:
- source: restart_nassl_worker_cron
target: /etc/periodic/15min/restart_nassl_worker
mode: 0755
- source: restart_slow_worker_cron
target: /etc/periodic/daily/restart_slow_worker
mode: 0755
# remove unused old docker images to reduce disk space usage
- source: docker_image_prune
target: /etc/periodic/weekly/docker-image-prune
mode: 0755
environment:
- AUTO_UPDATE_TO
- WORKER_REPLICAS
- WORKER_SLOW_REPLICAS

restart: unless-stopped
logging:
driver: $LOGGING_DRIVER
Expand All @@ -753,6 +764,7 @@ services:
network_mode: "none"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /opt/Internet.nl:/opt/Internet.nl

profiles:
- cron
Expand Down Expand Up @@ -1128,24 +1140,83 @@ configs:
restart_worker_cron:
content: |
#!/bin/sh
# restart slow worker every day to prevent slow memory leaks
# as the slow worker can run very long tasks (eg: report generation)
# we first start a new container before stopping the previous one
set -e
# stop and start worker one at a time to ensure (batch) tasks are still being picked up
# workers are sent a TERM signal with which a 10 minute grace period before QUIT is sent
for worker in $(docker ps --filter label=com.docker.compose.service=worker --quiet); do
docker stop "$$worker"
docker start "$$worker"
done
cd /opt/Internet.nl
SERVICE=worker
REPLICAS=$$WORKER_REPLICAS
COMPOSE_CMD="docker compose --env-file=docker/defaults.env --env-file=docker/host.env --env-file=docker/local.env"
OLD_CONTAINERS=$$($$COMPOSE_CMD ps --format "{{ .Name }}"|grep "$$SERVICE")
# bring up new containers, wait until healthy
$$COMPOSE_CMD up --no-deps --no-recreate --wait --scale="$$SERVICE=$$(($$REPLICAS*2))" "$$SERVICE"
# graceful shutdown and remove old containers
docker rm --force "$$OLD_CONTAINERS"
# restore replica number to original
$$COMPOSE_CMD scale $$SERVICE=$$REPLICAS
restart_nassl_worker_cron:
content: |
#!/bin/sh
# restart slow worker every day to prevent slow memory leaks
# as the slow worker can run very long tasks (eg: report generation)
# we first start a new container before stopping the previous one
set -e
# stop and start worker one at a time to ensure (batch) tasks are still being picked up
# workers are sent a TERM signal with which a 10 minute grace period before QUIT is sent
for worker in $(docker ps --filter label=com.docker.compose.service=worker-nassl --quiet); do
docker stop "$$worker"
docker start "$$worker"
done
cd /opt/Internet.nl
SERVICE=worker-nassl
REPLICAS=$$WORKER_REPLICAS
COMPOSE_CMD="docker compose --env-file=docker/defaults.env --env-file=docker/host.env --env-file=docker/local.env"
OLD_CONTAINERS=$$($$COMPOSE_CMD ps --format "{{ .Name }}"|grep "$$SERVICE")
# bring up new containers, wait until healthy
$$COMPOSE_CMD up --no-deps --no-recreate --wait --scale="$$SERVICE=$$(($$REPLICAS*2))" "$$SERVICE"
# graceful shutdown and remove old containers
docker rm --force "$$OLD_CONTAINERS"
# restore replica number to original
$$COMPOSE_CMD scale $$SERVICE=$$REPLICAS
restart_slow_worker_cron:
content: |
#!/bin/sh
# restart slow worker every day to prevent slow memory leaks
# as the slow worker can run very long tasks (eg: report generation)
# we first start a new container before stopping the previous one
set -e
cd /opt/Internet.nl
SERVICE=worker-slow
REPLICAS=$$WORKER_SLOW_REPLICAS
COMPOSE_CMD="docker compose --env-file=docker/defaults.env --env-file=docker/host.env --env-file=docker/local.env"
OLD_CONTAINERS=$$($$COMPOSE_CMD ps --format "{{ .Name }}"|grep "$$SERVICE")
# bring up new containers, wait until healthy
$$COMPOSE_CMD up --no-deps --no-recreate --wait --scale="$$SERVICE=$$(($$REPLICAS*2))" "$$SERVICE"
# graceful shutdown and remove old containers
docker rm --force "$$OLD_CONTAINERS"
# restore replica number to original
$$COMPOSE_CMD scale $$SERVICE=$$REPLICAS
docker_image_prune:
content: |
Expand Down

0 comments on commit 75dde4c

Please sign in to comment.