-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ahab's dick #284
Merged
Merged
Ahab's dick #284
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0028391
Basic module-script for spawning `marketstore`, needs correct bind mo…
goodboy d18d0d7
Extract non-sudo user for config dir path
goodboy 394218d
Add a super simple `marketstore` container supervisor
goodboy aa286ba
Drop import, it's got madness with and SIGINT?
goodboy d319a63
Drop old client instantiate line
goodboy 6a212df
Drop dependence on `msgpack` and `msgpack_numpy`
goodboy 78e5394
Type annot updates
goodboy a90d205
Only throttle warn on rate >= display rate
goodboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
# piker: trading gear for hackers | ||
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of piker0) | ||
|
||
# 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 | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
|
||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
|
||
# 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/>. | ||
|
||
''' | ||
Supervisor for docker with included specific-image service helpers. | ||
|
||
''' | ||
from typing import ( | ||
Optional, | ||
# Any, | ||
) | ||
from contextlib import asynccontextmanager as acm | ||
# import time | ||
|
||
import trio | ||
import tractor | ||
import docker | ||
import json | ||
from docker.models.containers import Container | ||
|
||
from ..log import get_logger # , get_console_log | ||
from ..config import _config_dir | ||
|
||
log = get_logger(__name__) | ||
|
||
|
||
_config = ''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the config we need to talk about. |
||
# piker's ``marketstore`` config. | ||
|
||
# mount this config using: | ||
# sudo docker run --mount \ | ||
# type=bind,source="$HOME/.config/piker/",target="/etc" -i -p \ | ||
# 5993:5993 alpacamarkets/marketstore:latest | ||
|
||
root_directory: data | ||
listen_port: 5993 | ||
grpc_listen_port: 5995 | ||
log_level: info | ||
queryable: true | ||
stop_grace_period: 0 | ||
wal_rotate_interval: 5 | ||
stale_threshold: 5 | ||
enable_add: true | ||
enable_remove: false | ||
|
||
triggers: | ||
- module: ondiskagg.so | ||
on: "*/1Sec/OHLCV" | ||
config: | ||
# filter: "nasdaq" | ||
destinations: | ||
- 1Min | ||
- 5Min | ||
- 15Min | ||
- 1H | ||
- 1D | ||
|
||
- module: stream.so | ||
on: '*/*/*' | ||
# config: | ||
# filter: "nasdaq" | ||
|
||
''' | ||
|
||
|
||
@acm | ||
async def open_docker( | ||
url: Optional[str] = None, | ||
**kwargs, | ||
|
||
) -> docker.DockerClient: | ||
|
||
client = docker.DockerClient( | ||
base_url=url, | ||
**kwargs | ||
) if url else docker.from_env(**kwargs) | ||
|
||
try: | ||
yield client | ||
finally: | ||
# for c in client.containers.list(): | ||
# c.kill() | ||
client.close() | ||
# client.api._custom_adapter.close() | ||
|
||
|
||
# async def waitfor( | ||
# cntr: Container, | ||
# attr_path: tuple[str], | ||
# expect=None, | ||
# timeout: float = 0.5, | ||
|
||
# ) -> Any: | ||
# ''' | ||
# Wait for a container's attr value to be set. If ``expect`` is | ||
# provided wait for the value to be set to that value. | ||
|
||
# This is an async version of the helper from our ``pytest-dockerctl`` | ||
# plugin. | ||
|
||
# ''' | ||
# def get(val, path): | ||
# for key in path: | ||
# val = val[key] | ||
# return val | ||
|
||
# start = time.time() | ||
# while time.time() - start < timeout: | ||
# cntr.reload() | ||
# val = get(cntr.attrs, attr_path) | ||
# if expect is None and val: | ||
# return val | ||
# elif val == expect: | ||
# return val | ||
# else: | ||
# raise TimeoutError("{} failed to be {}, value: \"{}\"".format( | ||
# attr_path, expect if expect else 'not None', val)) | ||
|
||
|
||
@tractor.context | ||
async def open_marketstore_container( | ||
ctx: tractor.Context, | ||
**kwargs, | ||
|
||
) -> None: | ||
''' | ||
Start and supervise a marketstore instance with its config bind-mounted | ||
in from the piker config directory on the system. | ||
|
||
The equivalent cli cmd to this code is: | ||
|
||
sudo docker run --mount \ | ||
type=bind,source="$HOME/.config/piker/",target="/etc" -i -p \ | ||
5993:5993 alpacamarkets/marketstore:latest | ||
|
||
''' | ||
# log = get_console_log('info', name=__name__) | ||
|
||
async with open_docker() as client: | ||
# create a mount from user's local piker config dir into container | ||
config_dir_mnt = docker.types.Mount( | ||
target='/etc', | ||
source=_config_dir, | ||
type='bind', | ||
) | ||
|
||
cntr: Container = client.containers.run( | ||
'alpacamarkets/marketstore:latest', | ||
# do we need this for cmds? | ||
# '-i', | ||
|
||
# '-p 5993:5993', | ||
ports={'5993/tcp': 5993}, | ||
mounts=[config_dir_mnt], | ||
detach=True, | ||
# stop_signal='SIGINT', | ||
# init=True, | ||
# remove=True, | ||
) | ||
try: | ||
started: bool = False | ||
logs = cntr.logs(stream=True) | ||
|
||
with trio.move_on_after(0.5): | ||
for entry in logs: | ||
entry = entry.decode() | ||
try: | ||
record = json.loads(entry.strip()) | ||
except json.JSONDecodeError: | ||
if 'Error' in entry: | ||
raise RuntimeError(entry) | ||
msg = record['msg'] | ||
|
||
if "launching tcp listener for all services..." in msg: | ||
started = True | ||
break | ||
|
||
await trio.sleep(0) | ||
|
||
if not started and cntr not in client.containers.list(): | ||
raise RuntimeError( | ||
'Failed to start `marketstore` check logs output for deats' | ||
) | ||
|
||
await ctx.started() | ||
await trio.sleep_forever() | ||
|
||
finally: | ||
cntr.stop() | ||
|
||
|
||
async def main(): | ||
|
||
async with tractor.open_nursery( | ||
loglevel='runtime', | ||
) as tn: | ||
async with ( | ||
( | ||
await tn.start_actor('ahab', enable_modules=[__name__]) | ||
).open_context( | ||
open_marketstore_container | ||
) as (ctx, first), | ||
): | ||
assert not first | ||
await trio.sleep_forever() | ||
|
||
|
||
if __name__ == '__main__': | ||
trio.run(main) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Literally had to add this because use of
click
somehow causesSIGINT
to be masked.See goodboy/tractor#302