Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gql-cli add signal handlers to catch ctrl-c and close cleanly #276

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions scripts/gql-cli
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import asyncio
import sys
from signal import SIGINT, SIGTERM

from gql.cli import get_parser, main

Expand All @@ -9,8 +10,23 @@ parser = get_parser(with_examples=True)
args = parser.parse_args()

try:
# Execute the script
exit_code = asyncio.run(main(args))
# Create a new asyncio event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

# Create a gql-cli task with the supplied arguments
main_task = asyncio.ensure_future(main(args), loop=loop)

# Add signal handlers to close gql-cli cleanly on Control-C
for signal in [SIGINT, SIGTERM]:
loop.add_signal_handler(signal, main_task.cancel)

# Run the asyncio loop to execute the task
exit_code = 0
try:
exit_code = loop.run_until_complete(main_task)
finally:
loop.close()

# Return with the correct exit code
sys.exit(exit_code)
Expand Down