Skip to content

Commit

Permalink
store config in own module
Browse files Browse the repository at this point in the history
  • Loading branch information
nonnontrivial committed Jul 20, 2024
1 parent e920804 commit e315010
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: "3"

services:
postgres:
image: "postgres:latest"
Expand Down Expand Up @@ -52,7 +53,7 @@ services:
depends_on:
- openmeteo

pp:
producer:
build: ./pp
environment:
API_VERSION: "v1"
Expand All @@ -69,7 +70,7 @@ services:
links:
- rabbitmq

pc:
consumer:
build: ./pc
environment:
AMQP_HOST: "rabbitmq"
Expand Down
2 changes: 1 addition & 1 deletion pc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ RUN pip install --no-cache-dir --upgrade -r ./requirements.txt

COPY . .

CMD python -m pc.consume
CMD python -m pc.main
15 changes: 15 additions & 0 deletions pc/pc/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

PG_USER = os.getenv("PG_USER", "postgres")
PG_PASSWORD = os.getenv("PG_PASSWORD", "password")
PG_DATABASE = os.getenv("PG_DATABASE", "localhost")
PG_HOST = os.getenv("PG_HOST", "postgres")
PG_PORT = int(os.getenv("PG_PORT", 5432))

pg_dsn = f"dbname={PG_DATABASE} user={PG_USER} password={PG_PASSWORD} host={PG_HOST}"

AMQP_USER = os.getenv("AMQP_USER", "guest")
AMQP_PASSWORD = os.getenv("AMQP_PASSWORD", "guest")
AMQP_HOST = os.getenv("AMQP_HOST", "localhost")
AMQP_PREDICTION_QUEUE = os.getenv("AMQP_PREDICTION_QUEUE", "prediction")

20 changes: 4 additions & 16 deletions pc/pc/consume.py → pc/pc/main.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import os
import json
import asyncio
import logging
from dataclasses import dataclass

import psycopg
import aio_pika
import websockets

from pc.config import *

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger(__name__)

PG_USER = os.getenv("PG_USER", "postgres")
PG_PASSWORD = os.getenv("PG_PASSWORD", "password")
PG_DATABASE = os.getenv("PG_DATABASE", "localhost")
PG_HOST = os.getenv("PG_HOST", "postgres")
PG_PORT = int(os.getenv("PG_PORT", 5432))

pg_dsn = f"dbname={PG_DATABASE} user={PG_USER} password={PG_PASSWORD} host={PG_HOST}"

AMQP_USER = os.getenv("AMQP_USER", "guest")
AMQP_PASSWORD = os.getenv("AMQP_PASSWORD", "guest")
AMQP_HOST = os.getenv("AMQP_HOST", "localhost")
AMQP_PREDICTION_QUEUE = os.getenv("AMQP_PREDICTION_QUEUE", "prediction")


# FIXME should be defined elsewhere
@dataclass
Expand Down Expand Up @@ -54,7 +43,6 @@ def initialize_db():
conn.commit()


#
def insert_brightness_message_in_db(message: BrightnessMessage):
"""insert subset of brightness message into the predictions table"""
with psycopg.connect(pg_dsn) as conn:
Expand All @@ -77,7 +65,7 @@ async def main():
amqp_connection = await aio_pika.connect_robust(f"amqp://{AMQP_USER}:{AMQP_PASSWORD}@{AMQP_HOST}")
except Exception as e:
import sys
log.error(f"could not form amqp connection; has rabbitmq started?")
log.error(f"could not form amqp connection because {e}; has rabbitmq started?")
log.warning("exiting")
sys.exit(1)
else:
Expand Down

0 comments on commit e315010

Please sign in to comment.