From 1e40d8fbc0cabc8bc75f3f1a0b139e24c86aac93 Mon Sep 17 00:00:00 2001 From: Griatch Date: Fri, 26 Jun 2020 12:31:57 +0200 Subject: [PATCH] Add maintenace task to portal, dropping db connection to resolve mysql timeouts. Resolves #2101. --- evennia/server/portal/portal.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/evennia/server/portal/portal.py b/evennia/server/portal/portal.py index 633e63bb51..1bf3fa2acf 100644 --- a/evennia/server/portal/portal.py +++ b/evennia/server/portal/portal.py @@ -13,6 +13,7 @@ import time from os.path import dirname, abspath from twisted.application import internet, service +from twisted.internet.task import LoopingCall from twisted.internet import protocol, reactor from twisted.python.log import ILogObserver @@ -20,6 +21,7 @@ import django django.setup() from django.conf import settings +from django.db import connection import evennia @@ -101,10 +103,29 @@ except ImportError: WEB_PLUGINS_MODULE = None INFO_DICT["errors"] = ( "WARNING: settings.WEB_PLUGINS_MODULE not found - " - "copy 'evennia/game_template/server/conf/web_plugins.py to mygame/server/conf." + "copy 'evennia/game_template/server/conf/web_plugins.py to " + "mygame/server/conf." ) +_MAINTENANCE_COUNT = 0 + + +def _portal_maintenance(): + """ + Repeated maintenance tasks for the portal. + + """ + global _MAINTENANCE_COUNT + + _MAINTENANCE_COUNT += 1 + + if _MAINTENANCE_COUNT % (3600 * 7) == 0: + # drop database connection every 7 hrs to avoid default timeouts on MySQL + # (see https://github.com/evennia/evennia/issues/1376) + connection.close() + + # ------------------------------------------------------------- # Portal Service object # ------------------------------------------------------------- @@ -142,6 +163,8 @@ class Portal(object): self.server_info_dict = {} self.start_time = time.time() + self.maintenance_task = LoopingCall(_portal_maintenance) + self.maintenance_task.start(60, now=True) # call every minute # in non-interactive portal mode, this gets overwritten by # cmdline sent by the evennia launcher