mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-21 20:06:06 +00:00
feat: kill runtime process when exit in stdio mode
This commit is contained in:
@@ -156,6 +156,9 @@ class Application:
|
|||||||
self.logger.error(f'应用运行致命异常: {e}')
|
self.logger.error(f'应用运行致命异常: {e}')
|
||||||
self.logger.debug(f'Traceback: {traceback.format_exc()}')
|
self.logger.debug(f'Traceback: {traceback.format_exc()}')
|
||||||
|
|
||||||
|
def dispose(self):
|
||||||
|
self.plugin_connector.dispose()
|
||||||
|
|
||||||
async def print_web_access_info(self):
|
async def print_web_access_info(self):
|
||||||
"""打印访问 webui 的提示"""
|
"""打印访问 webui 的提示"""
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -52,7 +52,7 @@ async def main(loop: asyncio.AbstractEventLoop):
|
|||||||
|
|
||||||
def signal_handler(sig, frame):
|
def signal_handler(sig, frame):
|
||||||
print('[Signal] 程序退出.')
|
print('[Signal] 程序退出.')
|
||||||
# ap.shutdown()
|
app_inst.dispose()
|
||||||
os._exit(0)
|
os._exit(0)
|
||||||
|
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
|||||||
+10
-4
@@ -30,6 +30,8 @@ class PluginRuntimeConnector:
|
|||||||
|
|
||||||
stdio_client_controller: stdio_client_controller.StdioClientController
|
stdio_client_controller: stdio_client_controller.StdioClientController
|
||||||
|
|
||||||
|
ctrl: stdio_client_controller.StdioClientController | ws_client_controller.WebSocketClientController
|
||||||
|
|
||||||
runtime_disconnect_callback: typing.Callable[
|
runtime_disconnect_callback: typing.Callable[
|
||||||
[PluginRuntimeConnector], typing.Coroutine[typing.Any, typing.Any, None]
|
[PluginRuntimeConnector], typing.Coroutine[typing.Any, typing.Any, None]
|
||||||
]
|
]
|
||||||
@@ -73,22 +75,22 @@ class PluginRuntimeConnector:
|
|||||||
self.ap.logger.error('Failed to connect to plugin runtime, trying to reconnect...')
|
self.ap.logger.error('Failed to connect to plugin runtime, trying to reconnect...')
|
||||||
await self.runtime_disconnect_callback(self)
|
await self.runtime_disconnect_callback(self)
|
||||||
|
|
||||||
ctrl = ws_client_controller.WebSocketClientController(
|
self.ctrl = ws_client_controller.WebSocketClientController(
|
||||||
ws_url=ws_url,
|
ws_url=ws_url,
|
||||||
make_connection_failed_callback=make_connection_failed_callback,
|
make_connection_failed_callback=make_connection_failed_callback,
|
||||||
)
|
)
|
||||||
task = ctrl.run(new_connection_callback)
|
task = self.ctrl.run(new_connection_callback)
|
||||||
else: # stdio
|
else: # stdio
|
||||||
self.ap.logger.info('use stdio to connect to plugin runtime')
|
self.ap.logger.info('use stdio to connect to plugin runtime')
|
||||||
# cmd: lbp rt -s
|
# cmd: lbp rt -s
|
||||||
python_path = sys.executable
|
python_path = sys.executable
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
ctrl = stdio_client_controller.StdioClientController(
|
self.ctrl = stdio_client_controller.StdioClientController(
|
||||||
command=python_path,
|
command=python_path,
|
||||||
args=['-m', 'langbot_plugin.cli.__init__', 'rt', '-s'],
|
args=['-m', 'langbot_plugin.cli.__init__', 'rt', '-s'],
|
||||||
env=env,
|
env=env,
|
||||||
)
|
)
|
||||||
task = ctrl.run(new_connection_callback)
|
task = self.ctrl.run(new_connection_callback)
|
||||||
|
|
||||||
asyncio.create_task(task)
|
asyncio.create_task(task)
|
||||||
|
|
||||||
@@ -135,3 +137,7 @@ class PluginRuntimeConnector:
|
|||||||
cmd_ret = command_context.CommandReturn.model_validate(ret)
|
cmd_ret = command_context.CommandReturn.model_validate(ret)
|
||||||
|
|
||||||
yield cmd_ret
|
yield cmd_ret
|
||||||
|
|
||||||
|
def dispose(self):
|
||||||
|
if isinstance(self.ctrl, stdio_client_controller.StdioClientController):
|
||||||
|
self.ctrl.process.terminate()
|
||||||
|
|||||||
Reference in New Issue
Block a user