Cancel symbol unsub if brokerd is already down

kivy_mainline_and_py3.8
Tyler Goodlet 2018-07-06 17:26:24 -04:00
parent 6c977cfb7b
commit 4427e2869c
1 changed files with 67 additions and 65 deletions

View File

@ -371,7 +371,7 @@ async def update_quotes(
grid.render_rows(cache) grid.render_rows(cache)
log.debug("Waiting on quotes") log.debug("Waiting on quotes")
log.warn("Server connection dropped") log.warn("`brokerd` connection dropped")
nursery.cancel_scope.cancel() nursery.cancel_scope.cancel()
@ -391,80 +391,82 @@ async def _async_main(name, portal, tickers, brokermod, rate):
"piker.brokers.core", 'start_quote_stream', "piker.brokers.core", 'start_quote_stream',
broker=brokermod.name, tickers=tickers) broker=brokermod.name, tickers=tickers)
try: async with trio.open_nursery() as nursery:
async with trio.open_nursery() as nursery: # get first quotes response
# get first quotes response log.debug("Waiting on first quote...")
log.debug("Waiting on first quote...") quotes = await agen.__anext__()
quotes = await agen.__anext__() first_quotes = [
first_quotes = [ brokermod.format_quote(quote, symbol_data=sd)[0]
brokermod.format_quote(quote, symbol_data=sd)[0] for quote in quotes.values()]
for quote in quotes.values()]
if first_quotes[0].get('last') is None: if first_quotes[0].get('last') is None:
log.error("Broker API is down temporarily") log.error("Broker API is down temporarily")
nursery.cancel_scope.cancel() nursery.cancel_scope.cancel()
return return
# build out UI # build out UI
Window.set_title(f"watchlist: {name}\t(press ? for help)") Window.set_title(f"watchlist: {name}\t(press ? for help)")
Builder.load_string(_kv) Builder.load_string(_kv)
box = BoxLayout(orientation='vertical', padding=5, spacing=5) box = BoxLayout(orientation='vertical', padding=5, spacing=5)
# define bid-ask "stacked" cells # define bid-ask "stacked" cells
# (TODO: needs some rethinking and renaming for sure) # (TODO: needs some rethinking and renaming for sure)
bidasks = brokermod._bidasks bidasks = brokermod._bidasks
# add header row # add header row
headers = first_quotes[0].keys() headers = first_quotes[0].keys()
header = Row( header = Row(
{key: key for key in headers}, {key: key for key in headers},
headers=headers, headers=headers,
bidasks=bidasks, bidasks=bidasks,
is_header_row=True, is_header_row=True,
size_hint=(1, None), size_hint=(1, None),
) )
box.add_widget(header) box.add_widget(header)
# build grid # build grid
grid = TickerTable( grid = TickerTable(
cols=1, cols=1,
size_hint=(1, None), size_hint=(1, None),
) )
for ticker_record in first_quotes: for ticker_record in first_quotes:
grid.append_row(ticker_record, bidasks=bidasks) grid.append_row(ticker_record, bidasks=bidasks)
# associate the col headers row with the ticker table even though # associate the col headers row with the ticker table even though
# they're technically wrapped separately in containing BoxLayout # they're technically wrapped separately in containing BoxLayout
header.table = grid header.table = grid
# mark the initial sorted column header as bold and underlined # mark the initial sorted column header as bold and underlined
sort_cell = header.get_cell(grid.sort_key) sort_cell = header.get_cell(grid.sort_key)
sort_cell.bold = sort_cell.underline = True sort_cell.bold = sort_cell.underline = True
grid.last_clicked_col_cell = sort_cell grid.last_clicked_col_cell = sort_cell
# set up a pager view for large ticker lists # set up a pager view for large ticker lists
grid.bind(minimum_height=grid.setter('height')) grid.bind(minimum_height=grid.setter('height'))
pager = PagerView(box, grid, nursery) pager = PagerView(box, grid, nursery)
box.add_widget(pager) box.add_widget(pager)
widgets = { widgets = {
# 'anchor': anchor, # 'anchor': anchor,
'root': box, 'root': box,
'grid': grid, 'grid': grid,
'box': box, 'box': box,
'header': header, 'header': header,
'pager': pager, 'pager': pager,
} }
nursery.start_soon( nursery.start_soon(
update_quotes, nursery, brokermod, widgets, agen, sd, quotes) update_quotes, nursery, brokermod, widgets, agen, sd, quotes)
try:
# Trio-kivy entry point. # Trio-kivy entry point.
await async_runTouchApp(widgets['root']) # run kivy await async_runTouchApp(widgets['root']) # run kivy
await agen.aclose() # cancel aysnc gen call await agen.aclose() # cancel aysnc gen call
finally: finally:
# un-subscribe from symbols stream # un-subscribe from symbols stream (cancel if brokerd
await portal.run( # was already torn down - say by SIGINT)
"piker.brokers.core", 'modify_quote_stream', with trio.move_on_after(0.2):
broker=brokermod.name, tickers=[]) await portal.run(
"piker.brokers.core", 'modify_quote_stream',
broker=brokermod.name, tickers=[])
# cancel GUI update task # cancel GUI update task
nursery.cancel_scope.cancel() nursery.cancel_scope.cancel()