def usersTimeoutCheckLoop(self): """ Start timed out users disconnect loop. This function will be called every `checkTime` seconds and so on, forever. CALL THIS FUNCTION ONLY ONCE! :return: """ try: log.debug("Checking timed out clients") exceptions = [] timedOutTokens = [] # timed out users timeoutLimit = int(time.time()) - 100 for key, value in self.tokens.items(): # Check timeout (fokabot is ignored) #print("UserID {} Always Online: {}".format(value.userID, aobaHelper.getAlwaysOnline(value.userID))) if aobaHelper.getAlwaysOnline(value.userID) == False: if value.pingTime < timeoutLimit and not value.irc and not value.tournament: # That user has timed out, add to disconnected tokens # We can't delete it while iterating or items() throws an error timedOutTokens.append(key) # Delete timed out users from self.tokens # i is token string (dictionary key) for i in timedOutTokens: log.debug("{} timed out!!".format(self.tokens[i].username)) self.tokens[i].enqueue( serverPackets.notification( "Your connection to the server timed out.")) try: logoutEvent.handle(self.tokens[i], None) except Exception as e: exceptions.append(e) log.error( "Something wrong happened while disconnecting a timed out client. Reporting to Sentry " "when the loop ends.") del timedOutTokens # Re-raise exceptions if needed if exceptions: raise periodicLoopException(exceptions) finally: # Schedule a new check (endless loop) threading.Timer(100, self.usersTimeoutCheckLoop).start()
def cleanupLoop(self): """ Start match cleanup loop. Empty matches that have been created more than 60 seconds ago will get deleted. Useful when people create useless lobbies with `!mp make`. The check is done every 30 seconds. This method starts an infinite loop, call it only once! :return: """ try: log.debug("Checking empty matches") t = int(time.time()) emptyMatches = [] exceptions = [] # Collect all empty matches for key, m in self.matches.items(): #if [x for x in m.slots if x.user is not None]: # continue if m.inProgress: continue if t - m.lastActionTime >= 1800: log.debug("Match #{} marked for cleanup".format(m.matchID)) emptyMatches.append(m.matchID) # Dispose all idle matches for matchID in emptyMatches: try: self.disposeMatch(matchID) except Exception as e: exceptions.append(e) log.error( "Something wrong happened while disposing a timed out match. Reporting to Sentry when " "the loop ends." ) # Re-raise exception if needed if exceptions: raise periodicLoopException(exceptions) finally: # Schedule a new check (endless loop) threading.Timer(30, self.cleanupLoop).start()