Add empty match cleanup job
This commit is contained in:
		| @@ -1,6 +1,9 @@ | ||||
| import copy | ||||
| import json | ||||
| import threading | ||||
|  | ||||
| import time | ||||
|  | ||||
| from common.log import logUtils as log | ||||
| from constants import dataTypes | ||||
| from constants import matchModModes | ||||
| @@ -62,6 +65,7 @@ class match: | ||||
| 		self.isLocked = False 	# if True, users can't change slots/teams. Used in tourney matches | ||||
| 		self.isStarting = False | ||||
| 		self._lock = threading.Lock() | ||||
| 		self.createTime = int(time.time()) | ||||
|  | ||||
| 		# Create all slots and reset them | ||||
| 		self.slots = [] | ||||
|   | ||||
| @@ -1,3 +1,6 @@ | ||||
| import threading | ||||
| import time | ||||
|  | ||||
| from objects import match | ||||
| from objects import glob | ||||
| from constants import serverPackets | ||||
| @@ -62,4 +65,32 @@ class matchList: | ||||
| 		# Send match dispose packet to everyone in lobby | ||||
| 		glob.streams.broadcast("lobby", serverPackets.disposeMatch(matchID)) | ||||
| 		del self.matches[matchID] | ||||
| 		log.info("MPROOM{}: Room disposed manually".format(_match.matchID)) | ||||
| 		log.info("MPROOM{}: Room disposed manually".format(_match.matchID)) | ||||
|  | ||||
| 	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: | ||||
| 		""" | ||||
| 		log.debug("Checking empty matches") | ||||
| 		t = int(time.time()) | ||||
| 		emptyMatches = [] | ||||
|  | ||||
| 		# 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 t - m.createTime >= 120: | ||||
| 				log.debug("Match #{} marked for cleanup".format(m.matchID)) | ||||
| 				emptyMatches.append(m.matchID) | ||||
|  | ||||
| 		# Dispose all empty matches | ||||
| 		for matchID in emptyMatches: | ||||
| 			self.disposeMatch(matchID) | ||||
|  | ||||
| 		# Schedule a new check (endless loop) | ||||
| 		threading.Timer(30, self.cleanupLoop).start() | ||||
							
								
								
									
										5
									
								
								pep.py
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								pep.py
									
									
									
									
									
								
							| @@ -181,6 +181,11 @@ if __name__ == "__main__": | ||||
| 		glob.tokens.spamProtectionResetLoop() | ||||
| 		consoleHelper.printDone() | ||||
|  | ||||
| 		# Initialize multiplayer cleanup loop | ||||
| 		consoleHelper.printNoNl("> Initializing multiplayer cleanup loop... ") | ||||
| 		glob.matches.cleanupLoop() | ||||
| 		consoleHelper.printDone() | ||||
|  | ||||
| 		# Localize warning | ||||
| 		glob.localize = generalUtils.stringToBool(glob.conf.config["localize"]["enable"]) | ||||
| 		if not glob.localize: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user