From 5a7d55b735284809302cca046f512996ae2708d2 Mon Sep 17 00:00:00 2001 From: Vince Date: Sat, 13 Aug 2016 18:57:24 +0100 Subject: [PATCH 1/2] Refactored SIGINT handler so it catches SIGINT during relog loop --- pokecli.py | 19 ++++++++++++++----- pokemongo_bot/__init__.py | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pokecli.py b/pokecli.py index 064c87c9ba..301b281303 100644 --- a/pokecli.py +++ b/pokecli.py @@ -64,11 +64,11 @@ class SIGINTRecieved(Exception): pass def main(): bot = False - try: - def handle_sigint(*args): - raise SIGINTRecieved - signal.signal(signal.SIGINT, handle_sigint) + def handle_sigint(*args): + raise SIGINTRecieved + signal.signal(signal.SIGINT, handle_sigint) + try: logger.info('PokemonGO Bot v1.0') sys.stdout = codecs.getwriter('utf8')(sys.stdout) sys.stderr = codecs.getwriter('utf8')(sys.stderr) @@ -102,7 +102,7 @@ def handle_sigint(*args): while True: bot.tick() - except (KeyboardInterrupt, SIGINTRecieved): + except KeyboardInterrupt: bot.event_manager.emit( 'bot_exit', sender=bot, @@ -139,6 +139,15 @@ def handle_sigint(*args): except GeocoderQuotaExceeded: raise Exception("Google Maps API key over requests limit.") + except SIGINTRecieved: + if bot: + bot.event_manager.emit( + 'bot_interrupted', + sender=bot, + level='info', + formatted='Bot got SIGTERM. Shutting down.' + ) + report_summary(bot) except Exception as e: # always report session summary and then raise exception if bot: diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index ff5257c043..27ce65f8e4 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -150,6 +150,7 @@ def _register_events(self): self.event_manager.register_event('bot_start') self.event_manager.register_event('bot_exit') + self.event_manager.register_event('bot_interrupted') # sleep stuff self.event_manager.register_event( From 34e63b813326457c2dd5f6632bae47b094d31929 Mon Sep 17 00:00:00 2001 From: Vince Date: Sun, 14 Aug 2016 19:08:18 +0100 Subject: [PATCH 2/2] typo fix --- pokecli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pokecli.py b/pokecli.py index 301b281303..1c07365adf 100644 --- a/pokecli.py +++ b/pokecli.py @@ -145,7 +145,7 @@ def handle_sigint(*args): 'bot_interrupted', sender=bot, level='info', - formatted='Bot got SIGTERM. Shutting down.' + formatted='Bot caught SIGINT. Shutting down.' ) report_summary(bot) except Exception as e: