Skip to content

Commit 89207f2

Browse files
committed
Try having brokerd eps defined in .brokers._daemon
Since it's a bit weird having service specific implementation details inside the general service `._daemon` mod, and since i'd mentioned trying this re-org; let's do it B) Requires enabling the new mod in both `pikerd` and `brokerd` and obviously a bit more runtime-loading of the service modules in the `brokerd` service eps to avoid import cycles. Also moved `_setup_persistent_brokerd()` into the new mod since the naming would place it there even though the implementation really wouldn't (longer run) since we want to split up `.data.feed` layer backend-invoked eps into a separate actor eventually from the "actual" `brokerd` which will be the actor running **only** the trade control eps (eg. trades_dialogue()` and friends).
1 parent 2fc1787 commit 89207f2

File tree

8 files changed

+179
-136
lines changed

8 files changed

+179
-136
lines changed

piker/accounting/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def broker_init(
7979
# enabled.append('piker.data.feed')
8080

8181
# non-blocking setup of brokerd service nursery
82-
from ..data import _setup_persistent_brokerd
82+
from ..brokers import _setup_persistent_brokerd
8383

8484
return (
8585
start_actor_kwargs, # to `ActorNursery.start_actor()`

piker/brokers/_daemon.py

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# piker: trading gear for hackers
2+
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
3+
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU Affero General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU Affero General Public License for more details.
13+
14+
# You should have received a copy of the GNU Affero General Public License
15+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
'''
18+
Broker-daemon-actor "endpoint-hooks": the service task entry points for
19+
``brokerd``.
20+
21+
'''
22+
from contextlib import (
23+
asynccontextmanager as acm,
24+
)
25+
26+
import tractor
27+
import trio
28+
29+
from . import _util
30+
from . import get_brokermod
31+
32+
# `brokerd` enabled modules
33+
# TODO: move this def to the `.data` subpkg..
34+
# NOTE: keeping this list as small as possible is part of our caps-sec
35+
# model and should be treated with utmost care!
36+
_data_mods = [
37+
'piker.brokers.core',
38+
'piker.brokers.data',
39+
'piker.brokers._daemon',
40+
'piker.data',
41+
'piker.data.feed',
42+
'piker.data._sampling'
43+
]
44+
45+
46+
# TODO: we should rename the daemon to datad prolly once we split up
47+
# broker vs. data tasks into separate actors?
48+
@tractor.context
49+
async def _setup_persistent_brokerd(
50+
ctx: tractor.Context,
51+
brokername: str,
52+
loglevel: str | None = None,
53+
54+
) -> None:
55+
'''
56+
Allocate a actor-wide service nursery in ``brokerd``
57+
such that feeds can be run in the background persistently by
58+
the broker backend as needed.
59+
60+
'''
61+
log = _util.get_console_log(
62+
loglevel or tractor.current_actor().loglevel,
63+
name=f'{_util.subsys}.{brokername}',
64+
)
65+
# set global for this actor to this new process-wide instance B)
66+
_util.log = log
67+
68+
from piker.data.feed import (
69+
_bus,
70+
get_feed_bus,
71+
)
72+
global _bus
73+
assert not _bus
74+
75+
async with trio.open_nursery() as service_nursery:
76+
# assign a nursery to the feeds bus for spawning
77+
# background tasks from clients
78+
get_feed_bus(brokername, service_nursery)
79+
80+
# unblock caller
81+
await ctx.started()
82+
83+
# we pin this task to keep the feeds manager active until the
84+
# parent actor decides to tear it down
85+
await trio.sleep_forever()
86+
87+
88+
async def spawn_brokerd(
89+
90+
brokername: str,
91+
loglevel: str | None = None,
92+
93+
**tractor_kwargs,
94+
95+
) -> bool:
96+
97+
from piker.service import Services
98+
from piker.service._util import log # use service mngr log
99+
100+
log.info(f'Spawning {brokername} broker daemon')
101+
102+
brokermod = get_brokermod(brokername)
103+
dname = f'brokerd.{brokername}'
104+
105+
extra_tractor_kwargs = getattr(brokermod, '_spawn_kwargs', {})
106+
tractor_kwargs.update(extra_tractor_kwargs)
107+
108+
# ask `pikerd` to spawn a new sub-actor and manage it under its
109+
# actor nursery
110+
modpath = brokermod.__name__
111+
broker_enable = [modpath]
112+
for submodname in getattr(
113+
brokermod,
114+
'__enable_modules__',
115+
[],
116+
):
117+
subpath = f'{modpath}.{submodname}'
118+
broker_enable.append(subpath)
119+
120+
portal = await Services.actor_n.start_actor(
121+
dname,
122+
enable_modules=_data_mods + broker_enable,
123+
loglevel=loglevel,
124+
debug_mode=Services.debug_mode,
125+
**tractor_kwargs
126+
)
127+
128+
# non-blocking setup of brokerd service nursery
129+
await Services.start_service_task(
130+
dname,
131+
portal,
132+
133+
# signature of target root-task endpoint
134+
_setup_persistent_brokerd,
135+
brokername=brokername,
136+
loglevel=loglevel,
137+
)
138+
return True
139+
140+
141+
@acm
142+
async def maybe_spawn_brokerd(
143+
144+
brokername: str,
145+
loglevel: str | None = None,
146+
147+
**pikerd_kwargs,
148+
149+
) -> tractor.Portal:
150+
'''
151+
Helper to spawn a brokerd service *from* a client
152+
who wishes to use the sub-actor-daemon.
153+
154+
'''
155+
from piker.service import maybe_spawn_daemon
156+
157+
async with maybe_spawn_daemon(
158+
159+
f'brokerd.{brokername}',
160+
service_task_target=spawn_brokerd,
161+
spawn_args={
162+
'brokername': brokername,
163+
},
164+
loglevel=loglevel,
165+
166+
**pikerd_kwargs,
167+
168+
) as portal:
169+
yield portal

piker/brokers/_util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
)
3232
subsys: str = 'piker.brokers'
3333

34+
# NOTE: level should be reset by any actor that is spawned
3435
log = get_logger(subsys)
3536

3637
get_console_log = partial(

piker/data/__init__.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,3 @@
5050
'open_shm_array',
5151
'get_shm_token',
5252
]
53-
54-
55-
@tractor.context
56-
async def _setup_persistent_brokerd(
57-
ctx: tractor.Context,
58-
brokername: str,
59-
loglevel: str | None = None,
60-
61-
) -> None:
62-
'''
63-
Allocate a actor-wide service nursery in ``brokerd``
64-
such that feeds can be run in the background persistently by
65-
the broker backend as needed.
66-
67-
'''
68-
get_console_log(
69-
loglevel or tractor.current_actor().loglevel,
70-
)
71-
72-
from .feed import (
73-
_bus,
74-
get_feed_bus,
75-
)
76-
global _bus
77-
assert not _bus
78-
79-
async with trio.open_nursery() as service_nursery:
80-
# assign a nursery to the feeds bus for spawning
81-
# background tasks from clients
82-
get_feed_bus(brokername, service_nursery)
83-
84-
# unblock caller
85-
await ctx.started()
86-
87-
# we pin this task to keep the feeds manager active until the
88-
# parent actor decides to tear it down
89-
await trio.sleep_forever()

piker/service/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"""
2121
from __future__ import annotations
2222

23-
from ._util import log
2423
from ._mngr import Services
2524
from ._registry import ( # noqa
2625
_tractor_kwargs,
@@ -33,8 +32,6 @@
3332
)
3433
from ._daemon import ( # noqa
3534
maybe_spawn_daemon,
36-
spawn_brokerd,
37-
maybe_spawn_brokerd,
3835
spawn_emsd,
3936
maybe_open_emsd,
4037
)
@@ -44,6 +41,10 @@
4441
open_pikerd,
4542
get_tractor_runtime_kwargs,
4643
)
44+
from ..brokers._daemon import (
45+
spawn_brokerd,
46+
maybe_spawn_brokerd,
47+
)
4748

4849

4950
__all__ = [

piker/service/_actor_runtime.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,11 @@ async def open_piker_runtime(
133133
_root_modules = [
134134
__name__,
135135
'piker.service._daemon',
136+
'piker.brokers._daemon',
137+
136138
'piker.clearing._ems',
137139
'piker.clearing._client',
140+
138141
'piker.data._sampling',
139142
]
140143

piker/service/_daemon.py

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,12 @@
3232
from ._util import (
3333
log, # sub-sys logger
3434
)
35-
from ..brokers import get_brokermod
3635
from ._mngr import (
3736
Services,
3837
)
3938
from ._actor_runtime import maybe_open_pikerd
4039
from ._registry import find_service
4140

42-
# `brokerd` enabled modules
43-
# TODO: move this def to the `.data` subpkg..
44-
# NOTE: keeping this list as small as possible is part of our caps-sec
45-
# model and should be treated with utmost care!
46-
_data_mods = [
47-
'piker.brokers.core',
48-
'piker.brokers.data',
49-
'piker.data',
50-
'piker.data.feed',
51-
'piker.data._sampling'
52-
]
53-
5441

5542
@acm
5643
async def maybe_spawn_daemon(
@@ -145,87 +132,6 @@ async def maybe_spawn_daemon(
145132
await portal.cancel_actor()
146133

147134

148-
async def spawn_brokerd(
149-
150-
brokername: str,
151-
loglevel: str | None = None,
152-
153-
**tractor_kwargs,
154-
155-
) -> bool:
156-
157-
log.info(f'Spawning {brokername} broker daemon')
158-
159-
brokermod = get_brokermod(brokername)
160-
dname = f'brokerd.{brokername}'
161-
162-
extra_tractor_kwargs = getattr(brokermod, '_spawn_kwargs', {})
163-
tractor_kwargs.update(extra_tractor_kwargs)
164-
165-
# ask `pikerd` to spawn a new sub-actor and manage it under its
166-
# actor nursery
167-
modpath = brokermod.__name__
168-
broker_enable = [modpath]
169-
for submodname in getattr(
170-
brokermod,
171-
'__enable_modules__',
172-
[],
173-
):
174-
subpath = f'{modpath}.{submodname}'
175-
broker_enable.append(subpath)
176-
177-
portal = await Services.actor_n.start_actor(
178-
dname,
179-
enable_modules=_data_mods + broker_enable,
180-
loglevel=loglevel,
181-
debug_mode=Services.debug_mode,
182-
**tractor_kwargs
183-
)
184-
185-
# non-blocking setup of brokerd service nursery
186-
from ..data import _setup_persistent_brokerd
187-
188-
await Services.start_service_task(
189-
dname,
190-
portal,
191-
192-
# signature of target root-task endpoint
193-
_setup_persistent_brokerd,
194-
brokername=brokername,
195-
loglevel=loglevel,
196-
)
197-
return True
198-
199-
200-
@acm
201-
async def maybe_spawn_brokerd(
202-
203-
brokername: str,
204-
loglevel: str | None = None,
205-
206-
**pikerd_kwargs,
207-
208-
) -> tractor.Portal:
209-
'''
210-
Helper to spawn a brokerd service *from* a client
211-
who wishes to use the sub-actor-daemon.
212-
213-
'''
214-
async with maybe_spawn_daemon(
215-
216-
f'brokerd.{brokername}',
217-
service_task_target=spawn_brokerd,
218-
spawn_args={
219-
'brokername': brokername,
220-
},
221-
loglevel=loglevel,
222-
223-
**pikerd_kwargs,
224-
225-
) as portal:
226-
yield portal
227-
228-
229135
async def spawn_emsd(
230136

231137
loglevel: str | None = None,

piker/service/elastic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import docker
2727
from ._ahab import DockerContainer
2828

29-
from . import log # sub-sys logger
29+
from ._util import log # sub-sys logger
3030
from ._util import (
3131
get_console_log,
3232
)

0 commit comments

Comments
 (0)