Add --pdb flag to pikerd to enable service debug mode

order_mode_finesse
Tyler Goodlet 2021-04-14 10:56:14 -04:00
parent 7f580ad356
commit feeabb3d28
2 changed files with 46 additions and 16 deletions

View File

@ -43,6 +43,7 @@ _root_modules = [
class Services(BaseModel): class Services(BaseModel):
actor_n: tractor._trionics.ActorNursery actor_n: tractor._trionics.ActorNursery
service_n: trio.Nursery service_n: trio.Nursery
debug_mode: bool # tractor sub-actor debug mode flag
class Config: class Config:
arbitrary_types_allowed = True arbitrary_types_allowed = True
@ -54,9 +55,14 @@ _services: Optional[Services] = None
@asynccontextmanager @asynccontextmanager
async def open_pikerd( async def open_pikerd(
loglevel: Optional[str] = None, loglevel: Optional[str] = None,
**kwargs,
# XXX: you should pretty much never want debug mode
# for data daemons when running in production.
debug_mode: bool = False,
) -> Optional[tractor._portal.Portal]: ) -> Optional[tractor._portal.Portal]:
"""Start a root piker daemon who's lifetime extends indefinitely """
Start a root piker daemon who's lifetime extends indefinitely
until cancelled. until cancelled.
A root actor nursery is created which can be used to create and keep A root actor nursery is created which can be used to create and keep
@ -71,18 +77,22 @@ async def open_pikerd(
# passed through to ``open_root_actor`` # passed through to ``open_root_actor``
name=_root_dname, name=_root_dname,
loglevel=loglevel, loglevel=loglevel,
debug_mode=debug_mode,
# TODO: eventually we should be able to avoid # TODO: eventually we should be able to avoid
# having the root have more then permissions to # having the root have more then permissions to
# spawn other specialized daemons I think? # spawn other specialized daemons I think?
# enable_modules=[__name__], # enable_modules=[__name__],
enable_modules=_root_modules, enable_modules=_root_modules,
) as _, tractor.open_nursery() as actor_nursery: ) as _, tractor.open_nursery() as actor_nursery:
async with trio.open_nursery() as service_nursery: async with trio.open_nursery() as service_nursery:
# assign globally for future daemon/task creation # assign globally for future daemon/task creation
_services = Services( _services = Services(
actor_n=actor_nursery, actor_n=actor_nursery,
service_n=service_nursery service_n=service_nursery,
debug_mode=debug_mode,
) )
yield _services yield _services
@ -93,6 +103,10 @@ async def maybe_open_runtime(
loglevel: Optional[str] = None, loglevel: Optional[str] = None,
**kwargs, **kwargs,
) -> None: ) -> None:
"""
Start the ``tractor`` runtime (a root actor) if none exists.
"""
if not tractor.current_actor(err_on_no_runtime=False): if not tractor.current_actor(err_on_no_runtime=False):
async with tractor.open_root_actor(loglevel=loglevel, **kwargs): async with tractor.open_root_actor(loglevel=loglevel, **kwargs):
yield yield
@ -142,9 +156,11 @@ _data_mods = [
async def spawn_brokerd( async def spawn_brokerd(
brokername,
brokername: str,
loglevel: Optional[str] = None, loglevel: Optional[str] = None,
**tractor_kwargs **tractor_kwargs,
) -> tractor._portal.Portal: ) -> tractor._portal.Portal:
from .data import _setup_persistent_brokerd from .data import _setup_persistent_brokerd
@ -164,6 +180,7 @@ async def spawn_brokerd(
dname, dname,
enable_modules=_data_mods + [brokermod.__name__], enable_modules=_data_mods + [brokermod.__name__],
loglevel=loglevel, loglevel=loglevel,
debug_mode=_services.debug_mode,
**tractor_kwargs **tractor_kwargs
) )
@ -187,16 +204,14 @@ async def spawn_brokerd(
@asynccontextmanager @asynccontextmanager
async def maybe_spawn_brokerd( async def maybe_spawn_brokerd(
brokername: str, brokername: str,
loglevel: Optional[str] = None, loglevel: Optional[str] = None,
# XXX: you should pretty much never want debug mode
# for data daemons when running in production.
debug_mode: bool = True,
**kwargs, **kwargs,
) -> tractor._portal.Portal: ) -> tractor._portal.Portal:
"""If no ``brokerd.{brokername}`` daemon-actor can be found, """
If no ``brokerd.{brokername}`` daemon-actor can be found,
spawn one in a local subactor and return a portal to it. spawn one in a local subactor and return a portal to it.
""" """
@ -229,7 +244,6 @@ async def maybe_spawn_brokerd(
spawn_brokerd, spawn_brokerd,
brokername=brokername, brokername=brokername,
loglevel=loglevel, loglevel=loglevel,
debug_mode=debug_mode,
) )
async with tractor.wait_for_actor(dname) as portal: async with tractor.wait_for_actor(dname) as portal:
@ -237,11 +251,16 @@ async def maybe_spawn_brokerd(
async def spawn_emsd( async def spawn_emsd(
brokername,
brokername: str,
loglevel: Optional[str] = None, loglevel: Optional[str] = None,
**extra_tractor_kwargs **extra_tractor_kwargs
) -> tractor._portal.Portal:
) -> tractor._portal.Portal:
"""
Start the clearing engine under ``pikerd``.
"""
log.info('Spawning emsd') log.info('Spawning emsd')
# TODO: raise exception when _services == None? # TODO: raise exception when _services == None?
@ -254,6 +273,7 @@ async def spawn_emsd(
'piker.clearing._client', 'piker.clearing._client',
], ],
loglevel=loglevel, loglevel=loglevel,
debug_mode=_services.debug_mode, # set by pikerd flag
**extra_tractor_kwargs **extra_tractor_kwargs
) )
return 'emsd' return 'emsd'

View File

@ -32,15 +32,25 @@ _context_defaults = dict(
@click.command() @click.command()
@click.option('--loglevel', '-l', default='warning', help='Logging level') @click.option('--loglevel', '-l', default='warning', help='Logging level')
@click.option('--tl', is_flag=True, help='Enable tractor logging') @click.option('--tl', is_flag=True, help='Enable tractor logging')
@click.option('--pdb', is_flag=True, help='Enable tractor debug mode')
@click.option('--host', '-h', default='127.0.0.1', help='Host address to bind') @click.option('--host', '-h', default='127.0.0.1', help='Host address to bind')
def pikerd(loglevel, host, tl): def pikerd(loglevel, host, tl, pdb):
"""Spawn the piker broker-daemon. """Spawn the piker broker-daemon.
""" """
from .._daemon import _data_mods, open_pikerd from .._daemon import _data_mods, open_pikerd
get_console_log(loglevel) log = get_console_log(loglevel)
if pdb:
log.warning((
"\n"
"!!! You have enabled daemon DEBUG mode !!!\n"
"If a daemon crashes it will likely block"
" the service until resumed from console!\n"
"\n"
))
async def main(): async def main():
async with open_pikerd(loglevel): async with open_pikerd(loglevel, debug_mode=pdb):
await trio.sleep_forever() await trio.sleep_forever()
trio.run(main) trio.run(main)