diff --git a/worker/emulator.py b/worker/emulator.py index bda2d65..c6262d7 100644 --- a/worker/emulator.py +++ b/worker/emulator.py @@ -68,6 +68,10 @@ def install_apk(self, path): OutputExecutor(cmd, banner=r"Success").start() return True + def stop(self): + logger.debug('Stopping emulator') + self._proc.stop() + if __name__ == '__main__': em = Emulator() diff --git a/worker/helpers.py b/worker/helpers.py index 32b6391..cc5556c 100644 --- a/worker/helpers.py +++ b/worker/helpers.py @@ -35,9 +35,9 @@ def get_endpoint(self, id): return self.instances[id]['endpoint'] def get_zk_client(self, id): - return self.instances[id]['zl_client'] + return self.instances[id]['zk_client'] - def get_num_endpoints(self): + def count(self): return len(self.instances) def start_endpoint(self, id): @@ -52,7 +52,7 @@ def iter_endpoints(self): return self.instances.iteritems() def setup(self): - for instance_id, _ in self.iter_instances(): + for instance_id, _ in self.iter_endpoints(): self.start_endpoint(instance_id) def stop_endpoint(self, id): @@ -64,7 +64,7 @@ def stop_endpoint(self, id): zk_client.teardown() def teardown(self): - for instance_id, _ in self.iter_instances(): + for instance_id, _ in self.iter_endpoints(): self.stop_endpoint(instance_id) \ No newline at end of file diff --git a/worker/thrift_server.py b/worker/thrift_server.py index 24114c6..b4c03bd 100644 --- a/worker/thrift_server.py +++ b/worker/thrift_server.py @@ -1,6 +1,7 @@ +import atexit + from logger import logger -from worker.factory import EndpointFactory -from worker.helpers import EndpointCoordinator, EndpointBuilder +from worker.helpers import DroidCoordinator, DroidBuilder from worker.utils import get_config, get_public_hostname from tgen.droid_service.ttypes import ConnParams @@ -14,12 +15,12 @@ class DroidServiceHandler(object): def __init__(self): - self.coordinator = EndpointCoordinator() + self.coordinator = DroidCoordinator() def setup(self): logger.debug('Setting up droids') config = get_config() - builder = EndpointBuilder() + builder = DroidBuilder() for droid in config['droids']: if droid['port']: builder.set_port(droid['port']) @@ -54,19 +55,21 @@ def start_package(self, endpoint_id, package_name): def pre_server_start_log(self): logger.debug("{} droid(s) at your service".format( - self.coordinator.get_num_endpoints())) + self.coordinator.count())) def teardown(self): + logger.debug("Running teardown operations") self.coordinator.teardown() def start_server(): handler = DroidServiceHandler() handler.setup() + atexit.register(handler.teardown) processor = DroidService.Processor(handler) config = get_config() transport = TSocket.TServerSocket( - host=config['thrift_port'], + host=config['thrift_host'], port=int(config['thrift_port'])) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() diff --git a/worker/utils.py b/worker/utils.py index 1791e08..4c9f4fd 100644 --- a/worker/utils.py +++ b/worker/utils.py @@ -16,29 +16,32 @@ def is_open_port(port): return result != 0 # Implies port is being used -_HOSTNAME = None +class CachedGlobals: + HOSTNAME = None + CONFIG = None + + def get_public_hostname(): - if _HOSTNAME is None: + if CachedGlobals.HOSTNAME is None: try: resp = requests.get("http://169.254.169.254/latest/meta-data/public-host", timeout=(3, 2)) if resp.status == 200: - _HOSTNAME = resp.text + CachedGlobals.HOSTNAME = resp.text except ConnectTimeout: - _HOSTNAME = socket.gethostname() + CachedGlobals.HOSTNAME = socket.gethostname() - return _HOSTNAME + return CachedGlobals.HOSTNAME -_CONFIG = None def get_config(): - if _CONFIG is None: + if CachedGlobals.CONFIG is None: config_path = os.path.join(ROOT_DIR, 'worker', 'config.json') assert os.path.exists(config_path), "Config file not provided for worker" with open(config_path) as source: - _CONFIG = json.load(source) + CachedGlobals.CONFIG = json.load(source) - return deepcopy(_CONFIG) + return deepcopy(CachedGlobals.CONFIG) if __name__ == '__main__':