Skip to content

Commit

Permalink
Refactored SIGINT handler so it catches SIGINT during relog loop (#3867)
Browse files Browse the repository at this point in the history
* Refactored SIGINT handler so it catches SIGINT during relog loop

* typo fix
  • Loading branch information
Quantra authored and elicwhite committed Aug 14, 2016
1 parent 9a06549 commit e15a06d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,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)
Expand Down Expand Up @@ -103,7 +103,7 @@ def handle_sigint(*args):
while True:
bot.tick()

except (KeyboardInterrupt, SIGINTRecieved):
except KeyboardInterrupt:
bot.event_manager.emit(
'bot_exit',
sender=bot,
Expand Down Expand Up @@ -147,6 +147,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 caught SIGINT. Shutting down.'
)
report_summary(bot)
except Exception as e:
# always report session summary and then raise exception
if bot:
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e15a06d

Please sign in to comment.