Add empty match cleanup job

This commit is contained in:
Giuseppe Guerra 2018-03-23 20:59:04 +01:00
parent e63a85e4a4
commit e0d54f49d1
3 changed files with 41 additions and 1 deletions

View File

@ -1,6 +1,9 @@
import copy import copy
import json import json
import threading import threading
import time
from common.log import logUtils as log from common.log import logUtils as log
from constants import dataTypes from constants import dataTypes
from constants import matchModModes 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.isLocked = False # if True, users can't change slots/teams. Used in tourney matches
self.isStarting = False self.isStarting = False
self._lock = threading.Lock() self._lock = threading.Lock()
self.createTime = int(time.time())
# Create all slots and reset them # Create all slots and reset them
self.slots = [] self.slots = []

View File

@ -1,3 +1,6 @@
import threading
import time
from objects import match from objects import match
from objects import glob from objects import glob
from constants import serverPackets from constants import serverPackets
@ -62,4 +65,32 @@ class matchList:
# Send match dispose packet to everyone in lobby # Send match dispose packet to everyone in lobby
glob.streams.broadcast("lobby", serverPackets.disposeMatch(matchID)) glob.streams.broadcast("lobby", serverPackets.disposeMatch(matchID))
del self.matches[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
View File

@ -181,6 +181,11 @@ if __name__ == "__main__":
glob.tokens.spamProtectionResetLoop() glob.tokens.spamProtectionResetLoop()
consoleHelper.printDone() consoleHelper.printDone()
# Initialize multiplayer cleanup loop
consoleHelper.printNoNl("> Initializing multiplayer cleanup loop... ")
glob.matches.cleanupLoop()
consoleHelper.printDone()
# Localize warning # Localize warning
glob.localize = generalUtils.stringToBool(glob.conf.config["localize"]["enable"]) glob.localize = generalUtils.stringToBool(glob.conf.config["localize"]["enable"])
if not glob.localize: if not glob.localize: