paper-eng: close context and terminate actor on exit

rekt_pps
Tyler Goodlet 2023-04-06 00:26:53 -04:00
parent 250e1c4c51
commit b7ddf9cb05
1 changed files with 10 additions and 3 deletions

View File

@ -666,21 +666,23 @@ async def open_paperboi(
else: else:
broker, symbol, expiry = unpack_fqme(fqme) broker, symbol, expiry = unpack_fqme(fqme)
we_spawned: bool = False
service_name = f'paperboi.{broker}' service_name = f'paperboi.{broker}'
async with ( async with (
tractor.find_actor(service_name) as portal, tractor.find_actor(service_name) as portal,
tractor.open_nursery() as tn, tractor.open_nursery() as tn,
): ):
# only spawn if no paperboi already is up # NOTE: only spawn if no paperboi already is up since we likely
# (we likely don't need more then one proc for basic # don't need more then one actor for simulated order clearing
# simulated order clearing) # per broker-backend.
if portal is None: if portal is None:
log.info('Starting new paper-engine actor') log.info('Starting new paper-engine actor')
portal = await tn.start_actor( portal = await tn.start_actor(
service_name, service_name,
enable_modules=[__name__] enable_modules=[__name__]
) )
we_spawned = True
async with portal.open_context( async with portal.open_context(
trades_dialogue, trades_dialogue,
@ -690,3 +692,8 @@ async def open_paperboi(
) as (ctx, first): ) as (ctx, first):
yield ctx, first yield ctx, first
# tear down connection and any spawned actor on exit
await ctx.cancel()
if we_spawned:
await portal.cancel_actor()