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:
- "8000:8000"
environment:
DATABASE_HOST: 'db'
DATABASE_HOST: 'database'
CELERY_BROKER: 'redis://redis:6379/0'
CELERY_BACKEND: 'redis://redis:6379/1'
depends_on:
- db
- database
command: >
sh -c "sleep 5 &&
sh -c "./scripts/wait_for_service database 3306 &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
celery-worker:
build: .
environment:
DATABASE_HOST: 'db'
DATABASE_HOST: 'database'
CELERY_BROKER: 'redis://redis:6379/0'
CELERY_BACKEND: 'redis://redis:6379/1'
depends_on:
- web
- redis
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:
build: .
environment:
DATABASE_HOST: 'db'
DATABASE_HOST: 'database'
CELERY_BROKER: 'redis://redis:6379/0'
CELERY_BACKEND: 'redis://redis:6379/1'
depends_on:
- web
- redis
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:
image: redis
image: redis:latest
# Db stuff
db:
image: mysql:8.0.21
command: --default-authentication-plugin=mysql_native_password
database:
image: mariadb:10.5.4-focal
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: kers
@ -51,6 +56,6 @@ services:
adminer:
image: adminer:latest
depends_on:
- db
- database
ports:
- 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"