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
29 changes: 19 additions & 10 deletions api/app/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from task_processor.task_run_method import TaskRunMethod # type: ignore[import-untyped]

from app.routers import ReplicaReadStrategy
from app.utils import get_numbered_env_vars_with_prefix

env = Env()

Expand Down Expand Up @@ -177,11 +178,15 @@
),
}
REPLICA_DATABASE_URLS_DELIMITER = env("REPLICA_DATABASE_URLS_DELIMITER", ",")
REPLICA_DATABASE_URLS = env.list(
"REPLICA_DATABASE_URLS",
subcast=str,
default=[],
delimiter=REPLICA_DATABASE_URLS_DELIMITER,
REPLICA_DATABASE_URLS = (
env.list(
"REPLICA_DATABASE_URLS",
subcast=str,
default=[],
delimiter=REPLICA_DATABASE_URLS_DELIMITER,
)
if not os.getenv("REPLICA_DATABASE_URL_0")
else get_numbered_env_vars_with_prefix("REPLICA_DATABASE_URL_")
)
NUM_DB_REPLICAS = len(REPLICA_DATABASE_URLS)

Expand All @@ -190,11 +195,15 @@
CROSS_REGION_REPLICA_DATABASE_URLS_DELIMITER = env(
"CROSS_REGION_REPLICA_DATABASE_URLS_DELIMITER", ","
)
CROSS_REGION_REPLICA_DATABASE_URLS: list[str] = env.list(
"CROSS_REGION_REPLICA_DATABASE_URLS",
subcast=str,
default=[],
delimiter=CROSS_REGION_REPLICA_DATABASE_URLS_DELIMITER,
CROSS_REGION_REPLICA_DATABASE_URLS = (
env.list(
"CROSS_REGION_REPLICA_DATABASE_URLS",
subcast=str,
default=[],
delimiter=CROSS_REGION_REPLICA_DATABASE_URLS_DELIMITER,
)
if not os.getenv("CROSS_REGION_REPLICA_DATABASE_URL_0")
else get_numbered_env_vars_with_prefix("CROSS_REGION_REPLICA_DATABASE_URL_")
)
NUM_CROSS_REGION_DB_REPLICAS = len(CROSS_REGION_REPLICA_DATABASE_URLS)

Expand Down
18 changes: 18 additions & 0 deletions api/app/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import os

import shortuuid


def create_hash() -> str:
"""Helper function to create a short hash"""
return shortuuid.uuid()


def get_numbered_env_vars_with_prefix(prefix: str) -> list[str]:
"""
Returns a list containing the values of all environment variables whose names have a given prefix followed by an
integer, starting from 0, until no more variables with that prefix are found.
"""
db_urls = []
i = 0
while True:
db_url = os.getenv(f"{prefix}{i}")
if not db_url:
break
db_urls.append(db_url)
i += 1
return db_urls
16 changes: 16 additions & 0 deletions api/tests/unit/app/test_unit_app_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

from app.utils import get_numbered_env_vars_with_prefix


def test_get_numbered_env_vars_with_prefix(monkeypatch: pytest.MonkeyPatch) -> None:
# Given
monkeypatch.setenv("DB_URL_0", "0")
monkeypatch.setenv("DB_URL_1", "1")
monkeypatch.setenv("DB_URL_3", "3")

# When
env_vars = get_numbered_env_vars_with_prefix("DB_URL_")

# Then
assert env_vars == ["0", "1"]