[Core] Improved exit codes handling

Clean exits on crashes should not happen anymore
This commit is contained in:
Twentysix 2017-01-18 18:01:11 +01:00
parent bc20177d36
commit 7b6dbd201e

16
red.py
View File

@ -66,7 +66,7 @@ class Bot(commands.Bot):
self._message_modifiers = [] self._message_modifiers = []
self.settings = Settings() self.settings = Settings()
self._intro_displayed = False self._intro_displayed = False
self._restart_requested = False self._shutdown_mode = None
self.logger = set_logger(self) self.logger = set_logger(self)
if 'self_bot' in kwargs: if 'self_bot' in kwargs:
self.settings.self_bot = kwargs['self_bot'] self.settings.self_bot = kwargs['self_bot']
@ -101,8 +101,7 @@ class Bot(commands.Bot):
If restart is True, the exit code will be 26 instead If restart is True, the exit code will be 26 instead
The launcher automatically restarts Red when that happens""" The launcher automatically restarts Red when that happens"""
if restart: self._shutdown_mode = not restart
self._restart_requested = True
await self.logout() await self.logout()
def add_message_modifier(self, func): def add_message_modifier(self, func):
@ -594,12 +593,10 @@ if __name__ == '__main__':
errors="replace", errors="replace",
line_buffering=True) line_buffering=True)
bot = initialize() bot = initialize()
error = False
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
try: try:
loop.run_until_complete(main(bot)) loop.run_until_complete(main(bot))
except discord.LoginFailure: except discord.LoginFailure:
error = True
bot.logger.error(traceback.format_exc()) bot.logger.error(traceback.format_exc())
if not bot.settings.no_prompt: if not bot.settings.no_prompt:
choice = input("Invalid login credentials. If they worked before " choice = input("Invalid login credentials. If they worked before "
@ -616,13 +613,14 @@ if __name__ == '__main__':
except KeyboardInterrupt: except KeyboardInterrupt:
loop.run_until_complete(bot.logout()) loop.run_until_complete(bot.logout())
except Exception as e: except Exception as e:
error = True
bot.logger.exception("Fatal exception, attempting graceful logout", bot.logger.exception("Fatal exception, attempting graceful logout",
exc_info=e) exc_info=e)
loop.run_until_complete(bot.logout()) loop.run_until_complete(bot.logout())
finally: finally:
loop.close() loop.close()
if error: if bot._shutdown_mode is True:
exit(0)
elif bot._shutdown_mode is False:
exit(26) # Restart
else:
exit(1) exit(1)
if bot._restart_requested:
exit(26)