catch SIGINT signal properly in 'up' function and call compose 'down' function for a graceful shutdown

Signed-off-by: Mohammad Kazemi <mokazemi@disroot.org>
This commit is contained in:
Mohammad Kazemi 2025-02-24 10:40:42 +03:30
parent 8b1bd0123c
commit 3973c476c4

View File

@ -2778,9 +2778,17 @@ async def compose_up(compose: PodmanCompose, args):
max_service_length = curr_length if curr_length > max_service_length else max_service_length
tasks = set()
async def handle_sigint():
log.info("Caught SIGINT, shutting down...")
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False))
await compose.commands["down"](compose, down_args)
for task in tasks:
task.cancel()
if sys.platform != 'win32':
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGINT, lambda: [t.cancel("User exit") for t in tasks])
loop.add_signal_handler(signal.SIGINT, lambda: asyncio.create_task(handle_sigint()))
for i, cnt in enumerate(compose.containers):
# Add colored service prefix to output by piping output through sed