Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
env:
# Environment variables to configure the database
POSTGRES_USER: deploy_tracker
POSTGRES_PASSWORD: password
POSTGRES_PASSWORD: murilinroblox
POSTGRES_DB: deploy_tracker
ports:
- 5432:5432 # Container port of db
Expand All @@ -52,8 +52,13 @@ jobs:

- name: Run tests
env:
POSTGRES_USER: deploy_tracker
POSTGRES_PASSWORD: murilinroblox
POSTGRES_DB: deploy_tracker
DB_HOST: localhost
DB_PORT: 5432
REDIS_HOST: localhost
REDIS_PORT: 6379
run: pytest

- name: Build Docker Image
Expand Down
18 changes: 16 additions & 2 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@
import os

# -------------------------------------------------
REDIS_HOST = os.getenv("REDIS_HOST", "redis")
REDIS_HOST = os.getenv("REDIS_HOST")
REDIS_PORT = os.getenv("REDIS_PORT")

required_envs = {
"REDIS_HOST": REDIS_HOST,
"REDIS_PORT": REDIS_PORT,
}

missings_env = [name for name, value in required_envs.items() if value is None]

if missings_env:
raise RuntimeError(
f"Missing required environments variables: {', '.join(missings_env)}"
)

# -------------------------------------------------
r = redis.Redis(host=REDIS_HOST, port=6379, decode_responses=True)
r = redis.Redis(host=REDIS_HOST, port=int(REDIS_PORT), decode_responses=True)
# decode_responses means -> Redis returns strings instead of bytes, which is easier to work with.
# --------------------------------------------------

Expand Down
28 changes: 22 additions & 6 deletions celery_app.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
from celery import Celery
from cache import REDIS_HOST
import os


# -------------------------------------------------
REDIS_HOST = os.getenv("REDIS_HOST")
REDIS_PORT = os.getenv("REDIS_PORT")

required_envs = {
"REDIS_HOST": REDIS_HOST,
"REDIS_PORT": REDIS_PORT,
}

missing_envs = [name for name, value in required_envs.items() if value is None]

if missing_envs:
raise RuntimeError(
f"Missing required environment variables: {', '.join(missing_envs)}"
)
# -------------------------------------------------
REDIS_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/0"
# -------------------------------------------------
celery_app = Celery(
"deploy_tracker",
broker=f"redis://{REDIS_HOST}:6379/0",
backend=f"redis://{REDIS_HOST}:6379/0",
broker=REDIS_URL,
backend=REDIS_URL,
)
# -------------------------------------------------

celery_app.conf.beat_schedule = {
"health_check_30s": {
"task": "tasks.health_checker",
"schedule": 30.0, # seconds
"args": (
1,
), # TODO: Dynamically check all registered applications instead of hardcoded id
"args": (1,),
},
}

Expand Down
29 changes: 24 additions & 5 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@

# ------------------------------------------------------

# DB_HOST defaults to "db" for Docker Compose, override with env variable for other environments.
DB_HOST = os.getenv("DB_HOST", "db")
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
POSTGRES_DB = os.getenv("POSTGRES_DB")
DB_HOST = os.getenv("DB_HOST")
DB_PORT = os.getenv("DB_PORT")

required_envs = {
"POSTGRES_USER": POSTGRES_USER,
"POSTGRES_PASSWORD": POSTGRES_PASSWORD,
"POSTGRES_DB": POSTGRES_DB,
"DB_HOST": DB_HOST,
"DB_PORT": DB_PORT,
}

missing_envs = [name for name, value in required_envs.items() if value is None]

if missing_envs:
raise RuntimeError(
f"Missing required environment variables: {', '.join(missing_envs)}"
)


# ------------------------------------------------------

engine = create_engine(
f"postgresql+psycopg2://deploy_tracker:password@{DB_HOST}/deploy_tracker"
)
DATABASE_URL = f"postgresql+psycopg2://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{DB_HOST}:{DB_PORT}/{POSTGRES_DB}"

engine = create_engine(DATABASE_URL)

# ------------------------------------------------------

Expand Down
Loading