Skip to content

Commit 452602d

Browse files
committed
refactor(cli): Refactor client termination to ClientManager
1 parent c03c490 commit 452602d

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

src/vectorcode/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,10 @@ async def get_client(self, configs: Config) -> _ClientModel:
293293

294294
def get_processes(self) -> list[Process]:
295295
return [i.process for i in self.__clients.values() if i.process is not None]
296+
297+
async def kill_servers(self):
298+
termination_tasks: list[asyncio.Task] = []
299+
for p in ClientManager().get_processes():
300+
p.terminate()
301+
termination_tasks.append(asyncio.create_task(p.wait()))
302+
await asyncio.gather(*termination_tasks)

src/vectorcode/lsp_main.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
get_project_config,
4444
parse_cli_args,
4545
)
46-
from vectorcode.common import get_client, get_collection, try_server
46+
from vectorcode.common import ClientManager, get_collection, try_server
4747
from vectorcode.subcommands.ls import get_collection_list
4848
from vectorcode.subcommands.query import build_query_results
4949

@@ -114,7 +114,7 @@ async def execute_command(ls: LanguageServer, args: list[str]):
114114
parsed_args.project_root
115115
].merge_from(parsed_args)
116116
final_configs.pipe = True
117-
client = await get_client(final_configs)
117+
client = (await ClientManager().get_client(final_configs)).client
118118
if final_configs.action in {CliAction.vectorise, CliAction.query}:
119119
collection = await get_collection(
120120
client=client,
@@ -123,7 +123,7 @@ async def execute_command(ls: LanguageServer, args: list[str]):
123123
)
124124
else:
125125
final_configs = parsed_args
126-
client = await get_client(parsed_args)
126+
client = (await ClientManager().get_client(final_configs)).client
127127
collection = None
128128
logger.info("Merged final configs: %s", final_configs)
129129
progress_token = str(uuid.uuid4())
@@ -266,9 +266,11 @@ async def lsp_start() -> int:
266266
logger.info(f"{DEFAULT_PROJECT_ROOT=}")
267267

268268
logger.info("Parsed LSP server CLI arguments: %s", args)
269-
await asyncio.to_thread(server.start_io)
270-
271-
return 0
269+
try:
270+
await asyncio.to_thread(server.start_io)
271+
finally:
272+
await ClientManager().kill_servers()
273+
return 0
272274

273275

274276
def main(): # pragma: nocover

src/vectorcode/main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ async def async_main():
100100
return_val = 1
101101
logger.error(traceback.format_exc())
102102
finally:
103-
for p in ClientManager().get_processes():
104-
p.terminate()
105-
await p.wait()
103+
await ClientManager().kill_servers()
106104
return return_val
107105

108106

src/vectorcode/mcp_main.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,7 @@ async def run_server(): # pragma: nocover
299299
mcp = await mcp_server()
300300
await mcp.run_stdio_async()
301301
finally:
302-
termination_tasks: list[asyncio.Task] = []
303-
for p in ClientManager().get_processes():
304-
p.terminate()
305-
termination_tasks.append(asyncio.create_task(p.wait()))
306-
await asyncio.gather(*termination_tasks)
302+
await ClientManager().kill_servers()
307303
return 0
308304

309305

0 commit comments

Comments
 (0)