Skip to content

Commit

Permalink
Bring worker to working state
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalg0wda committed Feb 28, 2017
1 parent d6c7943 commit f0e6e37
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
4 changes: 4 additions & 0 deletions worker/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions worker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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)


15 changes: 9 additions & 6 deletions worker/thrift_server.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'])
Expand Down Expand Up @@ -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()
Expand Down
21 changes: 12 additions & 9 deletions worker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit f0e6e37

Please sign in to comment.