Add script to wait for a service

This commit is contained in:
Maxime Bloch 2020-07-24 23:27:48 +02:00
parent 4387f950da
commit 261f1c215a
2 changed files with 24 additions and 12 deletions

View file

@ -5,43 +5,48 @@ services:
ports: ports:
- "8000:8000" - "8000:8000"
environment: environment:
DATABASE_HOST: 'db' DATABASE_HOST: 'database'
CELERY_BROKER: 'redis://redis:6379/0' CELERY_BROKER: 'redis://redis:6379/0'
CELERY_BACKEND: 'redis://redis:6379/1' CELERY_BACKEND: 'redis://redis:6379/1'
depends_on: depends_on:
- db - database
command: > command: >
sh -c "sleep 5 && sh -c "./scripts/wait_for_service database 3306 &&
python manage.py migrate && python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000" python manage.py runserver 0.0.0.0:8000"
celery-worker: celery-worker:
build: . build: .
environment: environment:
DATABASE_HOST: 'db' DATABASE_HOST: 'database'
CELERY_BROKER: 'redis://redis:6379/0' CELERY_BROKER: 'redis://redis:6379/0'
CELERY_BACKEND: 'redis://redis:6379/1' CELERY_BACKEND: 'redis://redis:6379/1'
depends_on: depends_on:
- web - web
- redis - redis
command: > command: >
sh -c "celery -A KeRS worker -l info" sh -c "./scripts/wait_for_service web 8000 &&
celery -A KeRS worker -l info"
celery-beat: celery-beat:
build: . build: .
environment: environment:
DATABASE_HOST: 'db' DATABASE_HOST: 'database'
CELERY_BROKER: 'redis://redis:6379/0' CELERY_BROKER: 'redis://redis:6379/0'
CELERY_BACKEND: 'redis://redis:6379/1' CELERY_BACKEND: 'redis://redis:6379/1'
depends_on: depends_on:
- web - web
- redis - redis
command: > command: >
sh -c "celery -A KeRS beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler" sh -c "./scripts/wait_for_service web 8000 &&
celery -A KeRS beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler"
redis: redis:
image: redis image: redis:latest
# Db stuff # Db stuff
db: database:
image: mysql:8.0.21 image: mariadb:10.5.4-focal
command: --default-authentication-plugin=mysql_native_password
environment: environment:
MYSQL_ROOT_PASSWORD: example MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: kers MYSQL_DATABASE: kers
@ -51,6 +56,6 @@ services:
adminer: adminer:
image: adminer:latest image: adminer:latest
depends_on: depends_on:
- db - database
ports: ports:
- 8080:8080 - 8080:8080

7
scripts/wait_for_service Executable file
View file

@ -0,0 +1,7 @@
if [ $# -ne 2 ]
then
echo "Hostname and port of the service was not specified"
exit 1
fi
while ! nc -z $1 $2; do echo "Not yet available, waiting" && sleep 1; done; echo "Done"