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