Skip to content

Commit

Permalink
Faster post build startup:
Browse files Browse the repository at this point in the history
- use named volumes that mount faster and don't need renewing
- skip docker compose healthcheck
- add healthcheck script to initialize
  • Loading branch information
KevinMind committed Dec 11, 2024
1 parent 88e9d13 commit 3a46ba8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 27 deletions.
1 change: 1 addition & 0 deletions Makefile-docker
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ initialize: ## ensure database exists
@echo "Initializing data..."
@echo "args: $(ARGS)"
$(PYTHON_COMMAND) ./manage.py initialize $(ARGS)
./scripts/healthcheck.py

PYTEST_SRC := src/olympia/

Expand Down
3 changes: 0 additions & 3 deletions Makefile-os
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ ifneq ($(INIT_LOAD),)
INITIALIZE_ARGS += --load $(INIT_LOAD)
endif



DOCKER_BAKE_ARGS := \
--file docker-bake.hcl \
--file .env \
Expand All @@ -43,7 +41,6 @@ DOCKER_COMPOSE_ARGS := \
--remove-orphans \
--no-build \
--quiet-pull \
--renew-anon-volumes

# Paths should be cleaned before mounting .:/data/olympia
# These are files which should be sourced from the container
Expand Down
62 changes: 38 additions & 24 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,9 @@ services:
]
volumes:
- .:/data/olympia

extra_hosts:
- "olympia.test:127.0.0.1"
restart: on-failure:5
# entrypoint.sh takes some time
# we can wait for services to be up and running
healthcheck:
test: ["CMD-SHELL", "DJANGO_SETTINGS_MODULE=olympia celery -A olympia.amo.celery status"]
# The interval is 90s after the start period of 60s
interval: 90s
# 3 failed attempts result in container failure
retries: 3
# While starting, ping faster to get the container healthy as soon as possible
start_interval: 1s
# The start period is 60s
start_period: 120s
depends_on:
- mysqld
- elasticsearch
Expand All @@ -91,18 +78,12 @@ services:
web:
extends:
service: worker
healthcheck:
test: ["CMD-SHELL", "curl --fail --show-error --include --location http://127.0.0.1:8002/__version__"]
retries: 3
interval: 90s
start_interval: 1s
start_period: 120s
command:
- uwsgi --ini /data/olympia/docker/uwsgi.ini
volumes:
# Don't mount generated files. They only exist in the container
# and would otherwiser be deleted by mounting the cwd volume above
- /data/olympia/static-build
- data_static_build:/data/olympia/static-build
- *site-static-mount
- ./package.json:/deps/package.json
- ./package-lock.json:/deps/package-lock.json
Expand All @@ -112,7 +93,7 @@ services:
nginx:
image: nginx
volumes:
- ./docker/nginx/addons.conf:/etc/nginx/conf.d/addons.conf
- data_nginx:/etc/nginx/conf.d
- .:/srv
- *site-static-mount
ports:
Expand All @@ -138,9 +119,22 @@ services:
- "3306:3306"
volumes:
- data_mysqld:/var/lib/mysql
command:
# Optimize for development speed over durability
- --innodb-flush-log-at-trx-commit=0
- --innodb-buffer-pool-size=64M
- --innodb-log-buffer-size=8M
- --innodb-log-file-size=32M
# Skip DNS lookups
- --skip-name-resolve
# Disable performance schema for faster startup
- --performance-schema=OFF
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "--silent"]
test: ["CMD-SHELL", "mysql -u root --silent --execute='SELECT 1;'"]
start_interval: 1s
timeout: 2s
start_period: 10s
retries: 3

elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.3
Expand Down Expand Up @@ -173,9 +167,9 @@ services:
autograph:
image: mozilla/autograph:3.3.2
platform: linux/amd64
command: /go/bin/autograph -c /autograph_localdev_config.yaml
command: /go/bin/autograph -c /data/autograph/autograph_localdev_config.yaml
volumes:
- ./scripts/autograph_localdev_config.yaml:/autograph_localdev_config.yaml
- data_autograph:/data/autograph

addons-frontend:
<<: *env
Expand All @@ -196,13 +190,33 @@ services:

networks:
default:
driver: bridge
enable_ipv6: false

volumes:
# Volumes for static files that should not be
# mounted from the host.
data_static_build:
data_site_static:
# Volume for rabbitmq/redis to avoid anonymous volumes
data_rabbitmq:
data_redis:
data_mysqld:
# Keep this value in sync with Makefile-os
# External volumes must be manually created/destroyed
name: addons-server_data_mysqld
external: true
# Volume for nginx configuration
data_nginx:
driver: local
driver_opts:
type: none
o: bind
device: ${PWD}/docker/nginx
# Volume for autograph configuration
data_autograph:
driver: local
driver_opts:
type: none
o: bind
device: ${PWD}/docker/autograph
File renamed without changes.
43 changes: 43 additions & 0 deletions scripts/healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3

import os
import subprocess
import time
import sys
env = os.environ.copy()

env['DJANGO_SETTINGS_MODULE'] = 'olympia'

def worker_healthcheck():
subprocess.run(
['celery', '-A', 'olympia.amo.celery', 'status'],
env=env,
stdout=subprocess.DEVNULL,
)

def web_healthcheck():
subprocess.run([
'curl',
'--fail',
'--show-error',
'--include',
'--location',
'--silent',
'http://127.0.0.1:8002/__version__'
], stdout=subprocess.DEVNULL)


TIME = time.time()
TIMEOUT = 60
SLEEP = 1

while time.time() - TIME < TIMEOUT:
try:
worker_healthcheck()
web_healthcheck()
print('OK')
sys.exit(0)
except Exception as e:
print(f'Error: {e}')
time.sleep(SLEEP)
SLEEP *= 2

0 comments on commit 3a46ba8

Please sign in to comment.