Skip to content

Commit

Permalink
fix(backend-python): fix support for custom options during startup an…
Browse files Browse the repository at this point in the history
…d add basic doc strings
  • Loading branch information
cb0s committed Mar 14, 2024
1 parent 6ec272b commit 922acab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
17 changes: 15 additions & 2 deletions backend-python/src/telestion/backend/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@

@dataclass
class Service:
"""Helper Class for starting NATS clients also exposing the parsed config."""
nc: NatsClient | None # None if Options.nats = False
"""Configured and started NATS client for this service."""
data_dir: Path
"""Directory where all data (temporary and persistent) should be stored."""
service_name: str
"""Name of this service. Note that it is not necessarily unique!"""
config: TelestionConfig
"""TelestionConfig instance for this service """

# wrapper methods for NatsClient instance for convenience
async def publish(self, **kwargs) -> None:
Expand Down Expand Up @@ -45,18 +50,25 @@ async def close(self) -> None:

@dataclass
class Options:
"""Storing a custom configuration which overwrites the parsed config during startup of the Telestion service."""
nats: bool = True
"""Whether a service should use nats. If set to False no nats client is set up during startup"""
# (officially) we don't support int keys, btw...
overwrite_args: dict[str, Any] | None = None
"""Arguments overwriting the parsed config of a service."""
custom_nc: NatsClient | None = None
"""Custom nats client. During startup no configuration takes place if present."""

def without_nats(self) -> 'Options':
return replace(self, nats=False)
"""Returns a copy of this Options instance with nats switched off."""
return replace(self, nats=False, custom_nc=None)

def with_overwrite_args(self, **kwargs) -> 'Options':
"""Returns a copy of this Options instance with different custom arguments."""
return replace(self, overwrite_args=kwargs)

def with_custom_nc(self, nats_client: NatsClient) -> 'Options':
"""Returns a copy of this Options instance with a custom client."""
return replace(self, custom_nc=nats_client)


Expand All @@ -79,12 +91,13 @@ async def _respond_hc(msg):


async def start_service(opts: Options = None) -> Service:
"""Creates a Service for the given Options and the parsed config and spins up a new NATS client if configured so."""
if opts is None:
opts = Options()

config = build_config()
if opts.overwrite_args is not None:
config.update(opts.overwrite_args)
config = config.model_copy(update=opts.overwrite_args)

service = Service(opts.custom_nc, config.data_dir, config.service_name, config)

Expand Down
5 changes: 3 additions & 2 deletions backend-python/src/telestion/backend/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio

from telestion.backend.config import build_config, TelestionConfig
from telestion.backend.lib import start_service
from telestion.backend.lib import start_service, Options


async def main():
Expand All @@ -10,8 +10,9 @@ async def main():
sys.argv.extend(['--dev', '--NATS_URL', 'nats://172.21.73.221:4222'])
_config = build_config(TelestionConfig)
print(_config)
service = await start_service()
service = await start_service(Options().with_overwrite_args(test="foo"))
await service.nc.close()
print(service.config)


if __name__ == '__main__':
Expand Down

0 comments on commit 922acab

Please sign in to comment.