Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 3eb497c

Browse files
NickCaogithub-actions[bot]
authored andcommitted
Handle signals in j command
(cherry picked from commit 9d64e6f)
1 parent 4cfc5da commit 3eb497c

1 file changed

Lines changed: 38 additions & 14 deletions

File tree

  • packages/jumpstarter-cli/jumpstarter_cli

packages/jumpstarter-cli/jumpstarter_cli/j.py

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
1+
import concurrent
12
import sys
3+
from contextlib import ExitStack
24

35
import asyncclick as click
4-
from jumpstarter_cli_common.exceptions import handle_exceptions
6+
from anyio import create_task_group, get_cancelled_exc_class, run, to_thread
7+
from anyio.from_thread import BlockingPortal
8+
from jumpstarter_cli_common.exceptions import async_handle_exceptions, leaf_exceptions
9+
from jumpstarter_cli_common.signal import signal_handler
510

6-
from jumpstarter.utils.env import env
11+
from jumpstarter.utils.env import env_async
12+
13+
14+
async def j_async():
15+
@async_handle_exceptions
16+
async def cli():
17+
async with BlockingPortal() as portal:
18+
with ExitStack() as stack:
19+
async with env_async(portal, stack) as client:
20+
async with client.log_stream_async():
21+
await to_thread.run_sync(lambda: client.cli()(standalone_mode=False))
22+
23+
try:
24+
async with create_task_group() as tg:
25+
tg.start_soon(signal_handler, tg.cancel_scope)
26+
27+
try:
28+
await cli()
29+
finally:
30+
tg.cancel_scope.cancel()
31+
32+
except* click.ClickException as excgroup:
33+
for exc in leaf_exceptions(excgroup):
34+
exc.show()
35+
36+
sys.exit(1)
37+
except* (
38+
get_cancelled_exc_class(),
39+
concurrent.futures._base.CancelledError,
40+
) as _:
41+
sys.exit(2)
742

843

944
def j():
10-
with env() as client:
11-
12-
@handle_exceptions
13-
def cli():
14-
with client.log_stream():
15-
client.cli()(standalone_mode=False)
16-
17-
try:
18-
cli()
19-
except click.ClickException as e:
20-
e.show()
21-
sys.exit(1)
45+
run(j_async)
2246

2347

2448
if __name__ == "__main__":

0 commit comments

Comments
 (0)