From 4414a71331754cacf44c651e85b8b3683efdfdb6 Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Tue, 13 Dec 2022 20:47:49 +0530 Subject: [PATCH] build: updated config for redis and wait_for_db command --- apiserver/bin/takeoff | 2 +- apiserver/bin/worker | 2 +- apiserver/plane/db/management/__init__.py | 0 .../plane/db/management/commands/__init__.py | 0 .../db/management/commands/wait_for_db.py | 19 ++++++++++++++ apiserver/plane/settings/redis.py | 12 +-------- docker-compose.yml | 26 +++++++++++++------ 7 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 apiserver/plane/db/management/__init__.py create mode 100644 apiserver/plane/db/management/commands/__init__.py create mode 100644 apiserver/plane/db/management/commands/wait_for_db.py diff --git a/apiserver/bin/takeoff b/apiserver/bin/takeoff index d6106003e..b76a9fd3b 100755 --- a/apiserver/bin/takeoff +++ b/apiserver/bin/takeoff @@ -1,5 +1,5 @@ #!/bin/bash set -e - +python manage.py wait_for_db python manage.py migrate exec gunicorn plane.wsgi -k gthread --workers 8 --bind 0.0.0.0:8000 --config gunicorn.config.py --max-requests 10000 --max-requests-jitter 1000 --access-logfile - diff --git a/apiserver/bin/worker b/apiserver/bin/worker index 17b42fd9b..25a947613 100755 --- a/apiserver/bin/worker +++ b/apiserver/bin/worker @@ -1,6 +1,6 @@ #!/bin/bash set -e +python manage.py wait_for_db python manage.py migrate - python manage.py rqworker \ No newline at end of file diff --git a/apiserver/plane/db/management/__init__.py b/apiserver/plane/db/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/apiserver/plane/db/management/commands/__init__.py b/apiserver/plane/db/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/apiserver/plane/db/management/commands/wait_for_db.py b/apiserver/plane/db/management/commands/wait_for_db.py new file mode 100644 index 000000000..365452a7a --- /dev/null +++ b/apiserver/plane/db/management/commands/wait_for_db.py @@ -0,0 +1,19 @@ +import time +from django.db import connections +from django.db.utils import OperationalError +from django.core.management import BaseCommand + +class Command(BaseCommand): + """Django command to pause execution until db is available""" + + def handle(self, *args, **options): + self.stdout.write('Waiting for database...') + db_conn = None + while not db_conn: + try: + db_conn = connections['default'] + except OperationalError: + self.stdout.write('Database unavailable, waititng 1 second...') + time.sleep(1) + + self.stdout.write(self.style.SUCCESS('Database available!')) diff --git a/apiserver/plane/settings/redis.py b/apiserver/plane/settings/redis.py index c1eb1b59a..390a075c8 100644 --- a/apiserver/plane/settings/redis.py +++ b/apiserver/plane/settings/redis.py @@ -6,17 +6,7 @@ from urllib.parse import urlparse def redis_instance(): if settings.REDIS_URL: - tls_url = os.environ.get("REDIS_TLS_URL", False) - url = urlparse(settings.REDIS_URL) - if tls_url: - url = urlparse(tls_url) - ri = redis.Redis( - host=url.hostname, - port=url.port, - password=url.password, - ssl=True, - ssl_cert_reqs=None, - ) + ri = redis.from_url(settings.REDIS_URL, db=0) else: ri = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0) diff --git a/docker-compose.yml b/docker-compose.yml index d1cdd3314..757033e4b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,19 +3,24 @@ version: "3.8" services: db: image: postgres:12-alpine - restart: on-failure + restart: always volumes: - - postgres-data:/var/lib/postgresql/data + - pgdata:/var/lib/postgresql/data environment: POSTGRES_USER: plane POSTGRES_DB: plane POSTGRES_PASSWORD: plane command: postgres -c 'max_connections=1000' + ports: + - "5432:5432" redis: image: redis:6.2.7-alpine - restart: on-failure - command: redis-server --maxmemory-policy allkeys-lru --maxmemory 200mb + restart: always + ports: + - "6379:6379" + volumes: + - redisdata:/data plane-web: image: plane-web @@ -64,16 +69,20 @@ services: - redis command: ./bin/takeoff links: - - db - - redis + - db:db + - redis:redis plane-worker: image: plane-api + container_name: plane-bg depends_on: - redis + - db + - plane-api command: ./bin/worker links: - - redis + - redis:redis + - db:db environment: SENTRY_DSN: $SENTRY_DSN WEB_URL: $WEB_URL @@ -94,4 +103,5 @@ services: volumes: - postgres-data: + pgdata: + redisdata: