1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-30 20:28:08 +00:00

Implement special request to stop a module.

This commit is contained in:
grossmj 2014-05-13 14:17:04 -06:00
parent 0b47b5b41e
commit 0d944d39ef
4 changed files with 36 additions and 18 deletions

View File

@ -159,7 +159,7 @@ class IModule(multiprocessing.Process):
if signum: if signum:
self._ioloop.add_callback_from_signal(self._shutdown) self._ioloop.add_callback_from_signal(self._shutdown)
else: else:
self._shutdown() self._ioloop.add_callback(self._shutdown)
def send_response(self, results): def send_response(self, results):
""" """
@ -240,6 +240,13 @@ class IModule(multiprocessing.Process):
if self._stopping: if self._stopping:
return return
# handle special request to stop the module
# e.g. useful on Windows where the
# SIGBREAK signal cound't be propagated
if request[0] == b"stop":
self.stop()
return
try: try:
request = zmq.utils.jsonapi.loads(request[0]) request = zmq.utils.jsonapi.loads(request[0])
except ValueError: except ValueError:

View File

@ -132,9 +132,8 @@ class Dynamips(IModule):
if not sys.platform.startswith("win32"): if not sys.platform.startswith("win32"):
self._callback.stop() self._callback.stop()
if self._hypervisor_manager:
self._hypervisor_manager.stop_all_hypervisors()
self.reset()
IModule.stop(self, signum) # this will stop the I/O loop IModule.stop(self, signum) # this will stop the I/O loop
def _check_hypervisors(self): def _check_hypervisors(self):
@ -175,11 +174,11 @@ class Dynamips(IModule):
return instance_dict[device_id] return instance_dict[device_id]
@IModule.route("dynamips.reset") @IModule.route("dynamips.reset")
def reset(self, request): def reset(self, request=None):
""" """
Resets the module. Resets the module (JSON-RPC notification).
:param request: JSON request :param request: JSON request (not used)
""" """
# stop all Dynamips hypervisors # stop all Dynamips hypervisors

View File

@ -112,11 +112,7 @@ class IOU(IModule):
""" """
self._iou_callback.stop() self._iou_callback.stop()
# delete all IOU instances self.reset()
for iou_id in self._iou_instances:
iou_instance = self._iou_instances[iou_id]
iou_instance.delete()
IModule.stop(self, signum) # this will stop the I/O loop IModule.stop(self, signum) # this will stop the I/O loop
def _check_iou_is_alive(self): def _check_iou_is_alive(self):
@ -161,11 +157,11 @@ class IOU(IModule):
return self._iou_instances[iou_id] return self._iou_instances[iou_id]
@IModule.route("iou.reset") @IModule.route("iou.reset")
def reset(self, request): def reset(self, request=None):
""" """
Resets the module. Resets the module (JSON-RPC notification).
:param request: JSON request :param request: JSON request (not used)
""" """
# delete all IOU instances # delete all IOU instances

View File

@ -215,6 +215,17 @@ class Server(object):
log.info("ZeroMQ server listening to 127.0.0.1:{}".format(self._zmq_port)) log.info("ZeroMQ server listening to 127.0.0.1:{}".format(self._zmq_port))
return self._router return self._router
def stop_module(self, module):
"""
Stop a given module.
:param module: module name
"""
if not self._router.closed:
self._router.send_string(module, zmq.SNDMORE)
self._router.send_string("stop")
def _shutdown(self): def _shutdown(self):
""" """
Shutdowns the I/O loop and the ZeroMQ stream & socket. Shutdowns the I/O loop and the ZeroMQ stream & socket.
@ -243,6 +254,11 @@ class Server(object):
# terminate all modules # terminate all modules
for module in self._modules: for module in self._modules:
if module.is_alive(): if module.is_alive():
log.info("stopping {}".format(module.name))
self.stop_module(module.name)
module.join(timeout=3)
if module.is_alive():
# just kill the module if it is still alive.
log.info("terminating {}".format(module.name)) log.info("terminating {}".format(module.name))
module.terminate() module.terminate()
module.join(timeout=1) module.join(timeout=1)
@ -252,4 +268,4 @@ class Server(object):
if signum: if signum:
ioloop.add_callback_from_signal(self._shutdown) ioloop.add_callback_from_signal(self._shutdown)
else: else:
self._shutdown() ioloop.add_callback(self._shutdown)