def main(): errors = [] try: bot.log("Initializing bot") while True: try: for error in errors: bot.log(error, alert=True) bot.main() except HTTPError as e: # HTTP Errors may indicate reddit is overloaded. # Sleep for some extra time. bot.log(str(e) + " ") time.sleep(SLEEP_TIME*2) except ConnectionError as e: bot.log(str(e) + " ") time.sleep(SLEEP_TIME*2) except Exception as e: # If another exception occurs, add the message to a buffer so # it can be sent to the admins in the try block above. # Otherwise the bot.log method may cause another error that # won't be caught. error_msg = "Error running bot.main: {error}".format(error=e) # Avoid adding duplicates. if len(errors) == 0 or errors[-1] != error_msg: errors.append(error_msg) time.sleep(SLEEP_TIME) except KeyboardInterrupt: exit_msg = '' exit(0) except Exception as e: tb = traceback.format_exc() exit_msg = "Depoyment error: {traceback}\n".format(traceback=tb) bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True) exit(1)
def test_send_admin_message(self, mock_reddit): r = mock_reddit.return_value cb.ADMIN = 'AdminUser' body = "Hello Admin" cb.log(body, alert=True) args, kwargs = r.send_message.call_args self.assertTrue(body in args[2]) r.send_message.assert_called_once_with('AdminUser', args[1], args[2])
def main(): try: bot.log("Initializing bot") while True: try: bot.main() except HTTPError as e: # HTTP Errors may indicate reddit is overloaded. # Sleep for some extra time. bot.log(str(e) + " ") time.sleep(SLEEP_TIME*2) except ConnectionError as e: bot.log(str(e) + " ") time.sleep(SLEEP_TIME*2) except Exception as e: bot.log("Error running bot.main: {error}".format( error=e), alert=True) time.sleep(SLEEP_TIME) except KeyboardInterrupt: exit_msg = '' except Exception as e: tb = traceback.format_exc() exit_msg = "Depoyment error: {traceback}\n".format(traceback=tb) bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True)
def main(): try: bot.log("Initializing bot") while True: try: bot.main() except HTTPError as e: # HTTP Errors may indicate reddit is overloaded. # Sleep for some extra time. bot.log(str(e) + " ") time.sleep(SLEEP_TIME * 2) except Exception as e: bot.log("Error running bot.main: {error}".format(error=e), alert=True) time.sleep(SLEEP_TIME) except KeyboardInterrupt: exit_msg = '' except Exception as e: tb = traceback.format_exc() exit_msg = "Depoyment error: {traceback}\n".format(traceback=tb) bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True)
def main(): errors, log_buffer = {}, [] try: bot.log("Initializing bot") while True: try: for error in log_buffer: bot.log(error, alert=True) log_buffer = [] bot.main() errors = {} time.sleep(SLEEP_TIME) except HTTPError as e: # HTTP Errors may indicate reddit is overloaded. # Sleep for some extra time. bot.log(str(e) + " ") time.sleep(ERROR_TIMEOUT) except ConnectionError as e: bot.log(str(e) + " ") time.sleep(ERROR_TIMEOUT) except Exception as e: error = str(e) or repr(e) if errors.get(error): errors[error] += 1 if errors[error] >= ERROR_LIMIT: bot.log( "Encounted error of type \"{}\" {} times in a row, " "bot shutting down".format(error, ERROR_LIMIT)) exit(1) bot.log("Error running bot.main: {} ({}/{})".format( error, errors[error], ERROR_LIMIT)) else: errors[error] = 1 # If another exception occurs, add the message to a buffer so # it can be sent to the admins in the try block above. # Otherwise the bot.log method may cause another error that # won't be caught. tb = traceback.format_exc() error_msg = "Error running bot.main:\n{error}".format( error=bot.code_block(tb)) # Avoid adding duplicates. if len(log_buffer) == 0 or log_buffer[-1] != error_msg: log_buffer.append(error_msg) time.sleep(ERROR_TIMEOUT) except KeyboardInterrupt: exit_msg = '' exit(0) except Exception as e: tb = traceback.format_exc() exit_msg = "Depoyment error:\n{traceback}\n".format( traceback=bot.code_block(tb)) bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True) exit(1)
def main(): errors, log_buffer = {}, [] try: bot.log("Initializing bot") while True: try: for error in log_buffer: bot.log(error, alert=True) log_buffer = [] bot.main() errors = {} time.sleep(SLEEP_TIME) except HTTPError as e: # HTTP Errors may indicate reddit is overloaded. # Sleep for some extra time. bot.log(str(e) + " ") time.sleep(ERROR_TIMEOUT) except ConnectionError as e: bot.log(str(e) + " ") time.sleep(ERROR_TIMEOUT) except Exception as e: error = str(e) or repr(e) if errors.get(error): errors[error] += 1 if errors[error] >= ERROR_LIMIT: bot.log( 'Encounted error of type "{}" {} times in a row, ' "bot shutting down".format(error, ERROR_LIMIT) ) exit(1) bot.log("Error running bot.main: {} ({}/{})".format(error, errors[error], ERROR_LIMIT)) else: errors[error] = 1 # If another exception occurs, add the message to a buffer so # it can be sent to the admins in the try block above. # Otherwise the bot.log method may cause another error that # won't be caught. tb = traceback.format_exc() error_msg = "Error running bot.main:\n{error}".format(error=bot.code_block(tb)) # Avoid adding duplicates. if len(log_buffer) == 0 or log_buffer[-1] != error_msg: log_buffer.append(error_msg) time.sleep(ERROR_TIMEOUT) except KeyboardInterrupt: exit_msg = "" exit(0) except Exception as e: tb = traceback.format_exc() exit_msg = "Depoyment error:\n{traceback}\n".format(traceback=bot.code_block(tb)) bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True) exit(1)