Skip to content

Commit

Permalink
Merge pull request #49 from dailybruin/ming/update-for-postgres
Browse files Browse the repository at this point in the history
Changed some stuff to get postgres working
  • Loading branch information
DumboOctopus authored Apr 15, 2021
2 parents b06a62a + 711302d commit 5dafdb2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ The API backend for Kerckhoff.. Check out the project's [documentation](http://k

# Local Development

Make sure your `.env` file is correct
```
PYTHONPATH="./:$PYTHONPATH"
OAUTHLIB_RELAX_TOKEN_SCOPE=notnull
DATABASE_URL=postgres://postgres:example@postgres:5432/postgres
DJANGO_SECRET_KEY=password
```

Make sure your `.secrets` file is correct as well

Start the dev server for local development:
```bash
docker-compose up
Expand Down Expand Up @@ -73,3 +83,8 @@ git push origin master
```

You're now ready to continuously ship! ✨ 💅 🛳


# Troubleshooting

If you get this error: `/usr/bin/env: ‘python\r’: No such file or directory`, try changing the `manage.py` file to use LF line endings instead of CRLF.
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ version: "2"
services:
postgres:
image: postgres:9.6
environment:
POSTGRES_PASSWORD: example
web:
restart: always
env_file:
Expand All @@ -11,9 +13,7 @@ services:
image: web
build: ./
command: >
bash -c "python wait_for_postgres.py &&
./manage.py migrate &&
./manage.py runserver 0.0.0.0:8000"
bash -c "python wait_for_postgres.py && ./manage.py migrate && ./manage.py runserver 0.0.0.0:8000"
volumes:
- ./:/code
ports:
Expand Down
14 changes: 5 additions & 9 deletions wait_for_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@
check_timeout = os.getenv("POSTGRES_CHECK_TIMEOUT", 30)
check_interval = os.getenv("POSTGRES_CHECK_INTERVAL", 1)
interval_unit = "second" if check_interval == 1 else "seconds"
config = {
"dbname": os.getenv("POSTGRES_DB", "postgres"),
"user": os.getenv("POSTGRES_USER", "postgres"),
"password": os.getenv("POSTGRES_PASSWORD", ""),
"host": os.getenv("DATABASE_URL", "postgres")
}

DATABASE_URL = os.getenv("DATABASE_URL")

start_time = time()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())


def pg_isready(host, user, password, dbname):
def pg_isready(db_url):
while time() - start_time < check_timeout:
try:
conn = psycopg2.connect(**vars())
conn = psycopg2.connect(db_url)
logger.info("Postgres is ready! ✨ 💅")
conn.close()
return True
Expand All @@ -33,4 +29,4 @@ def pg_isready(host, user, password, dbname):
return False


pg_isready(**config)
pg_isready(DATABASE_URL)

0 comments on commit 5dafdb2

Please sign in to comment.