Web client users are now notified if the server is shut down forcefully (Ctrl-C, reboot etc)

This commit is contained in:
Griatch 2010-12-11 14:26:57 +00:00
parent 0eb5d29560
commit 7f3633c70a
3 changed files with 14 additions and 6 deletions

View file

@ -20,7 +20,7 @@ import time
from hashlib import md5 from hashlib import md5
from twisted.web import server, resource from twisted.web import server, resource
from twisted.internet import defer from twisted.internet import defer, reactor
from django.utils import simplejson from django.utils import simplejson
from django.utils.functional import Promise from django.utils.functional import Promise
@ -64,6 +64,7 @@ class WebClient(resource.Resource):
def __init__(self): def __init__(self):
self.requests = {} self.requests = {}
self.databuffer = {} self.databuffer = {}
reactor.addSystemEventTrigger('before', 'shutdown',self._forced_disconnect)
def getChild(self, path, request): def getChild(self, path, request):
""" """
@ -77,7 +78,15 @@ class WebClient(resource.Resource):
self.requests.get(suid, []).remove(request) self.requests.get(suid, []).remove(request)
except ValueError: except ValueError:
pass pass
def _forced_disconnect(self):
"""
Callback launched when webserver is closing forcefully (Ctrl-C, reboot etc)
All we do is make sure the connected clients are notitifed.
"""
for suid in self.requests.keys():
self.lineSend(suid, parse_html("{rThe MUD server shut down. You were disconnected.{n"))
def lineSend(self, suid, string, data=None): def lineSend(self, suid, string, data=None):
""" """
This adds the data to the buffer and/or sends it to This adds the data to the buffer and/or sends it to
@ -95,7 +104,7 @@ class WebClient(resource.Resource):
dataentries = self.databuffer.get(suid, []) dataentries = self.databuffer.get(suid, [])
dataentries.append(jsonify({'msg':string, 'data':data})) dataentries.append(jsonify({'msg':string, 'data':data}))
self.databuffer[suid] = dataentries self.databuffer[suid] = dataentries
def disconnect(self, suid): def disconnect(self, suid):
"Disconnect session with given suid." "Disconnect session with given suid."
sess = SESSIONS.session_from_suid(suid) sess = SESSIONS.session_from_suid(suid)

View file

@ -45,7 +45,7 @@ class DjangoWebRoot(resource.Resource):
pool = threadpool.ThreadPool() pool = threadpool.ThreadPool()
pool.start() pool.start()
# Set it up so the pool stops after e.g. Ctrl-C kills the server # Set it up so the pool stops after e.g. Ctrl-C kills the server
reactor.addSystemEventTrigger('after', 'shutdown', pool.stop) reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
# combine twisted's wsgi resource with django's wsgi handler # combine twisted's wsgi resource with django's wsgi handler
wsgi_resource = WSGIResource(reactor, pool, WSGIHandler()) wsgi_resource = WSGIResource(reactor, pool, WSGIHandler())
return wsgi_resource return wsgi_resource

View file

@ -88,7 +88,7 @@ function webclient_input(){
$('#inputform')[0].reset(); // clear input field $('#inputform')[0].reset(); // clear input field
}, },
error: function(XMLHttpRequest, textStatus, errorThrown){ error: function(XMLHttpRequest, textStatus, errorThrown){
msg_display("err", "Error: Server returned an error or timed out. Try resending."); msg_display("err", "Error: Server returned an error or timed out. Try resending or reloading the page.");
}, },
}) })
} }
@ -145,7 +145,6 @@ function webclient_close(){
}, },
error: function(XMLHttpRequest, textStatus, errorThrown){ error: function(XMLHttpRequest, textStatus, errorThrown){
CLIENT_HASH = '0'; CLIENT_HASH = '0';
alert("There was an error disconnecting from the mud server.");
} }
}); });