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 chia services group #19044

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
28 changes: 14 additions & 14 deletions chia/_tests/core/services/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,38 +84,38 @@ async def test_daemon_terminates(signal_number: signal.Signals, chia_root: ChiaR

@pytest.mark.parametrize(argnames="signal_number", argvalues=sendable_termination_signals)
@pytest.mark.parametrize(
argnames=["create_service", "module_path", "service_config_name"],
argnames=["create_service", "module_args", "service_config_name"],
argvalues=[
[DataLayerRpcClient.create_as_context, "chia.server.start_data_layer", "data_layer"],
[FarmerRpcClient.create_as_context, "chia.server.start_farmer", "farmer"],
[FullNodeRpcClient.create_as_context, "chia.server.start_full_node", "full_node"],
[HarvesterRpcClient.create_as_context, "chia.server.start_harvester", "harvester"],
[WalletRpcClient.create_as_context, "chia.server.start_wallet", "wallet"],
[None, "chia.server.start_introducer", "introducer"],
[DataLayerRpcClient.create_as_context, ["chia.server.start_data_layer"], "data_layer"],
[FarmerRpcClient.create_as_context, ["chia.server.start_farmer"], "farmer"],
[FullNodeRpcClient.create_as_context, ["chia", "services", "full-node"], "full_node"],
[HarvesterRpcClient.create_as_context, ["chia.server.start_harvester"], "harvester"],
[WalletRpcClient.create_as_context, ["chia.server.start_wallet"], "wallet"],
[None, ["chia.server.start_introducer"], "introducer"],
# TODO: fails... make it not do that
# [None, "chia.seeder.start_crawler", "crawler"],
[None, "chia.server.start_timelord", "timelord"],
# [None, ["chia.seeder.start_crawler"], "crawler"],
[None, ["chia.server.start_timelord"], "timelord"],
pytest.param(
None,
"chia.timelord.timelord_launcher",
["chia.timelord.timelord_launcher"],
"timelord_launcher",
marks=pytest.mark.skipif(
sys.platform in {"win32", "cygwin"},
reason="windows is not supported by the timelord launcher",
),
),
# TODO: fails... starts creating plots etc
# [None, "chia.simulator.start_simulator", "simulator"],
# [None, ["chia.simulator.start_simulator"], "simulator"],
# TODO: fails... make it not do that
# [None, "chia.data_layer.data_layer_server", "data_layer"],
# [None, ["chia.data_layer.data_layer_server"], "data_layer"],
],
)
@pytest.mark.anyio
async def test_services_terminate(
signal_number: signal.Signals,
chia_root: ChiaRoot,
create_service: Optional[CreateServiceProtocol],
module_path: str,
module_args: list[str],
service_config_name: str,
) -> None:
with lock_and_load_config(root_path=chia_root.path, filename="config.yaml") as config:
Expand Down Expand Up @@ -145,7 +145,7 @@ async def test_services_terminate(
process = exit_stack.enter_context(
closing_chia_root_popen(
chia_root=chia_root,
args=[sys.executable, "-m", module_path],
args=[sys.executable, "-m", *module_args],
)
)

Expand Down
2 changes: 2 additions & 0 deletions chia/cmds/chia.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from chia.cmds.plots import plots_cmd
from chia.cmds.plotters import plotters_cmd
from chia.cmds.rpc import rpc_cmd
from chia.cmds.services import services_group
from chia.cmds.show import show_cmd
from chia.cmds.start import start_cmd
from chia.cmds.stop import stop_cmd
Expand Down Expand Up @@ -140,6 +141,7 @@ def run_daemon_cmd(ctx: click.Context, wait_for_unlock: bool) -> None:
cli.add_command(completion)
cli.add_command(dao_cmd)
cli.add_command(dev_cmd)
cli.add_command(services_group)


def main() -> None:
Expand Down
28 changes: 28 additions & 0 deletions chia/cmds/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import annotations

import click


@click.group(
name="services",
short_help="directly run Chia services",
help="""Directly run Chia services without launching them through the daemon. This
can be useful sometimes during development for launching with a debugger, or
when you want to use systemd or similar to manage the service processes.
""",
)
def services_group() -> None:
pass # pragma: no cover


@services_group.command("full-node", add_help_option=False, context_settings={"ignore_unknown_options": True})
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
@click.pass_context
def full_node_command(ctx: click.Context, args: list[str]) -> None:
import sys

from chia.server.start_full_node import main

# hack since main() uses load_config_cli() which uses argparse
sys.argv = [ctx.command_path, *args]
main(root_path=ctx.obj["root_path"])
8 changes: 3 additions & 5 deletions chia/server/start_full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from chia.types.aliases import FullNodeService
from chia.util.chia_logging import initialize_service_logging
from chia.util.config import get_unresolved_peer_infos, load_config, load_config_cli
from chia.util.default_root import resolve_root_path
from chia.util.ints import uint16
from chia.util.task_timing import maybe_manage_task_instrumentation

Expand Down Expand Up @@ -90,14 +89,13 @@ async def async_main(service_config: dict[str, Any], root_path: pathlib.Path) ->
return 0


def main() -> int:
def main(root_path: pathlib.Path, args: Optional[list[str]] = None) -> int:
freeze_support()
root_path = resolve_root_path(override=None)

with maybe_manage_task_instrumentation(
enable=os.environ.get(f"CHIA_INSTRUMENT_{SERVICE_NAME.upper()}") is not None
):
service_config = load_config_cli(root_path, "config.yaml", SERVICE_NAME)
service_config = load_config_cli(root_path, "config.yaml", SERVICE_NAME, args=args)
target_peer_count = service_config.get("target_peer_count", 40) - service_config.get(
"target_outbound_peer_count", 8
)
Expand All @@ -109,4 +107,4 @@ def main() -> int:


if __name__ == "__main__":
sys.exit(main())
sys.exit("module no longer directly callable, see the new command group `chia services`")
3 changes: 2 additions & 1 deletion chia/util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def load_config_cli(
filename: str,
sub_config: Optional[str] = None,
fill_missing_services: bool = False,
args: Optional[list[str]] = None,
) -> dict[str, Any]:
"""
Loads configuration from the specified filename, in the config directory,
Expand All @@ -176,7 +177,7 @@ def load_config_cli(
prop_type: Callable = str2bool if type(value) is bool else type(value) # type: ignore
parser.add_argument(f"--{prop_name}", type=prop_type, dest=prop_name)

for key, value in vars(parser.parse_args()).items():
for key, value in vars(parser.parse_args(args=args)).items():
if value is not None:
flattened_props[key] = value

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ packages = [{ include = "chia"}, { include = "mozilla-ca/cacert.pem" }]
chia = "chia.cmds.chia:main"
chia_daemon = "chia.daemon.server:main"
chia_wallet = "chia.server.start_wallet:main"
chia_full_node = "chia.server.start_full_node:main"
chia_full_node = "chia.cmds.services:full_node_command"
chia_harvester = "chia.server.start_harvester:main"
chia_farmer = "chia.server.start_farmer:main"
chia_introducer = "chia.server.start_introducer:main"
Expand Down
Loading