|
1 | 1 | import importlib |
| 2 | +import socket |
2 | 3 | import threading |
3 | 4 | from typing import Optional |
4 | 5 |
|
@@ -59,13 +60,27 @@ def init_connector(self) -> None: |
59 | 60 | def launch(self) -> None: |
60 | 61 | kernel_host: str = self.config.get_value(ConfigKey.KEY_KERNEL_HOST, "localhost") |
61 | 62 | kernel_port: int = self.config.get_value(ConfigKey.KEY_KERNEL_PORT, 27931) |
62 | | - self.logger.info( |
63 | | - f"Start agent launcher (host: {kernel_host}, port: {kernel_port})" |
64 | | - ) |
65 | 63 |
|
66 | 64 | component_launcher: ComponentLauncher = ComponentLauncher( |
67 | 65 | kernel_host, kernel_port, self.logger |
68 | 66 | ) |
| 67 | + timeout: int = self.config.get_value( |
| 68 | + ConfigKey.KEY_KERNEL_TIMEOUT, |
| 69 | + 30, |
| 70 | + ) |
| 71 | + if component_launcher.check_kernel_connection(timeout=timeout): |
| 72 | + self.logger.info( |
| 73 | + f"Kernel is running (host: {kernel_host}, port: {kernel_port})" |
| 74 | + ) |
| 75 | + else: |
| 76 | + self.logger.error( |
| 77 | + f"Kernel is not running (host: {kernel_host}, port: {kernel_port})" |
| 78 | + ) |
| 79 | + return |
| 80 | + |
| 81 | + self.logger.info( |
| 82 | + f"Start agent launcher (host: {kernel_host}, port: {kernel_port})" |
| 83 | + ) |
69 | 84 |
|
70 | 85 | gateway_launcher: Optional[GatewayLauncher] = None |
71 | 86 | gateway_flag: bool = self.config.get_value(ConfigKey.KEY_GATEWAY_FLAG, False) |
@@ -104,3 +119,14 @@ def connect() -> None: |
104 | 119 |
|
105 | 120 | for thread in self.agent_thread_list: |
106 | 121 | thread.join() |
| 122 | + |
| 123 | + def check_kernel_connection(self, host: str, port: int, timeout: int = 5) -> bool: |
| 124 | + try: |
| 125 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 126 | + sock.settimeout(timeout) |
| 127 | + result = sock.connect_ex((host, port)) |
| 128 | + sock.close() |
| 129 | + return result == 0 |
| 130 | + except Exception as e: |
| 131 | + self.logger.error(f"カーネルへの接続確認中にエラーが発生しました: {e}") |
| 132 | + return False |
0 commit comments