diff --git a/ixc_django_docker/bin/help.sh b/ixc_django_docker/bin/help.sh index 2e2ed19..56fc868 100755 --- a/ixc_django_docker/bin/help.sh +++ b/ixc_django_docker/bin/help.sh @@ -99,8 +99,11 @@ Here is a list of frequently used commands you might want to run: Create a PostgreSQL database with a name derived from the project directory and current Git branch. - Seed the new database it with data from the 'SRC_PG*' environment + Seed the new database with data from the 'SRC_PG*' environment variables, if defined. + + Additional 'pg_dump' args can be specified in the 'SRC_PGDUMP_EXTRA' + environment variable. E.g. '--exclude-table-data django_session' Drop and recreate the database if 'SETUP_POSTGRES_FORCE' is defined. diff --git a/ixc_django_docker/bin/setup-django.sh b/ixc_django_docker/bin/setup-django.sh index 6b6f9bb..f0c4d37 100755 --- a/ixc_django_docker/bin/setup-django.sh +++ b/ixc_django_docker/bin/setup-django.sh @@ -23,6 +23,8 @@ cat < /dev/null + PGPASSWORD="$SRC_PGPASSWORD" pg_dump $SRC_PGDUMP_EXTRA -h "$SRC_PGHOST" -p "$SRC_PGPORT" -U "$SRC_PGUSER" -O -x "$SRC_PGDATABASE" | pv | psql -d "$PGDATABASE" > /dev/null fi diff --git a/ixc_django_docker/bin/waitlock.py b/ixc_django_docker/bin/waitlock.py index 45c2fba..f7583dc 100755 --- a/ixc_django_docker/bin/waitlock.py +++ b/ixc_django_docker/bin/waitlock.py @@ -9,7 +9,9 @@ import os import subprocess import sys +import time +from redis.exceptions import ConnectionError import redis import redis_lock @@ -80,25 +82,48 @@ def waitlock(cmd, block=False): # Create lock object. lock = redis_lock.Lock(conn, name=cmd, expire=60, auto_renewal=True) - # Attempt to acquire lock. - if lock.acquire(blocking=False): - logger.debug('Acquired lock. Executing command: %s' % cmd) - - # Block until lock is available, then execute. - elif block: - logger.info('Waiting to acquire lock for command: %s' % cmd) - when = datetime.datetime.now() - lock.acquire() - duration = datetime.datetime.now() - when - logger.info( - 'Waited %s seconds to acquire lock. Executing command: %s' % ( - duration.seconds, - cmd, - )) - - else: - logger.info('Unable to acquire lock.') - return 0 + # Retry on connection errors, when told to block. + while True: + try: + # Attempt to acquire lock. + if lock.acquire(blocking=False): + logger.debug('Acquired lock. Executing command: %s' % cmd) + + # Block until lock is available, then execute. + elif block: + logger.info('Waiting to acquire lock for command: %s' % cmd) + when = datetime.datetime.now() + lock.acquire() + duration = datetime.datetime.now() - when + logger.info( + 'Waited %s seconds to acquire lock. Executing command: %s' % ( + duration.seconds, + cmd, + )) + + # Abort. + else: + logger.info('Unable to acquire lock.') + return 0 + except ConnectionError: + # Retry. + if block: + logger.warning( + "Unable to connect to Redis at '%s:%s'. Retrying in 1 " + 'second.' % ( + REDIS_HOST, + REDIS_PORT, + )) + time.sleep(1) + continue + logger.info( + 'Unable to acquire lock. Unable to connect to Redis at ' + "'%s:%s'." % ( + REDIS_HOST, + REDIS_PORT, + )) + return 0 + break # Execute command and get exit code from subprocess. exit_code = execute(cmd)[0]