Add a maybe-startup-runtime manager

cached_feeds
Tyler Goodlet 2021-04-05 08:01:26 -04:00
parent 100e27ac12
commit 28a961ebc0
1 changed files with 29 additions and 16 deletions

View File

@ -18,6 +18,7 @@
Structured, daemon tree service management. Structured, daemon tree service management.
""" """
from functools import partial
from typing import Optional, Union from typing import Optional, Union
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
@ -87,6 +88,18 @@ async def open_pikerd(
yield _services yield _services
@asynccontextmanager
async def maybe_open_runtime(
loglevel: Optional[str] = None,
**kwargs,
) -> None:
if not tractor.current_actor(err_on_no_runtime=False):
async with tractor.open_root_actor(loglevel=loglevel, **kwargs):
yield
else:
yield
@asynccontextmanager @asynccontextmanager
async def maybe_open_pikerd( async def maybe_open_pikerd(
loglevel: Optional[str] = None, loglevel: Optional[str] = None,
@ -100,14 +113,14 @@ async def maybe_open_pikerd(
if loglevel: if loglevel:
get_console_log(loglevel) get_console_log(loglevel)
try: # 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: async with tractor.find_actor(_root_dname) as portal:
assert portal is not None # assert portal is not None
if portal is not None:
yield portal yield portal
return return
except (RuntimeError, AssertionError): # tractor runtime not started yet
# presume pikerd role # presume pikerd role
async with open_pikerd( async with open_pikerd(
loglevel, loglevel,
@ -147,7 +160,7 @@ async def spawn_brokerd(
global _services global _services
assert _services assert _services
await _services.actor_n.start_actor( portal = await _services.actor_n.start_actor(
dname, dname,
enable_modules=_data_mods + [brokermod.__name__], enable_modules=_data_mods + [brokermod.__name__],
loglevel=loglevel, loglevel=loglevel,