Implemented persistent cache, events and gametime counter.

OBS - there is a new data table (for the persistent cache) so you need to sync or restart with your database.

* Persistent cache (pcache)- this works the same as the volatile cache, except it is regularly saved to disk and recovered upon restart. How often the pcache is backed up is set in preferences. This was heck of a tricky thing to get right due to the intricacies of pickle; for example it turns out there is a bug in cPickle, so only normal pickle works to store the cache objects.
* Persistent events - this makes use of the pcache to re-load the scheduled events every reload. Only events with the property "persistent" will be saved this way (if not set, events will get lost upon reboot, just like now). All the main system events have been implemented as persistent events, including a new event to regularly save the pcache to disk.
* In order to track persistent event timers across reboots, there is also a global "game time" defined now. This is saved in cache and counts seconds only when the server is running. Event timers are adjusted with an offset when restarting (otherwise they will be confused by the real time jumping forward after a downtime). There are also a small set of helpful routines in src/gametime.py to help convert from real time to game time (for easy creation of new events).
* Various info commands have been updated to incoorporate the time stamp and the cache sync information.
* There are a few test commands commented out in commands/general.py that I used for testing; I left them in if you want to test things quickly. It works here, but as always more people testing is needed.
/Griatch
This commit is contained in:
Griatch 2009-11-22 21:18:55 +00:00
parent 5e866c6b73
commit 1ea7e69821
16 changed files with 761 additions and 146 deletions

View file

@ -15,6 +15,9 @@ from src import alias_mgr
from src import cmdtable
from src import initial_setup
from src.util import functions_general
from src.cache import cache
from src import scheduler
from src import gametime
class EvenniaService(service.Service):
def __init__(self):
@ -34,11 +37,13 @@ class EvenniaService(service.Service):
# Begin startup debug output.
print '-'*50
firstrun = False
try:
# If this fails, this is an empty DB that needs populating.
ConfigValue.objects.get_configvalue('game_firstrun')
except ConfigValue.DoesNotExist:
print ' Game started for the first time, setting defaults.'
firstrun = True
initial_setup.handle_setup()
self.start_time = time.time()
@ -52,9 +57,27 @@ class EvenniaService(service.Service):
# Cache the aliases from the database for quick access.
alias_mgr.load_cmd_aliases()
# Load persistent cache from database into memory
cache.load_pcache()
if not firstrun:
# Find out how much offset the timer is (due to being
# offline).
time_sync = gametime.time_last_sync()
# Sync the in-game timer.
cache.set_pcache("_game_time0", self.start_time)
# Fire up the event scheduler.
event_cache = cache.get_pcache("_persistent_event_cache")
if event_cache and type(event_cache) == type(list()):
for event in event_cache:
# we adjust the executed time to account for offline time.
event.time_last_executed = event.time_last_executed + time_sync
scheduler.add_event(event)
print '-'*50
# Fire up the event scheduler.
events.add_global_events()
"""
BEGIN SERVER STARTUP METHODS
@ -102,6 +125,9 @@ class EvenniaService(service.Service):
"""
Gracefully disconnect everyone and kill the reactor.
"""
gametime.time_save()
cache.save_pcache()
logger.log_infomsg("Persistent cache and time saved prior to shutdown.")
session_mgr.announce_all(message)
session_mgr.disconnect_all_sessions()
reactor.callLater(0, reactor.stop)
@ -141,7 +167,6 @@ class EvenniaService(service.Service):
f.server = self
return f
def start_services(self, application):
"""
Starts all of the TCP services.