removed the core server reference from sessions, added a shutdown handler to initiate callbacks for when a session shutsdown, this is how the core server can run the same functionality going forward, small core-daemon documentation cleanup
This commit is contained in:
parent
3f82c980de
commit
7ad57bfb53
3 changed files with 115 additions and 68 deletions
|
@ -31,7 +31,7 @@ class CoreServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
|||
Server class initialization takes configuration data and calls
|
||||
the SocketServer constructor
|
||||
|
||||
:param tuple server_address: server host and port to use
|
||||
:param tuple[str, int] server_address: server host and port to use
|
||||
:param class handler_class: request handler
|
||||
:param dict config: configuration setting
|
||||
:return:
|
||||
|
@ -64,10 +64,8 @@ class CoreServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
|||
:param CoreServer server: server to remove
|
||||
:return: nothing
|
||||
"""
|
||||
try:
|
||||
if server in cls.servers:
|
||||
cls.servers.remove(server)
|
||||
except KeyError:
|
||||
logger.exception("error removing server: %s", server)
|
||||
|
||||
def shutdown(self):
|
||||
"""
|
||||
|
@ -150,8 +148,13 @@ class CoreServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
|||
if session_id not in self.sessions
|
||||
)
|
||||
|
||||
session = Session(session_id, config=self.config, server=self)
|
||||
# create and add session to local manager
|
||||
session = Session(session_id, config=self.config)
|
||||
self.add_session(session)
|
||||
|
||||
# add shutdown handler to remove session from manager
|
||||
session.shutdown_handlers.append(self.session_shutdown)
|
||||
|
||||
return session
|
||||
|
||||
def get_session(self, session_id=None):
|
||||
|
@ -187,6 +190,15 @@ class CoreServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
|||
|
||||
return session
|
||||
|
||||
def session_shutdown(self, session):
|
||||
"""
|
||||
Handler method to be used as a callback when a session has shutdown.
|
||||
|
||||
:param core.session.Session session: session shutting down
|
||||
:return: nothing
|
||||
"""
|
||||
self.remove_session(session)
|
||||
|
||||
def to_session_message(self, flags=0):
|
||||
"""
|
||||
Build CORE API Sessions message based on current session info.
|
||||
|
@ -293,7 +305,7 @@ class CoreUdpServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
|
|||
Server class initialization takes configuration data and calls
|
||||
the SocketServer constructor
|
||||
|
||||
:param str server_address: server address
|
||||
:param tuple[str, int] server_address: server address
|
||||
:param class handler_class: class for handling requests
|
||||
:param main_server: main server to associate with
|
||||
"""
|
||||
|
@ -320,7 +332,7 @@ class CoreAuxServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
|||
"""
|
||||
Create a CoreAuxServer instance.
|
||||
|
||||
:param str server_address: server address
|
||||
:param tuple[str, int] server_address: server address
|
||||
:param class handler_class: class for handling requests
|
||||
:param main_server: main server to associate with
|
||||
"""
|
||||
|
|
|
@ -210,13 +210,12 @@ class Session(object):
|
|||
CORE session manager.
|
||||
"""
|
||||
|
||||
def __init__(self, session_id, config=None, server=None, persistent=False, mkdir=True):
|
||||
def __init__(self, session_id, config=None, persistent=False, mkdir=True):
|
||||
"""
|
||||
Create a Session instance.
|
||||
|
||||
:param int session_id: session id
|
||||
:param dict config: session configuration
|
||||
:param core.coreserver.CoreServer server: core server object
|
||||
:param bool persistent: flag is session is considered persistent
|
||||
:param bool mkdir: flag to determine if a directory should be made
|
||||
"""
|
||||
|
@ -256,9 +255,6 @@ class Session(object):
|
|||
|
||||
self.add_state_hook(state=EventTypes.RUNTIME_STATE.value, hook=self.runtime_state_hook)
|
||||
|
||||
# TODO: remove this server reference
|
||||
self.server = server
|
||||
|
||||
if not persistent:
|
||||
SessionManager.add(self)
|
||||
|
||||
|
@ -307,6 +303,7 @@ class Session(object):
|
|||
self.link_handlers = []
|
||||
self.file_handlers = []
|
||||
self.config_handlers = []
|
||||
self.shutdown_handlers = []
|
||||
|
||||
def shutdown(self):
|
||||
"""
|
||||
|
@ -333,13 +330,13 @@ class Session(object):
|
|||
if not preserve:
|
||||
shutil.rmtree(self.session_dir, ignore_errors=True)
|
||||
|
||||
# remove session from server if one was provided
|
||||
if self.server:
|
||||
self.server.remove_session(self)
|
||||
|
||||
# remove this session from the manager
|
||||
SessionManager.remove(self)
|
||||
|
||||
# call session shutdown handlers
|
||||
for handler in self.shutdown_handlers:
|
||||
handler(self)
|
||||
|
||||
def broadcast_event(self, event_data):
|
||||
"""
|
||||
Handle event data that should be provided to event handler.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue