Add maintenace task to portal, dropping db connection to resolve mysql timeouts. Resolves #2101.

This commit is contained in:
Griatch 2020-06-26 12:31:57 +02:00
parent cc354cc223
commit 1e40d8fbc0

View file

@ -13,6 +13,7 @@ import time
from os.path import dirname, abspath from os.path import dirname, abspath
from twisted.application import internet, service from twisted.application import internet, service
from twisted.internet.task import LoopingCall
from twisted.internet import protocol, reactor from twisted.internet import protocol, reactor
from twisted.python.log import ILogObserver from twisted.python.log import ILogObserver
@ -20,6 +21,7 @@ import django
django.setup() django.setup()
from django.conf import settings from django.conf import settings
from django.db import connection
import evennia import evennia
@ -101,10 +103,29 @@ except ImportError:
WEB_PLUGINS_MODULE = None WEB_PLUGINS_MODULE = None
INFO_DICT["errors"] = ( INFO_DICT["errors"] = (
"WARNING: settings.WEB_PLUGINS_MODULE not found - " "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 # Portal Service object
# ------------------------------------------------------------- # -------------------------------------------------------------
@ -142,6 +163,8 @@ class Portal(object):
self.server_info_dict = {} self.server_info_dict = {}
self.start_time = time.time() 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 # in non-interactive portal mode, this gets overwritten by
# cmdline sent by the evennia launcher # cmdline sent by the evennia launcher