Skip to content
Merged
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
2 changes: 0 additions & 2 deletions das-cli/src/commands/atomdb_broker/atomdb_broker_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ def __init__(self):
self._settings,
),
]

self._settings = Settings(store=JsonConfigStore(os.path.expanduser(SECRETS_PATH)))
3 changes: 3 additions & 0 deletions das-cli/src/commands/command_router/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .command_router_module import CommandRouterModule

__all__ = ["CommandRouterModule"]
248 changes: 248 additions & 0 deletions das-cli/src/commands/command_router/command_router_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
from injector import inject

from common import (
Command,
CommandGroup,
CommandOption,
Settings,
StdoutSeverity,
StdoutType,
)
from common.container_manager.busnode_container_manager import (
BusNodeContainerManager,
)
from common.decorators import ensure_container_running
from common.docker.exceptions import (
DockerContainerDuplicateError,
DockerContainerNotFoundError,
DockerError,
)
from common.factory.atomdb.atomdb_backend import AtomdbBackend
from common.prompt_types import PortRangeType

from .command_router_docs import (
HELP_COMMAND_ROUTER,
HELP_RESTART,
HELP_START,
HELP_STOP,
SHORT_HELP_COMMAND_ROUTER,
SHORT_HELP_RESTART,
SHORT_HELP_START,
SHORT_HELP_STOP,
)
from .command_router_service_response import CommandRouterServiceResponse


class CommandRouterStart(Command):
name = "start"

short_help = SHORT_HELP_START
help = HELP_START

params = [
CommandOption(
["--port-range"],
help="The lower and upper bounds of the port range to be used by the node.",
default="48000:48999",
type=PortRangeType(),
),
]

@inject
def __init__(
self,
command_router_container_manager: BusNodeContainerManager,
atomdb_backend: AtomdbBackend,
settings: Settings,
):
self._settings = settings
self._atomdb_backend = atomdb_backend
self._command_router_container_manager = command_router_container_manager
super().__init__()

def _get_container(self):
return self._command_router_container_manager.get_container()

def _start_container(self, port_range):
container = self._get_container()
port = container.port

self.stdout("Starting Command Router service...")

try:
self._command_router_container_manager.start_container(ports_range=port_range)

message = f"Command Router started on port {port}"

self.stdout(message, severity=StdoutSeverity.SUCCESS)

self.stdout(
dict(
CommandRouterServiceResponse(
action="start",
status="success",
message=message,
container=self._get_container(),
)
),
stdout_type=StdoutType.MACHINE_READABLE,
)

except DockerContainerDuplicateError:
message = f"Command Router is already running. " f"It's listening on port {port}"

self.stdout(message, severity=StdoutSeverity.WARNING)

self.stdout(
dict(
CommandRouterServiceResponse(
action="start",
status="already_running",
message=message,
container=self._get_container(),
)
),
stdout_type=StdoutType.MACHINE_READABLE,
)

except DockerError as e:
raise DockerError(
f"Error occurred while trying to start "
f"Command Router on port {port}\n"
f"Original error: {e}"
)

@ensure_container_running(
["_atomdb_backend"],
exception_text="\nPlease start the required services before running "
"'command-router start'.\n"
"Run 'db start' to start the databases",
verbose=False,
)
def run(self, port_range):
self._settings.validate_configuration_file()
self._start_container(port_range)


class CommandRouterStop(Command):
name = "stop"

short_help = SHORT_HELP_STOP
help = HELP_STOP

@inject
def __init__(
self,
command_router_container_manager: BusNodeContainerManager,
settings: Settings,
):
self._settings = settings
self._command_router_container_manager = command_router_container_manager
super().__init__()

def _get_container(self):
return self._command_router_container_manager.get_container()

def _stop_container(self):
container = self._get_container()

self.stdout("Stopping Command Router service...")

try:
self._command_router_container_manager.stop()

message = "Command Router service stopped"

