From 5ac8ab16f894f931c842daf738de61ccc4410fea Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Thu, 22 Jun 2023 04:28:34 +0530 Subject: [PATCH] add DATABASE_URL export to celery scripts (#1402) --- celery_beat-ecs.sh | 3 +++ celery_beat.sh | 5 +++++ celery_worker-ecs.sh | 3 +++ celery_worker.sh | 5 +++++ start | 14 +++----------- start-ecs.sh | 15 ++++----------- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/celery_beat-ecs.sh b/celery_beat-ecs.sh index cb4fcd6e30..dd5dca1b6a 100755 --- a/celery_beat-ecs.sh +++ b/celery_beat-ecs.sh @@ -1,3 +1,6 @@ #!/bin/bash +if [ -z "${DATABASE_URL}" ]; then + export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" +fi celery --app=config.celery_app beat --loglevel=info diff --git a/celery_beat.sh b/celery_beat.sh index 72ffb21a1b..df7ef10e9d 100755 --- a/celery_beat.sh +++ b/celery_beat.sh @@ -1,2 +1,7 @@ +#!/bin/bash + +if [ -z "${DATABASE_URL}" ]; then + export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" +fi export NEW_RELIC_CONFIG_FILE=/etc/newrelic.ini newrelic-admin run-program celery --app=config.celery_app beat --loglevel=info diff --git a/celery_worker-ecs.sh b/celery_worker-ecs.sh index bce4c5aad1..6d0e19cab3 100755 --- a/celery_worker-ecs.sh +++ b/celery_worker-ecs.sh @@ -1,3 +1,6 @@ #!/bin/bash +if [ -z "${DATABASE_URL}" ]; then + export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" +fi celery --app=config.celery_app worker --max-tasks-per-child=6 --loglevel=info diff --git a/celery_worker.sh b/celery_worker.sh index 0b801c6a7b..1dbd467339 100755 --- a/celery_worker.sh +++ b/celery_worker.sh @@ -1,2 +1,7 @@ +#!/bin/bash + +if [ -z "${DATABASE_URL}" ]; then + export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" +fi export NEW_RELIC_CONFIG_FILE=/etc/newrelic.ini newrelic-admin run-program celery --app=config.celery_app worker --max-tasks-per-child=6 --loglevel=info diff --git a/start b/start index 79138e2917..c3ab83a932 100644 --- a/start +++ b/start @@ -4,11 +4,9 @@ set -o errexit set -o pipefail set -o nounset -if [ -z "${POSTGRES_USER}" ]; then - base_postgres_image_default_user='postgres' - export POSTGRES_USER="${base_postgres_image_default_user}" +if [ -z "${DATABASE_URL}" ]; then + export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" fi -export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" postgres_ready() { python << END @@ -17,13 +15,7 @@ import sys import psycopg try: - psycopg.connect( - dbname="${POSTGRES_DB}", - user="${POSTGRES_USER}", - password="${POSTGRES_PASSWORD}", - host="${POSTGRES_HOST}", - port="${POSTGRES_PORT}", - ) + psycopg.connect(conninfo="${DATABASE_URL}") except psycopg.OperationalError: sys.exit(-1) sys.exit(0) diff --git a/start-ecs.sh b/start-ecs.sh index 80fd5f4a45..c0f52a5273 100755 --- a/start-ecs.sh +++ b/start-ecs.sh @@ -4,11 +4,9 @@ set -o errexit set -o pipefail set -o nounset -if [ -z "${POSTGRES_USER}" ]; then - base_postgres_image_default_user='postgres' - export POSTGRES_USER="${base_postgres_image_default_user}" +if [ -z "${DATABASE_URL}" ]; then + export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" fi -export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" postgres_ready() { python << END @@ -17,19 +15,14 @@ import sys import psycopg try: - psycopg.connect( - dbname="${POSTGRES_DB}", - user="${POSTGRES_USER}", - password="${POSTGRES_PASSWORD}", - host="${POSTGRES_HOST}", - port="${POSTGRES_PORT}", - ) + psycopg.connect(conninfo="${DATABASE_URL}") except psycopg.OperationalError: sys.exit(-1) sys.exit(0) END } + until postgres_ready; do >&2 echo 'Waiting for PostgreSQL to become available...' sleep 1