Commit b4c6d8b0 authored by Gero Müller's avatar Gero Müller
Browse files

fix shutdown procedure. send stop topic

parent 6e7564d5
......@@ -293,22 +293,25 @@ class Server(object):
if os.path.isfile(cherrypy_conf):
self.__application.merge(cherrypy_conf)
def _on_exit(self):
vispa.bus.send_topic("exit", broadcast=True)
vispa.publish("exit")
def _on_stop(self):
vispa.bus.send_topic("stop", broadcast=True)
vispa.publish("stop")
def start(self):
if hasattr(cherrypy.engine, 'signal_handler'):
cherrypy.engine.signal_handler.subscribe()
cherrypy.engine.subscribe("exit", self._on_exit, 0)
cherrypy.engine.subscribe("stop", self._on_stop, 0)
cherrypy.engine.start()
vispa.setup_thread_dump()
def stop(self):
vispa.publish("shutdown")
cherrypy.engine.stop()
cherrypy.engine.exit()
self._engine.dispose()
def run(self):
self.start()
cherrypy.engine.block()
self.stop()
def _load_extensions(self):
self._extensions = {}
......
......@@ -73,25 +73,32 @@ class Bus:
self.cleaner.start()
def send(self, session_id=None, user_id=None, data=None,
except_sessions=None, binary=False, encode_json=True):
except_sessions=None, binary=False, encode_json=True,
broadcast=False):
except_sessions = except_sessions or []
subscribers = []
# session_id is preferred over user_id
if session_id:
session_id = unicode(session_id)
if session_id in SUBSCRIBERS.keys():
if session_id not in except_sessions:
subscribers.append(SUBSCRIBERS[session_id])
elif user_id:
user_id = unicode(user_id)
if user_id in USER_SESSIONS.keys():
for session_id in USER_SESSIONS[user_id]:
if broadcast:
logger.debug("broadcast")
subscribers = SUBSCRIBERS.values()
else:
subscribers = []
# session_id is preferred over user_id
if session_id:
session_id = unicode(session_id)
if session_id in SUBSCRIBERS.keys():
if session_id not in except_sessions:
subscribers.append(SUBSCRIBERS[session_id])
elif user_id:
user_id = unicode(user_id)
if user_id in USER_SESSIONS.keys():
for session_id in USER_SESSIONS[user_id]:
if session_id not in except_sessions:
subscribers.append(SUBSCRIBERS[session_id])
if encode_json:
data = dumps(data)
for subscriber in subscribers:
logger.debug("send")
subscriber.send(data, binary=binary)
def send_topic(self, topic, *args, **kwargs):
......@@ -108,7 +115,7 @@ class Bus:
data = None
# atm, we cannot do anything when the msg
# is not json parsable or has no key 'topic'
if not data or not "topic" in data.keys():
if not data or "topic" not in data.keys():
return
# session_id has to be set for safety reasons
if not session_id:
......@@ -123,7 +130,7 @@ class Bus:
user_id = SESSION_OWNERS[session_id]
for handler in self._handlers[topic]:
handler(session_id=session_id, user_id=user_id, data=data,
topic=topic)
topic=topic)
def subscribe(self, topic, handler):
if topic not in self._handlers.keys():
......@@ -194,7 +201,7 @@ class CleanerThread(Thread):
Thread.__init__(self, *kargs, **kwargs)
self.daemon = True
self.running = True
cherrypy.engine.subscribe("stop", self._stop)
cherrypy.engine.subscribe("exit", self._stop)
def _stop(self):
self._running = False
......
......@@ -711,7 +711,7 @@ class ConnectionPool(object):
self._serv_thread.start()
vispa.register_callback('user.logout', self.clear)
vispa.register_callback('shutdown', self.clear)
vispa.register_callback('stop', self.clear)
def __del__(self):
self._serv_running = False
......@@ -839,9 +839,6 @@ class InstancePool(object):
def __init__(self):
self._instances = {}
vispa.register_callback('user.logout', self.clear)
vispa.register_callback('shutdown', self.clear)
def __del__(self):
for key in self._instances.keys():
del self._instances[key]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment