Start `piker.service` sub-package

For now just moves everything that was in `piker._daemon` to a subpkg
module but a reorg is coming pronto!
service_subpkg_backup
Tyler Goodlet 2023-03-08 15:14:39 -05:00
parent a70d76e3e6
commit bd248381ea
16 changed files with 63 additions and 51 deletions

View File

@ -1,5 +1,5 @@
# piker: trading gear for hackers. # piker: trading gear for hackers.
# Copyright 2020-eternity Tyler Goodlet (in stewardship for piker0) # Copyright 2020-eternity Tyler Goodlet (in stewardship for pikers)
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by # it under the terms of the GNU Affero General Public License as published by
@ -14,11 +14,11 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
""" '''
piker: trading gear for hackers. piker: trading gear for hackers.
""" '''
from ._daemon import open_piker_runtime from .service import open_piker_runtime
from .data.feed import open_feed from .data.feed import open_feed
__all__ = [ __all__ = [

View File

@ -29,8 +29,15 @@ import tractor
from ..cli import cli from ..cli import cli
from .. import watchlists as wl from .. import watchlists as wl
from ..log import get_console_log, colorize_json, get_logger from ..log import get_console_log, colorize_json, get_logger
from .._daemon import maybe_spawn_brokerd, maybe_open_pikerd from ..service import (
from ..brokers import core, get_brokermod, data maybe_spawn_brokerd,
maybe_open_pikerd,
)
from ..brokers import (
core,
get_brokermod,
data,
)
log = get_logger('cli') log = get_logger('cli')
DEFAULT_BROKER = 'questrade' DEFAULT_BROKER = 'questrade'
@ -60,6 +67,7 @@ def get_method(client, meth_name: str):
print_ok('found!.') print_ok('found!.')
return method return method
async def run_method(client, meth_name: str, **kwargs): async def run_method(client, meth_name: str, **kwargs):
method = get_method(client, meth_name) method = get_method(client, meth_name)
print('running...', end='', flush=True) print('running...', end='', flush=True)
@ -67,19 +75,20 @@ async def run_method(client, meth_name: str, **kwargs):
print_ok(f'done! result: {type(result)}') print_ok(f'done! result: {type(result)}')
return result return result
async def run_test(broker_name: str): async def run_test(broker_name: str):
brokermod = get_brokermod(broker_name) brokermod = get_brokermod(broker_name)
total = 0 total = 0
passed = 0 passed = 0
failed = 0 failed = 0
print(f'getting client...', end='', flush=True) print('getting client...', end='', flush=True)
if not hasattr(brokermod, 'get_client'): if not hasattr(brokermod, 'get_client'):
print_error('fail! no \'get_client\' context manager found.') print_error('fail! no \'get_client\' context manager found.')
return return
async with brokermod.get_client(is_brokercheck=True) as client: async with brokermod.get_client(is_brokercheck=True) as client:
print_ok(f'done! inside client context.') print_ok('done! inside client context.')
# check for methods present on brokermod # check for methods present on brokermod
method_list = [ method_list = [
@ -130,7 +139,6 @@ async def run_test(broker_name: str):
total += 1 total += 1
# check for methods present con brokermod.Client and their # check for methods present con brokermod.Client and their
# results # results
@ -180,7 +188,6 @@ def brokercheck(config, broker):
trio.run(run_test, broker) trio.run(run_test, broker)
@cli.command() @cli.command()
@click.option('--keys', '-k', multiple=True, @click.option('--keys', '-k', multiple=True,
help='Return results only for these keys') help='Return results only for these keys')
@ -335,8 +342,6 @@ def contracts(ctx, loglevel, broker, symbol, ids):
brokermod = get_brokermod(broker) brokermod = get_brokermod(broker)
get_console_log(loglevel) get_console_log(loglevel)
contracts = trio.run(partial(core.contracts, brokermod, symbol)) contracts = trio.run(partial(core.contracts, brokermod, symbol))
if not ids: if not ids:
# just print out expiry dates which can be used with # just print out expiry dates which can be used with

View File

@ -28,7 +28,7 @@ import trio
from ..log import get_logger from ..log import get_logger
from . import get_brokermod from . import get_brokermod
from .._daemon import maybe_spawn_brokerd from ..service import maybe_spawn_brokerd
from .._cacheables import open_cached_client from .._cacheables import open_cached_client

View File

@ -29,8 +29,11 @@ from tractor.trionics import broadcast_receiver
from ..log import get_logger from ..log import get_logger
from ..data.types import Struct from ..data.types import Struct
from .._daemon import maybe_open_emsd from ..service import maybe_open_emsd
from ._messages import Order, Cancel from ._messages import (
Order,
Cancel,
)
from ..brokers import get_brokermod from ..brokers import get_brokermod
if TYPE_CHECKING: if TYPE_CHECKING:

View File

@ -19,16 +19,18 @@ CLI commons.
''' '''
import os import os
from pprint import pformat
from functools import partial
import click import click
import trio import trio
import tractor import tractor
from ..log import get_console_log, get_logger, colorize_json from ..log import (
get_console_log,
get_logger,
colorize_json,
)
from ..brokers import get_brokermod from ..brokers import get_brokermod
from .._daemon import ( from ..service import (
_default_registry_host, _default_registry_host,
_default_registry_port, _default_registry_port,
) )
@ -68,7 +70,7 @@ def pikerd(
''' '''
from .._daemon import open_pikerd from ..service import open_pikerd
log = get_console_log(loglevel) log = get_console_log(loglevel)
if pdb: if pdb:
@ -171,7 +173,7 @@ def cli(
@click.pass_obj @click.pass_obj
def services(config, tl, ports): def services(config, tl, ports):
from .._daemon import ( from ..service import (
open_piker_runtime, open_piker_runtime,
_default_registry_port, _default_registry_port,
_default_registry_host, _default_registry_host,

View File

@ -426,11 +426,15 @@ async def open_ahabd(
finally: finally:
# TODO: ensure loglevel can be set and teardown logs are # TODO: ensure loglevel can be set and teardown logs are
# reported if possible on error or cancel.. # reported if possible on error or cancel..
with trio.CancelScope(shield=True): # XXX WARNING: currently shielding here can result in hangs
await cntr.cancel( # on ctl-c from user.. ideally we can avoid a cancel getting
log_msg_key=conf['log_msg_key'], # consumed and not propagating whilst still doing teardown
stop_predicate=stop_lambda, # logging..
) # with trio.CancelScope(shield=True):
await cntr.cancel(
log_msg_key=conf['log_msg_key'],
stop_predicate=stop_lambda,
)
async def start_ahab( async def start_ahab(

View File

@ -42,7 +42,7 @@ from ..log import (
get_logger, get_logger,
get_console_log, get_console_log,
) )
from .._daemon import maybe_spawn_daemon from ..service import maybe_spawn_daemon
if TYPE_CHECKING: if TYPE_CHECKING:
from ._sharedmem import ( from ._sharedmem import (
@ -69,7 +69,7 @@ class Sampler:
This non-instantiated type is meant to be a singleton within This non-instantiated type is meant to be a singleton within
a `samplerd` actor-service spawned once by the user wishing to a `samplerd` actor-service spawned once by the user wishing to
time-step-sample (real-time) quote feeds, see time-step-sample (real-time) quote feeds, see
``._daemon.maybe_open_samplerd()`` and the below ``.service.maybe_open_samplerd()`` and the below
``register_with_sampler()``. ``register_with_sampler()``.
''' '''
@ -391,7 +391,7 @@ async def spawn_samplerd(
update and increment count write and stream broadcasting. update and increment count write and stream broadcasting.
''' '''
from piker._daemon import Services from piker.service import Services
dname = 'samplerd' dname = 'samplerd'
log.info(f'Spawning `{dname}`') log.info(f'Spawning `{dname}`')

View File

@ -137,7 +137,7 @@ def storesh(
''' '''
from piker.data.marketstore import open_tsdb_client from piker.data.marketstore import open_tsdb_client
from piker._daemon import open_piker_runtime from piker.service import open_piker_runtime
async def main(): async def main():
nonlocal symbols nonlocal symbols
@ -187,7 +187,7 @@ def storage(
''' '''
from piker.data.marketstore import open_tsdb_client from piker.data.marketstore import open_tsdb_client
from piker._daemon import open_piker_runtime from piker.service import open_piker_runtime
async def main(): async def main():
nonlocal symbols nonlocal symbols

View File

@ -58,7 +58,7 @@ from ..log import (
get_logger, get_logger,
get_console_log, get_console_log,
) )
from .._daemon import ( from ..service import (
maybe_spawn_brokerd, maybe_spawn_brokerd,
check_for_service, check_for_service,
) )

View File

@ -19,6 +19,8 @@ Structured, daemon tree service management.
""" """
from __future__ import annotations from __future__ import annotations
from pprint import pformat
from functools import partial
import os import os
from typing import ( from typing import (
Optional, Optional,
@ -35,14 +37,11 @@ import tractor
import trio import trio
from trio_typing import TaskStatus from trio_typing import TaskStatus
from .log import ( from ..log import (
get_logger, get_logger,
get_console_log, get_console_log,
) )
from .brokers import get_brokermod from ..brokers import get_brokermod
from pprint import pformat
from functools import partial
log = get_logger(__name__) log = get_logger(__name__)
@ -669,7 +668,7 @@ async def spawn_brokerd(
) )
# non-blocking setup of brokerd service nursery # non-blocking setup of brokerd service nursery
from .data import _setup_persistent_brokerd from ..data import _setup_persistent_brokerd
await Services.start_service_task( await Services.start_service_task(
dname, dname,
@ -732,7 +731,7 @@ async def spawn_emsd(
) )
# non-blocking setup of clearing service # non-blocking setup of clearing service
from .clearing._ems import _setup_persistent_emsd from ..clearing._ems import _setup_persistent_emsd
await Services.start_service_task( await Services.start_service_task(
'emsd', 'emsd',

View File

@ -24,7 +24,7 @@ from types import ModuleType
from PyQt5.QtCore import QEvent from PyQt5.QtCore import QEvent
import trio import trio
from .._daemon import maybe_spawn_brokerd from ..service import maybe_spawn_brokerd
from . import _event from . import _event
from ._exec import run_qtractor from ._exec import run_qtractor
from ..data.feed import install_brokerd_search from ..data.feed import install_brokerd_search

View File

@ -49,7 +49,7 @@ from qdarkstyle import DarkPalette
import trio import trio
from outcome import Error from outcome import Error
from .._daemon import ( from ..service import (
maybe_open_pikerd, maybe_open_pikerd,
get_tractor_runtime_kwargs, get_tractor_runtime_kwargs,
) )

View File

@ -24,7 +24,7 @@ import tractor
from ..cli import cli from ..cli import cli
from .. import watchlists as wl from .. import watchlists as wl
from .._daemon import maybe_spawn_brokerd from ..service import maybe_spawn_brokerd
_config_dir = click.get_app_dir('piker') _config_dir = click.get_app_dir('piker')

View File

@ -1,7 +1,6 @@
from contextlib import asynccontextmanager as acm from contextlib import asynccontextmanager as acm
from functools import partial from functools import partial
import os import os
from typing import AsyncContextManager
from pathlib import Path from pathlib import Path
from shutil import rmtree from shutil import rmtree
@ -11,7 +10,7 @@ from piker import (
# log, # log,
config, config,
) )
from piker._daemon import ( from piker.service import (
Services, Services,
) )
from piker.clearing._client import open_ems from piker.clearing._client import open_ems
@ -88,7 +87,7 @@ async def _open_test_pikerd(
''' '''
import random import random
from piker._daemon import maybe_open_pikerd from piker.service import maybe_open_pikerd
if reg_addr is None: if reg_addr is None:
port = random.randint(6e3, 7e3) port = random.randint(6e3, 7e3)
@ -151,8 +150,9 @@ async def _open_test_pikerd_and_ems(
fqsn, fqsn,
mode=mode, mode=mode,
loglevel=loglevel, loglevel=loglevel,
) as ems_services): ) as ems_services,
yield (services, ems_services) ):
yield (services, ems_services)
@pytest.fixture @pytest.fixture
@ -168,7 +168,7 @@ def open_test_pikerd_and_ems(
mode, mode,
loglevel, loglevel,
open_test_pikerd open_test_pikerd
) )
@pytest.fixture(scope='module') @pytest.fixture(scope='module')

View File

@ -3,7 +3,7 @@ import trio
from typing import AsyncContextManager from typing import AsyncContextManager
from piker._daemon import Services from piker.service import Services
from piker.log import get_logger from piker.log import get_logger
from elasticsearch import Elasticsearch from elasticsearch import Elasticsearch

View File

@ -9,8 +9,7 @@ import pytest
import trio import trio
import tractor import tractor
from piker.log import get_logger from piker.service import (
from piker._daemon import (
find_service, find_service,
Services, Services,
) )