From 895f8504cd98d85a5c2eb1348b2b699c4cf48b2b Mon Sep 17 00:00:00 2001 From: Griatch Date: Fri, 6 Mar 2015 16:54:37 +0100 Subject: [PATCH] Run migrations! Removed global system scripts, moving into maintenance functions on the portal and server level. This means that runtimes will be reset as the system resets to the new system. --- evennia/commands/default/system.py | 2 +- evennia/server/server.py | 33 +++++++++++++++++------------- evennia/utils/gametime.py | 17 ++++++--------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/evennia/commands/default/system.py b/evennia/commands/default/system.py index 7c9f8d8152..467444e369 100644 --- a/evennia/commands/default/system.py +++ b/evennia/commands/default/system.py @@ -604,7 +604,7 @@ class CmdTime(MuxCommand): table.align = 'l' table.add_row(["Current server uptime", utils.time_format(gametime.uptime(), 3)]) table.add_row(["Total server running time", utils.time_format(gametime.runtime(), 2)]) - table.add_row(["Total in-game time (realtime x %g" % (gametime.TIMEFACTOR), utils.time_format(gametime.gametime(), 2)]) + table.add_row(["Total in-game time (realtime x %g)" % (gametime.TIMEFACTOR), utils.time_format(gametime.gametime(), 2)]) table.add_row(["Server time stamp", datetime.datetime.now()]) self.caller.msg(str(table)) diff --git a/evennia/server/server.py b/evennia/server/server.py index d10aa84bcb..8ac181da76 100644 --- a/evennia/server/server.py +++ b/evennia/server/server.py @@ -79,22 +79,31 @@ WEBCLIENT_ENABLED = settings.WEBCLIENT_ENABLED _MAINTENANCE_COUNT = 0 _FLUSH_CACHE = None _IDMAPPER_CACHE_MAXSIZE = settings.IDMAPPER_CACHE_MAXSIZE +_GAMETIME_MODULE = None + def _server_maintenance(): """ This maintenance function handles repeated checks and updates that the server needs to do. It is called every 5 minutes. """ - global EVENNIA, _MAINTENANCE_COUNT - global _FLUSH_CACHE + global EVENNIA, _MAINTENANCE_COUNT, _FLUSH_CACHE, _GAMETIME_MODULE if not _FLUSH_CACHE: from evennia.utils.idmapper.models import conditional_flush as _FLUSH_CACHE + if not _GAMETIME_MODULE: + from evennia.utils import gametime as _GAMETIME_MODULE _MAINTENANCE_COUNT += 1 - # update game time - EVENNIA.runtime += 60.0 - ServerConfig.objects.conf("runtime", EVENNIA.runtime) - EVENNIA.runtime_last_saved = time.time() + now = time.time() + if _MAINTENANCE_COUNT == 1: + # first call after a reload + _GAMETIME_MODULE.SERVER_START_TIME = now + _GAMETIME_MODULE.SERVER_RUNTIME = ServerConfig.objects.conf("runtime", default=0.0) + else: + _GAMETIME_MODULE.SERVER_RUNTIME += 60.0 + # update game time and save it across reloads + _GAMETIME_MODULE.SERVER_RUNTIME_LAST_UPDATED = now + ServerConfig.objects.conf("runtime", _GAMETIME_MODULE.SERVER_RUNTIME) if _MAINTENANCE_COUNT % 300 == 0: # check cache size every 5 minutes @@ -108,7 +117,8 @@ def _server_maintenance(): # validate channels off-sync with scripts print "maintenance: validate channels..." evennia.CHANNEL_HANDLER.update() - +maintenance_task = LoopingCall(_server_maintenance) +maintenance_task.start(60, now=True) # call every minute #------------------------------------------------------------ # Evennia Main Server object @@ -139,6 +149,8 @@ class Evennia(object): # Database-specific startup optimizations. self.sqlite3_prep() + self.start_time = time.time() + # Run the initial setup if needed self.run_initial_setup() @@ -152,10 +164,6 @@ class Evennia(object): self.game_running = True # track the server time - self.start_time = time.time() - self.runtime = ServerConfig.objects.conf("runtime", default=0.0) - self.runtime_last_saved = self.start_time - self.run_init_hooks() # Server startup methods @@ -525,6 +533,3 @@ if os.name == 'nt': with open(os.path.join(settings.GAME_DIR, 'server.pid'), 'w') as f: f.write(str(os.getpid())) -# start the maintenance task -maintenance_task = LoopingCall(_server_maintenance) -maintenance_task.start(60) # call every minute diff --git a/evennia/utils/gametime.py b/evennia/utils/gametime.py index b4d51c2210..8b4a7db370 100644 --- a/evennia/utils/gametime.py +++ b/evennia/utils/gametime.py @@ -31,9 +31,10 @@ WEEK = DAY * settings.TIME_DAY_PER_WEEK MONTH = WEEK * settings.TIME_WEEK_PER_MONTH YEAR = MONTH * settings.TIME_MONTH_PER_YEAR -# link to the main Server -_EVENNIA = None - +# these are kept updated by the server maintenance loop +SERVER_START_TIME = 0.0 +SERVER_RUNTIME_LAST_UPDATED = 0.0 +SERVER_RUNTIME = 0.0 def _format(seconds, *divisors) : """ @@ -65,20 +66,14 @@ def _format(seconds, *divisors) : def runtime(format=False): "Get the total runtime of the server since first start (minus downtimes)" - global _EVENNIA - if not _EVENNIA: - from evennia.server.server import EVENNIA as _EVENNIA - runtime = _EVENNIA.runtime + (time() - _EVENNIA.runtime_last_saved) + runtime = SERVER_RUNTIME + (time() - SERVER_RUNTIME_LAST_UPDATED) if format: return _format(runtime, 31536000, 2628000, 604800, 86400, 3600, 60) return runtime def uptime(format=False): "Get the current uptime of the server since last reload" - global _EVENNIA - if not _EVENNIA: - from evennia.server.server import EVENNIA as _EVENNIA - uptime = time() - _EVENNIA.start_time + uptime = time() - SERVER_START_TIME if format: return _format(uptime, 31536000, 2628000, 604800, 86400, 3600, 60) return uptime