~rootmos/action

4fe14abd2624c287b41a4e6459303d0893b001e0 — Gustav Behm 7 months ago 7f90ac4
Catch SIGINT and exit cleanly
1 files changed, 21 insertions(+), 6 deletions(-)

M src/action/cli.py
M src/action/cli.py => src/action/cli.py +21 -6
@@ 1,10 1,11 @@
import asyncio
import socket
import argparse
import asyncio
import datetime
import json
import os
import signal
import socket
import sys
import json
import datetime

import xdg.BaseDirectory



@@ 96,16 97,30 @@ def main():

    if args.cmd == "bind":
        stop = asyncio.Event()
        async def go():
        async def act():
            stop.set()

        action = Action(name=args.action, coro=go)
        class Exit:
            def __init__(self):
                self.status = 0

            def interrupt(self):
                self.status = 1
                stop.set()
        ec = Exit()

        action = Action(name=args.action, coro=act)

        async def main():
            loop = asyncio.get_running_loop()

            loop.add_signal_handler(signal.SIGINT, ec.interrupt)

            await action.bind(socket_path)
            await stop.wait()

        asyncio.run(main())
        sys.exit(ec.status)

    elif args.cmd == "trigger":
        msg = {