diff --git a/src/robotremoteserver.py b/src/robotremoteserver.py index 0da8154..04f5e8e 100644 --- a/src/robotremoteserver.py +++ b/src/robotremoteserver.py @@ -74,6 +74,7 @@ def __init__(self, library, host='127.0.0.1', port=8270, port_file=None, self._server = StoppableXMLRPCServer(host, int(port)) self._register_functions(self._server) self._port_file = port_file + self._shutdown_callbacks = [] self._allow_remote_stop = allow_remote_stop \ if allow_stop == 'DEPRECATED' else allow_stop if serve: @@ -153,8 +154,28 @@ def _log(self, action, log=True, warn=False): print('*WARN*', end=' ') print('Robot Framework remote server at %s %s.' % (address, action)) + def add_shutdown_callback(self, shutdown_callable): + """ Adds each callabel to the list of functions to be called at shutdown + + :param shutdown_callable: A callabe function to call at shutdown + + A callable shutdown function without arguments may be added to the + RobotRemoteServer object. When the server is asked to shutdown it will + try to call each of the shutdown functions, passing if any error occurs. + """ + + if callable(shutdown_callable): + self._shutdown_callbacks.append(shutdown_callable) + else: + raise TypeError("Argument must be callable") + def stop(self): """Stop server.""" + for callback in self._shutdown_callbacks: + try: + callback() + except: + pass self._server.stop() # Exposed XML-RPC methods. Should they be moved to own class?