Started removing the global maintenance scripts, but ran into strange AMP issues.

This commit is contained in:
Griatch 2015-03-06 15:59:58 +01:00
parent 17400ede6b
commit 71e9038c62
7 changed files with 112 additions and 195 deletions

View file

@ -8,10 +8,6 @@ total runtime of the server and the current uptime.
from time import time
from django.conf import settings
from evennia.scripts.scripts import DefaultScript
from evennia.utils.create import create_script
GAMETIME_SCRIPT_NAME = "sys_game_time"
# Speed-up factor of the in-game time compared
# to real time.
@ -35,67 +31,25 @@ WEEK = DAY * settings.TIME_DAY_PER_WEEK
MONTH = WEEK * settings.TIME_WEEK_PER_MONTH
YEAR = MONTH * settings.TIME_MONTH_PER_YEAR
# Cached time stamps
SERVER_STARTTIME = time()
SERVER_RUNTIME = 0.0
# link to the main Server
_EVENNIA = None
class GameTime(DefaultScript):
"""
This script repeatedly saves server times so
it can be retrieved after server downtime.
"""
def at_script_creation(self):
"""
Setup the script
"""
self.key = GAMETIME_SCRIPT_NAME
self.desc = "Saves uptime/runtime"
self.interval = 60
self.persistent = True
self.start_delay = True
self.attributes.add("run_time", 0.0) # OOC time
self.attributes.add("up_time", 0.0) # OOC time
def at_repeat(self):
"""
Called every minute to update the timers.
"""
self.attributes.add("run_time", runtime())
self.attributes.add("up_time", uptime())
def at_start(self):
"""
This is called once every server restart.
We reset the up time and load the relevant
times.
"""
global SERVER_RUNTIME
SERVER_RUNTIME = self.attributes.get("run_time")
def save():
"Force save of time. This is called by server when shutting down/reloading."
from evennia.scripts.models import ScriptDB
try:
script = ScriptDB.objects.get(db_key=GAMETIME_SCRIPT_NAME)
script.at_repeat()
except Exception:
from evennia.utils import logger
logger.log_trace()
def _format(seconds, *divisors) :
"""
Helper function. Creates a tuple of even dividends given
a range of divisors.
Helper function. Creates a tuple of even dividends given a range
of divisors.
Inputs
seconds - number of seconds to format
*divisors - a number of integer dividends. The number of seconds will be
integer-divided by the first number in this sequence, the remainder
will be divided with the second and so on.
Output:
A tuple of length len(*args)+1, with the last element being the last remaining
seconds not evenly divided by the supplied dividends.
Args:
seconds (int): Number of seconds to format
*divisors (int): a sequence of numbers of integer dividends. The
number of seconds will be integer-divided by the first number in
this sequence, the remainder will be divided with the second and
so on.
Returns:
time (tuple): This tuple has length len(*args)+1, with the
last element being the last remaining seconds not evenly
divided by the supplied dividends.
"""
results = []
@ -111,14 +65,20 @@ def _format(seconds, *divisors) :
def runtime(format=False):
"Get the total runtime of the server since first start (minus downtimes)"
runtime = SERVER_RUNTIME + (time() - SERVER_STARTTIME)
global _EVENNIA
if not _EVENNIA:
from evennia.server.server import EVENNIA as _EVENNIA
runtime = _EVENNIA.runtime + (time() - _EVENNIA.runtime_last_saved)
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"
uptime = time() - SERVER_STARTTIME
global _EVENNIA
if not _EVENNIA:
from evennia.server.server import EVENNIA as _EVENNIA
uptime = time() - _EVENNIA.start_time
if format:
return _format(uptime, 31536000, 2628000, 604800, 86400, 3600, 60)
return uptime
@ -167,13 +127,3 @@ def realtime_to_gametime(secs=0, mins=0, hrs=0, days=0,
return _format(gametime, YEAR, MONTH, WEEK, DAY, HOUR, MIN)
return gametime
# Time administration routines
def init_gametime():
"""
This is called once, when the server starts for the very first time.
"""
# create the GameTime script and start it
game_time = create_script(GameTime)
game_time.start()