From a987f0ab814394d835adeaad94590d6af4aebe11 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 7 Nov 2022 11:22:38 -0500 Subject: [PATCH] Add registry socket cli flags to all client cmds Allows starting UI apps and passing the `pikerd` registry socket-addr args via `--host` or `--port` such that a separate actor tree can be started by selecting an unused port. This is handy when hacking new features but while also wishing to run a more stable version of the code for trading on the same host. --- piker/_daemon.py | 24 +++++++++++++++++------- piker/cli/__init__.py | 21 ++++++++++++++++++++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/piker/_daemon.py b/piker/_daemon.py index 64c22875..b34b2f85 100644 --- a/piker/_daemon.py +++ b/piker/_daemon.py @@ -254,6 +254,7 @@ async def maybe_open_runtime( @acm async def maybe_open_pikerd( loglevel: Optional[str] = None, + registry_addr: None | tuple = None, **kwargs, ) -> Union[tractor._portal.Portal, Services]: @@ -266,13 +267,21 @@ async def maybe_open_pikerd( get_console_log(loglevel) # subtle, we must have the runtime up here or portal lookup will fail - async with maybe_open_runtime(loglevel, **kwargs): - - async with tractor.find_actor(_root_dname) as portal: - # assert portal is not None - if portal is not None: - yield portal - return + async with ( + maybe_open_runtime(loglevel, **kwargs), + tractor.find_actor(_root_dname) as portal + ): + # connect to any existing daemon presuming + # its registry socket was selected. + if ( + portal is not None + and ( + registry_addr is None + or portal.channel.raddr == registry_addr + ) + ): + yield portal + return # presume pikerd role since no daemon could be found at # configured address @@ -280,6 +289,7 @@ async def maybe_open_pikerd( loglevel=loglevel, debug_mode=kwargs.get('debug_mode', False), + registry_addr=registry_addr, ) as _: # in the case where we're starting up the diff --git a/piker/cli/__init__.py b/piker/cli/__init__.py index 4bfd134a..1386bc83 100644 --- a/piker/cli/__init__.py +++ b/piker/cli/__init__.py @@ -125,8 +125,19 @@ def pikerd( @click.option('--loglevel', '-l', default='warning', help='Logging level') @click.option('--tl', is_flag=True, help='Enable tractor logging') @click.option('--configdir', '-c', help='Configuration directory') +@click.option('--host', '-h', default=None, help='Host addr to bind') +@click.option('--port', '-p', default=None, help='Port number to bind') @click.pass_context -def cli(ctx, brokers, loglevel, tl, configdir): +def cli( + ctx: click.Context, + brokers: list[str], + loglevel: str, + tl: bool, + configdir: str, + host: str, + port: int, + +) -> None: if configdir is not None: assert os.path.isdir(configdir), f"`{configdir}` is not a valid path" config._override_config_dir(configdir) @@ -138,6 +149,13 @@ def cli(ctx, brokers, loglevel, tl, configdir): else: brokermods = [get_brokermod(broker) for broker in brokers] + reg_addr: None | tuple[str, int] = None + if host or port: + reg_addr = ( + host or _registry_host, + int(port) or _registry_port, + ) + ctx.obj.update({ 'brokers': brokers, 'brokermods': brokermods, @@ -146,6 +164,7 @@ def cli(ctx, brokers, loglevel, tl, configdir): 'log': get_console_log(loglevel), 'confdir': config._config_dir, 'wl_path': config._watchlists_data_path, + 'registry_addr': reg_addr, }) # allow enabling same loglevel in ``tractor`` machinery