self.stdout(
message,
severity=StdoutSeverity.SUCCESS,
)

self.stdout(
dict(
CommandRouterServiceResponse(
action="stop",
status="success",
message=message,
container=container,
)
),
stdout_type=StdoutType.MACHINE_READABLE,
)

except DockerContainerNotFoundError:
container_name = self._get_container().name

message = f"The Command Router service named " f"{container_name} is already stopped."

self.stdout(
message,
severity=StdoutSeverity.WARNING,
)

self.stdout(
dict(
CommandRouterServiceResponse(
action="stop",
status="already_stopped",
message=message,
container=self._get_container(),
)
),
stdout_type=StdoutType.MACHINE_READABLE,
)

def run(self):
self._settings.validate_configuration_file()
self._stop_container()


class CommandRouterRestart(Command):
name = "restart"
short_help = SHORT_HELP_RESTART
help = HELP_RESTART

params = [
CommandOption(
["--port-range"],
help="The lower and upper bounds of the port range to be used by the node.",
default="48000:48999",
type=PortRangeType(),
)
]

@inject
def __init__(
self, command_router_start: CommandRouterStart, command_router_stop: CommandRouterStop
):
self.command_router_start = command_router_start
self.command_router_stop = command_router_stop
super().__init__()

def run(self, port_range):
self.command_router_stop.run()
self.command_router_start.run(port_range=port_range)


class CommandRouterCli(CommandGroup):
name = "command-router"

short_help = SHORT_HELP_COMMAND_ROUTER
help = HELP_COMMAND_ROUTER

@inject
def __init__(
self,
command_router_start: CommandRouterStart,
command_router_stop: CommandRouterStop,
command_router_restart: CommandRouterRestart,
):
super().__init__()

self.add_commands(
[
command_router_start,
command_router_stop,
command_router_restart,
]
)
112 changes: 112 additions & 0 deletions das-cli/src/commands/command_router/command_router_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
SHORT_HELP_START = "Starts the Command Router service"

HELP_START = '''
NAME

das-cli command-router start - Start the command router service

SYNOPSIS

das-cli command-router start [--port-range <start:end>]

DESCRIPTION

Starts the command router service in a Docker container. If the service is already running,
a warning will be shown.

The service begins listening on the configured port.

EXAMPLES

Start the command router service:

$ das-cli command-router start --port-range 47000:47999
'''
HELP_STOP = '''
NAME

das-cli command-router stop - Stop the Command Router service

SYNOPSIS

das-cli command-router stop

DESCRIPTION

Stops the currently running Command Router container. This halts the processing of messages
and deactivates the service until it is explicitly started again.

If the service is already stopped, a warning message is displayed.

EXAMPLES

Stops the command router service:

$ das-cli command-router stop
'''

SHORT_HELP_STOP = "Starts the command router agent"

HELP_RESTART = """
NAME

das-cli command-router restart - Restart the Command Router service

SYNOPSIS

das-cli command-router restart

DESCRIPTION

This command combines a stop and a start operation to ensure that the
Command Router is restarted cleanly.

Useful for refreshing configurations or recovering from faults.

EXAMPLES

Restart the Command Router service:

$ das-cli command-router restart
"""

SHORT_HELP_RESTART = "Restart the Command Router service."

HELP_COMMAND_ROUTER = """
NAME

das-cli command-router - Manage the Command Router service

SYNOPSIS

das-cli command-router [COMMAND]

DESCRIPTION

This command group allows you to manage the lifecycle of the Command Router service,

COMMANDS
start
Start the Command Router service and begin message processing.

stop
Stop the currently running Command Router container.

restart
Restart the Command Router container (stop followed by start).

EXAMPLES
Start the broker:

$ das-cli command-router start

Stop the broker:

$ das-cli command-router stop

Restart the broker:

$ das-cli command-router restart
"""

SHORT_HELP_COMMAND_ROUTER = "Control the lifecycle of the Command Router service."
Loading
Loading