Skip to content
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

Add Hub Mode for inspector #2881

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sanic/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,14 @@ def _inspector(self):
action = kwargs.pop("action") or "info"
api_key = kwargs.pop("api_key")
positional = kwargs.pop("positional", None)
as_json = kwargs.pop("json", False)
if action == "<custom>" and positional:
action = positional[0]
if len(positional) > 1:
kwargs["args"] = positional[1:]
InspectorClient(host, port, secure, raw, api_key).do(action, **kwargs)
InspectorClient(host, port, secure, raw, api_key).do(
action, as_json=as_json, **kwargs
)

def _repl(self, app: Sanic):
if is_atty():
Expand Down
10 changes: 10 additions & 0 deletions sanic/cli/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ def make_inspector_parser(parser: ArgumentParser) -> None:
title=" Subcommands",
parser_class=InspectorSubParser,
)
info = subparsers.add_parser(
"info",
help="Display information about the application instance",
formatter_class=SanicHelpFormatter,
)
info.add_argument(
"--json",
action="store_true",
help="Output the information in JSON format",
)
reloader = subparsers.add_parser(
"reload",
help="Trigger a reload of the server workers",
Expand Down
51 changes: 45 additions & 6 deletions sanic/cli/inspector_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def __init__(
self.scheme = scheme
self.host = self.host[len(full) :] # noqa E203

def do(self, action: str, **kwargs: Any) -> None:
def do(self, action: str, *, as_json: bool = False, **kwargs: Any) -> None:
if action == "info":
self.info()
self.info(as_json)
return
result = self.request(action, **kwargs).get("result")
if result:
Expand All @@ -54,21 +54,60 @@ def do(self, action: str, **kwargs: Any) -> None:
)
sys.stdout.write(out + "\n")

def info(self) -> None:
out = sys.stdout.write
def info(self, as_json: bool = False) -> None:
response = self.request("", "GET")
if self.raw or not response:
return
data = response["result"]
display = data.pop("info")
nodes = data.pop("nodes", {})
if as_json:
data = {
"info": display,
**data,
}
if nodes:
data["nodes"] = {
node: _data["info"] for node, _data in nodes.items()
}
data["workers"] = {
f"{name} (Hub)": {"node": "Hub", **info}
for name, info in data["workers"].items()
} | {
f"{name} ({node})": {"node": node, **info}
for node, _data in nodes.items()
for name, info in _data["workers"].items()
}
sys.stdout.write(dumps(data) + "\n")

return
self._display_info(display)
self._display_workers(data["workers"], None if not nodes else "Hub")
if nodes:
for name, node in nodes.items():
# info = node.pop("info")
workers = node.pop("workers")
# self._display_info(info)
self._display_workers(workers, name)

def _display_info(self, display: Dict[str, Any]) -> None:
extra = display.pop("extra", {})
out = sys.stdout.write
display["packages"] = ", ".join(display["packages"])
MOTDTTY(get_logo(), self.base_url, display, extra).display(
version=False,
action="Inspecting",
out=out,
)
for name, info in data["workers"].items():

def _display_workers(
self, workers: Dict[str, Dict[str, Any]], node: Optional[str] = None
) -> None:
out = sys.stdout.write
for name, info in workers.items():
name = f"{Colors.BOLD}{Colors.SANIC}{name}{Colors.END}"
if node:
name += f" {Colors.BOLD}{Colors.YELLOW}({node}){Colors.END}"
info = "\n".join(
f"\t{key}: {Colors.BLUE}{value}{Colors.END}"
for key, value in info.items()
Expand All @@ -78,7 +117,7 @@ def info(self) -> None:
+ indent(
"\n".join(
[
f"{Colors.BOLD}{Colors.SANIC}{name}{Colors.END}",
name,
info,
]
),
Expand Down
6 changes: 6 additions & 0 deletions sanic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
"INSPECTOR_TLS_KEY": _default,
"INSPECTOR_TLS_CERT": _default,
"INSPECTOR_API_KEY": "",
"INSPECTOR_HUB_MODE": _default,
"INSPECTOR_HUB_HOST": "",
"INSPECTOR_HUB_PORT": 0,
"KEEP_ALIVE_TIMEOUT": 120,
"KEEP_ALIVE": True,
"LOCAL_CERT_CREATOR": LocalCertCreator.AUTO,
Expand Down Expand Up @@ -112,6 +115,9 @@ class Config(dict, metaclass=DescriptorMeta):
INSPECTOR_TLS_KEY: Union[Path, str, Default]
INSPECTOR_TLS_CERT: Union[Path, str, Default]
INSPECTOR_API_KEY: str
INSPECTOR_HUB_MODE: Union[bool, Default]
INSPECTOR_HUB_HOST: str
INSPECTOR_HUB_PORT: int
KEEP_ALIVE_TIMEOUT: int
KEEP_ALIVE: bool
LOCAL_CERT_CREATOR: Union[str, LocalCertCreator]
Expand Down
6 changes: 5 additions & 1 deletion sanic/mixins/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,8 +1137,12 @@ def serve(
primary.config.INSPECTOR_API_KEY,
primary.config.INSPECTOR_TLS_KEY,
primary.config.INSPECTOR_TLS_CERT,
primary.config.INSPECTOR_HUB_MODE,
primary.config.INSPECTOR_HUB_HOST,
primary.config.INSPECTOR_HUB_PORT,
)
manager.manage("Inspector", inspector, {}, transient=False)
# TODO: Change back to false
manager.manage("Inspector", inspector, {}, transient=True)

primary._inspector = inspector
primary._manager = manager
Expand Down
Loading
Loading