Skip to content

Commit 3f151db

Browse files
refactor: split result_backend & broker_url into multiple config fields
1 parent 9769ef8 commit 3f151db

4 files changed

Lines changed: 62 additions & 21 deletions

File tree

config/sample.config.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# required configuration variables for docker compose setup
2-
# broker_url = "redis://redis:6379"
3-
# result_backend = "db+postgresql://smt:smt@postgres:5432"
2+
# postgres_host = "postgres"
3+
# postgres_password = "smt"
4+
# postgres_user = "smt"
5+
# redis_host = "redis"

docs/configuration.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ All lot of configuration values come with defaults.
2727
For running the services using Docker Compose set broker URL and result backend to:
2828

2929
```toml
30-
broker_url = "redis://redis:6379"
31-
result_backend = "db+postgresql://smt:smt@postgres:5432"
30+
postgres_host = "postgres"
31+
postgres_password = "smt"
32+
postgres_user = "smt"
33+
redis_host = "redis"
3234
```
3335

3436

@@ -38,8 +40,10 @@ All environment variables are prefixed with `SMT_`. For example above
3840
configuration via environment variables is done like this:
3941

4042
```sh
41-
SMT_BROKER_URL="redis://redis:6379"
42-
SMT_RESULT_BACKEND="db+postgresql://smt:smt@postgres:5432"
43+
SMT_POSTGRES_HOST = "postgres"
44+
SMT_POSTGRES_PASSWORD = "smt"
45+
SMT_POSTGRES_USER = "smt"
46+
SMT_REDIS_HOST = "redis"
4347
```
4448

4549

sketch_map_tool/config.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import os
33

4-
from pydantic import field_validator
4+
from pydantic import computed_field, field_validator
55
from pydantic_settings import (
66
BaseSettings,
77
PydanticBaseSettingsSource,
@@ -23,15 +23,22 @@ def get_config_path() -> str:
2323

2424

2525
class Config(BaseSettings):
26-
broker_url: str = "redis://localhost:6379"
2726
cleanup_map_frames_interval: str = "12 months"
2827
data_dir: str = str(get_project_root() / "data") # TODO: make this a Path
2928
esri_api_key: str = ""
3029
log_level: str = "INFO"
3130
max_nr_simultaneous_uploads: int = 100
3231
model_type_sam: str = "vit_b"
3332
point_area_threshold: float = 0.00047
34-
result_backend: str = "db+postgresql://smt:smt@localhost:5432"
33+
postgres_host: str = "localhost"
34+
postgres_port: str = "5432"
35+
postgres_dbname: str = ""
36+
postgres_user: str = "smt"
37+
postgres_password: str = "smt"
38+
redis_host: str = "localhost"
39+
redis_port: str = "6379"
40+
redis_db_number: str = ""
41+
redis_password: str | None = None
3542
user_agent: str = "sketch-map-tool"
3643
weights_dir: str = str(get_project_root() / "weights") # TODO: make this a Path
3744
wms_layers_esri_world_imagery: str = "world_imagery"
@@ -52,6 +59,34 @@ class Config(BaseSettings):
5259
toml_file=get_config_path(),
5360
)
5461

62+
@computed_field
63+
@property
64+
def result_backend(self) -> str:
65+
return "db+postgresql://{user}:{password}@{host}:{port}/{dbname}".format(
66+
user=self.postgres_user,
67+
password=self.postgres_password,
68+
host=self.postgres_host,
69+
port=self.postgres_port,
70+
dbname=self.postgres_dbname,
71+
)
72+
73+
@computed_field
74+
@property
75+
def broker_url(self) -> str:
76+
if self.redis_password is None:
77+
return "redis://{host}:{port}/{db_number}".format(
78+
host=self.redis_host,
79+
port=self.redis_port,
80+
db_number=self.redis_db_number,
81+
)
82+
else:
83+
return "redis://:{password}@{host}:{port}/{db_number}".format(
84+
password=self.redis_password,
85+
host=self.redis_host,
86+
port=self.redis_port,
87+
db_number=self.redis_db_number,
88+
)
89+
5590
@classmethod
5691
def settings_customise_sources(
5792
cls,

tests/integration/conftest.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ def postgres_container(monkeypatch_session):
4040
Connection string will be different for each test session.
4141
"""
4242
with PostgresContainer("postgres:15") as postgres:
43-
conn = "db+postgresql://{user}:{password}@127.0.0.1:{port}/{database}".format(
44-
user=postgres.username,
45-
password=postgres.password,
46-
port=postgres.get_exposed_port(5432), # 5432 is default port of postgres
47-
database=postgres.dbname,
43+
monkeypatch_session.setattr(
44+
CONFIG,
45+
"postgres_port",
46+
postgres.get_exposed_port(5432),
4847
)
49-
monkeypatch_session.setattr(CONFIG, "result_backend", conn)
50-
yield {"connection_url": conn}
48+
monkeypatch_session.setattr(CONFIG, "postgres_dbname", postgres.dbname)
49+
monkeypatch_session.setattr(CONFIG, "postgres_user", postgres.username)
50+
monkeypatch_session.setattr(CONFIG, "postgres_password", postgres.password)
51+
yield
5152
# cleanup
5253

5354

@@ -59,17 +60,16 @@ def redis_container(monkeypatch_session):
5960
"""
6061
with RedisContainer("redis:7") as redis:
6162
port = redis.get_exposed_port(6379) # 6379 is default port of redis
62-
conn = f"redis://127.0.0.1:{port}"
63-
monkeypatch_session.setattr(CONFIG, "broker_url", conn)
64-
yield {"connection_url": conn}
63+
monkeypatch_session.setattr(CONFIG, "redis_port", port)
64+
yield redis
6565
# cleanup
6666

6767

6868
@pytest.fixture(scope="session", autouse=True)
6969
def celery_config(postgres_container, redis_container):
7070
"""Set Celery config to point at testcontainers."""
71-
CELERY_CONFIG["result_backend"] = postgres_container["connection_url"]
72-
CELERY_CONFIG["broker_url"] = redis_container["connection_url"]
71+
CELERY_CONFIG["result_backend"] = CONFIG.result_backend
72+
CELERY_CONFIG["broker_url"] = CONFIG.broker_url
7373
return CELERY_CONFIG
7474

7575

0 commit comments

Comments
 (0)