Agent: Add block parameter to IMaster.terminate()

This allows the caller to decide whether or not they're willing to wait
for the master to finish shutting down.
This commit is contained in:
Mike Salvatore 2021-12-15 09:00:41 -05:00
parent a051759764
commit f46bb60da5
3 changed files with 8 additions and 4 deletions

View File

@ -10,9 +10,10 @@ class IMaster(metaclass=abc.ABCMeta):
"""
@abc.abstractmethod
def terminate(self) -> None:
def terminate(self, block: bool = False) -> None:
"""
Stop the master and interrupt any actions that are currently being executed.
:param bool block: Whether or not to block and wait for the master to terminate.
"""
@abc.abstractmethod

View File

@ -52,12 +52,15 @@ class AutomatedMaster(IMaster):
self._master_thread.join()
logger.info("The simulation has been shutdown.")
def terminate(self):
def terminate(self, block: bool = False):
logger.info("Stopping automated breach and attack simulation")
self._stop.set()
if self._master_thread.is_alive():
if self._master_thread.is_alive() and block:
self._master_thread.join()
# We can only have confidence that the master terminated successfully if block is set
# and join() has returned.
logger.info("AutomatedMaster successfully terminated.")
def _run_master_thread(self):
self._simulation_thread.start()

View File

@ -124,7 +124,7 @@ class MockMaster(IMaster):
self._telemetry_messenger.send_telemetry(FileEncryptionTelem(path, success, error))
logger.info("Finished running payloads")
def terminate(self) -> None:
def terminate(self, block: bool = False) -> None:
logger.info("Terminating MockMaster")
def cleanup(self) -> None: