Bleh! Ok make `open_symcache()` and `@acm`..

Turns in order to make things much cleaner from inside-the-runtime usage
we do probably want to just make the manager async so that we can
generate the cache on demand from async UI inits as well as daemon
actors.. So change to that and instead make `get_symcache()` the helper
that should ONLY be called from sync funcs / offline ledger processing
utils!
account_tests
Tyler Goodlet 2023-07-10 16:54:00 -04:00
parent 4123c97139
commit 243821aab1
1 changed files with 45 additions and 36 deletions

View File

@ -25,8 +25,7 @@ offline usage.
''' '''
from __future__ import annotations from __future__ import annotations
from contextlib import ( from contextlib import (
# asynccontextmanager as acm, asynccontextmanager as acm,
contextmanager as cm,
) )
from pathlib import Path from pathlib import Path
from pprint import pformat from pprint import pformat
@ -38,15 +37,20 @@ from types import ModuleType
from fuzzywuzzy import process as fuzzy from fuzzywuzzy import process as fuzzy
import tomli_w # for fast symbol cache writing import tomli_w # for fast symbol cache writing
import tractor
import trio
try: try:
import tomllib import tomllib
except ModuleNotFoundError: except ModuleNotFoundError:
import tomli as tomllib import tomli as tomllib
from msgspec import field from msgspec import field
from ..log import get_logger from piker.log import get_logger
from .. import config from piker import config
from ..brokers import open_cached_client from piker.brokers import (
open_cached_client,
get_brokermod,
)
from .types import Struct from .types import Struct
if TYPE_CHECKING: if TYPE_CHECKING:
@ -166,13 +170,19 @@ class SymbologyCache(Struct):
_caches: dict[str, SymbologyCache] = {} _caches: dict[str, SymbologyCache] = {}
@cm @acm
def open_symcache( async def open_symcache(
mod: ModuleType, mod_or_name: ModuleType | str,
reload: bool = False, reload: bool = False,
only_from_memcache: bool = False,
) -> SymbologyCache: ) -> SymbologyCache:
if isinstance(mod_or_name, str):
mod = get_brokermod(mod_or_name)
else:
mod: ModuleType = mod_or_name
provider: str = mod.name provider: str = mod.name
# actor-level cache-cache XD # actor-level cache-cache XD
@ -181,8 +191,13 @@ def open_symcache(
try: try:
yield _caches[provider] yield _caches[provider]
except KeyError: except KeyError:
log.warning('No asset info cache exists yet for ' msg: str = (
f'`{provider}`') f'No asset info cache exists yet for `{provider}`'
)
if only_from_memcache:
raise RuntimeError(msg)
else:
log.warning(msg)
cachedir: Path = config.get_conf_dir() / '_cache' cachedir: Path = config.get_conf_dir() / '_cache'
if not cachedir.is_dir(): if not cachedir.is_dir():
@ -204,23 +219,9 @@ def open_symcache(
or not cachefile.is_file() or not cachefile.is_file()
): ):
log.info(f'GENERATING symbology cache for `{mod.name}`') log.info(f'GENERATING symbology cache for `{mod.name}`')
await cache.load()
import tractor # NOTE: only (re-)write if explicit reload or non-existing
import trio
# spawn tractor runtime and generate cache
# if not existing.
async def sched_gen_symcache():
async with (
# only for runtime
tractor.open_nursery(debug_mode=True),
):
return await cache.load()
cache: SymbologyCache = trio.run(sched_gen_symcache)
# only (re-)write if explicit reload or non-existing
cache.write_config() cache.write_config()
else: else:
@ -265,7 +266,6 @@ def open_symcache(
# sanity check asset refs from those (presumably) # sanity check asset refs from those (presumably)
# loaded asset set above. # loaded asset set above.
# src_k: str = pairtable.get('bs_src_asset,
src: Asset = cache.assets[mkt.src.name] src: Asset = cache.assets[mkt.src.name]
assert src == mkt.src assert src == mkt.src
dst: Asset dst: Asset
@ -299,19 +299,28 @@ def get_symcache(
) -> SymbologyCache: ) -> SymbologyCache:
''' '''
Get any available symbology/assets cache from Get any available symbology/assets cache from sync code by
sync code by manually running `trio` to do the work. (maybe) manually running `trio` to do the work.
''' '''
from ..brokers import get_brokermod # spawn tractor runtime and generate cache
# if not existing.
async def sched_gen_symcache():
async with (
# only for runtime's debug mode
tractor.open_nursery(debug_mode=True),
open_symcache(
get_brokermod(provider),
reload=force_reload,
) as symcache,
):
return symcache
try: try:
with open_symcache( cache: SymbologyCache = trio.run(sched_gen_symcache)
get_brokermod(provider),
reload=force_reload,
) as symcache:
return symcache
except BaseException: except BaseException:
import pdbp import pdbp
pdbp.xpm() pdbp.xpm()
return cache