Skip to content

feat: カーネル接続の確認機能を追加し、タイムアウト設定を導入 #100

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

Merged
merged 1 commit into from
Apr 15, 2025
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
32 changes: 29 additions & 3 deletions adf_core_python/core/launcher/agent_launcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
import socket
import threading
from typing import Optional

Expand Down Expand Up @@ -59,13 +60,27 @@ def init_connector(self) -> None:
def launch(self) -> None:
kernel_host: str = self.config.get_value(ConfigKey.KEY_KERNEL_HOST, "localhost")
kernel_port: int = self.config.get_value(ConfigKey.KEY_KERNEL_PORT, 27931)
self.logger.info(
f"Start agent launcher (host: {kernel_host}, port: {kernel_port})"
)

component_launcher: ComponentLauncher = ComponentLauncher(
kernel_host, kernel_port, self.logger
)
timeout: int = self.config.get_value(
ConfigKey.KEY_KERNEL_TIMEOUT,
30,
)
if component_launcher.check_kernel_connection(timeout=timeout):
self.logger.info(
f"Kernel is running (host: {kernel_host}, port: {kernel_port})"
)
else:
self.logger.error(
f"Kernel is not running (host: {kernel_host}, port: {kernel_port})"
)
return

self.logger.info(
f"Start agent launcher (host: {kernel_host}, port: {kernel_port})"
)

gateway_launcher: Optional[GatewayLauncher] = None
gateway_flag: bool = self.config.get_value(ConfigKey.KEY_GATEWAY_FLAG, False)
Expand Down Expand Up @@ -104,3 +119,14 @@ def connect() -> None:

for thread in self.agent_thread_list:
thread.join()

def check_kernel_connection(self, host: str, port: int, timeout: int = 5) -> bool:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
result = sock.connect_ex((host, port))
sock.close()
return result == 0
except Exception as e:
self.logger.error(f"カーネルへの接続確認中にエラーが発生しました: {e}")
return False
1 change: 1 addition & 0 deletions adf_core_python/core/launcher/config_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ConfigKey:
KEY_LOADER_CLASS: Final[str] = "adf_core_python.launcher.loader"
KEY_KERNEL_HOST: Final[str] = "kernel.host"
KEY_KERNEL_PORT: Final[str] = "kernel.port"
KEY_KERNEL_TIMEOUT: Final[str] = "kernel.timeout"
KEY_TEAM_NAME: Final[str] = "team.name"
KEY_DEBUG_FLAG: Final[str] = "adf.debug.flag"
KEY_DEVELOP_FLAG: Final[str] = "adf.develop.flag"
Expand Down
46 changes: 46 additions & 0 deletions adf_core_python/core/launcher/connect/component_launcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import socket
import time

from structlog import BoundLogger

Expand Down Expand Up @@ -53,3 +54,48 @@ def connect(self, agent: Agent, _request_id: int) -> None:
def generate_request_id(self) -> int:
self.request_id += 1
return self.request_id

def check_kernel_connection(
self, timeout: int = 30, retry_interval: float = 5.0
) -> bool:
"""Attempts to connect to the kernel multiple times within the specified timeout period.

Args:
timeout (int): Total timeout duration in seconds
retry_interval (float): Interval between retry attempts in seconds

Returns:
bool: True if connection successful, False otherwise
"""
start_time = time.time()
attempt = 1

while True:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(retry_interval)
result = sock.connect_ex((self.host, self.port))
sock.close()

if result == 0:
self.logger.info(
f"Successfully connected to kernel (attempt: {attempt})"
)
return True

elapsed_time = time.time() - start_time
if elapsed_time >= timeout:
self.logger.error(
f"Timeout: Could not connect to kernel within {timeout} seconds (attempts: {attempt})"
)
return False

self.logger.debug(
f"Connection attempt {attempt} failed - retrying in {retry_interval} seconds"
)
time.sleep(retry_interval)
attempt += 1

except Exception as e:
self.logger.error(f"Error while checking kernel connection: {e}")
return False
15 changes: 11 additions & 4 deletions adf_core_python/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ def __init__(
self,
launcher_config_file: str,
) -> None:
resource.setrlimit(resource.RLIMIT_NOFILE, (8192, 9223372036854775807))
resource.setrlimit(resource.RLIMIT_NOFILE, (8192, 1048576))

configure_logger()

self.logger = get_logger(__name__)
self.launcher_config = Config(launcher_config_file)

parser = argparse.ArgumentParser(description="Agent Launcher")
Expand Down Expand Up @@ -80,6 +77,12 @@ def __init__(
action="store_true",
help="precompute flag",
)
parser.add_argument(
"--timeout",
type=int,
help="timeout in seconds",
metavar="",
)
parser.add_argument("--debug", action="store_true", help="debug flag")
parser.add_argument(
"--java",
Expand All @@ -98,6 +101,7 @@ def __init__(
ConfigKey.KEY_FIRE_STATION_COUNT: args.firestation,
ConfigKey.KEY_POLICE_OFFICE_COUNT: args.policeoffice,
ConfigKey.KEY_PRECOMPUTE: args.precompute,
ConfigKey.KEY_KERNEL_TIMEOUT: args.timeout,
ConfigKey.KEY_DEBUG_FLAG: args.debug,
ConfigKey.KEY_GATEWAY_FLAG: args.java,
}
Expand All @@ -106,6 +110,9 @@ def __init__(
if value is not None:
self.launcher_config.set_value(key, value)

configure_logger()
self.logger = get_logger(__name__)

self.logger.debug(f"launcher_config: {self.launcher_config}")

def launch(self) -> None:
Expand Down