Skip to content

Commit

Permalink
Merge branch 'master' into python-3-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jmurty committed May 21, 2018
2 parents 22a246b + 6dff200 commit 99a7497
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
5 changes: 4 additions & 1 deletion ixc_django_docker/bin/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions ixc_django_docker/bin/setup-django.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ cat <<EOF
#
EOF

dockerize -timeout 1m -wait "tcp://$REDIS_ADDRESS"

# Install Node modules.
waitlock.py -b "npm-install.sh '$PROJECT_DIR'"

Expand Down
2 changes: 1 addition & 1 deletion ixc_django_docker/bin/setup-postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ elif [[ -n "$SRC_PGDATABASE" ]]; then
echo "Waited $COUNT seconds for PostgreSQL."
fi
echo "Restore database '$PGDATABASE' from source database '$SRC_PGDATABASE' on tcp://$SRC_PGHOST:$SRC_PGPORT."
PGPASSWORD="$SRC_PGPASSWORD" pg_dump -h "$SRC_PGHOST" -p "$SRC_PGPORT" -U "$SRC_PGUSER" -O -x "$SRC_PGDATABASE" | pv | psql -d "$PGDATABASE" > /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
63 changes: 44 additions & 19 deletions ixc_django_docker/bin/waitlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import os
import subprocess
import sys
import time

from redis.exceptions import ConnectionError
import redis
import redis_lock

Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 99a7497

Please sign in to comment.