From 1c6a45a1d2680e42bab48d78eeb66c73884e72b5 Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 11 Nov 2023 15:40:52 -0500 Subject: [PATCH] Cleanly exit on SIGINT --- main.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 61a73f9..bbfd3bf 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ """GitMon""" +import signal import time from loguru import logger from gitmon.api import GitHubApi @@ -21,21 +22,32 @@ def log_event(event): logger.log('CREATE', f'{event_type}:\thttps://github.com/{repo["name"]}, {actor["login"]}') +def sigint_handler(signum, frame): + """Signal handler for Ctrl+C.""" + raise KeyboardInterrupt + + def main(): """Main entry point.""" + # Setup logger and signal handling setup_logger() + signal.signal(signal.SIGINT, sigint_handler) + api = GitHubApi(token) - while True: - events = api.get_events() - for event in events: - if ( - isinstance(event, dict) - and event.get('type') in ['CreateEvent', 'DeleteEvent'] - ): - log_event(event) + try: + while True: + events = api.get_events() + for event in events: + if ( + isinstance(event, dict) + and event.get('type') in ['CreateEvent', 'DeleteEvent'] + ): + log_event(event) - time.sleep(5) + time.sleep(1) + except KeyboardInterrupt: + logger.info('Program terminating') if __name__ == '__main__':