diff --git a/doc/worker/about.rst b/doc/worker/about.rst index afed78a1..1079160d 100644 --- a/doc/worker/about.rst +++ b/doc/worker/about.rst @@ -4,10 +4,10 @@ Description Purpose ------- -A Python-based Gearman worker that handles work for the job queue named -'lbaas-HOSTNAME'. It receives JSON data describing a load balancer, and -returns this same JSON object, but with status fields added to describe -the state of the LB. +A Python-based Gearman worker that handles messages for the Gearman job queue +sharing the same name as the local hostname. The messages that it receives are +JSON objects describing a load balancer, and returns this same JSON object, but +with status fields added to describe the state of the LB. Installation ------------ diff --git a/libra/worker/controller.py b/libra/worker/controller.py index 3fa6cdfa..4bbc380c 100644 --- a/libra/worker/controller.py +++ b/libra/worker/controller.py @@ -55,6 +55,8 @@ class LBaaSController(object): return self._action_enable() elif action == 'DELETE': return self._action_delete() + elif action == 'DISCOVER': + return self._action_discover() else: self.logger.error("Invalid `%s` value: %s" % (self.ACTION_FIELD, action)) @@ -66,6 +68,19 @@ class LBaaSController(object): self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE return self.msg + def _action_discover(self): + """ + Return service discovery information. + + This message type is currently used to report which message + version this worker supports. + """ + # Version of the JSON message format that this worker understands. + msg_fmt_version = "1.0" + self.msg['version'] = msg_fmt_version + self.msg[self.RESPONSE_FIELD] = self.RESPONSE_SUCCESS + return self.msg + def _action_update(self): """ Create/Update a Load Balancer. diff --git a/libra/worker/worker.py b/libra/worker/worker.py index 3ed91a6e..c27d1cad 100644 --- a/libra/worker/worker.py +++ b/libra/worker/worker.py @@ -49,14 +49,9 @@ def handler(worker, job): def config_thread(logger, driver, servers, reconnect_sleep): """ Worker thread function. """ - # Version of the JSON message format that this worker understands. - msg_fmt_version = "1.0" - # Hostname should be a unique value, like UUID hostname = socket.gethostname() - - task_name = "lbaas-%s-%s" % (msg_fmt_version, hostname) - logger.info("[worker] Registering task %s" % task_name) + logger.info("[worker] Registering task %s" % hostname) worker = CustomJSONGearmanWorker(servers) worker.set_client_id(hostname) diff --git a/tests/test_worker_controller.py b/tests/test_worker_controller.py index 7aa32cc1..7868ce5d 100644 --- a/tests/test_worker_controller.py +++ b/tests/test_worker_controller.py @@ -127,3 +127,10 @@ class TestWorkerController(unittest.TestCase): response = controller.run() self.assertIn(c.RESPONSE_FIELD, response) self.assertEquals(response[c.RESPONSE_FIELD], c.RESPONSE_FAILURE) + + def testDiscover(self): + msg = { c.ACTION_FIELD: 'DISCOVER' } + controller = c(self.logger, self.driver, msg) + response = controller.run() + self.assertIn('version', response) + self.assertEquals(response[c.RESPONSE_FIELD], c.RESPONSE_SUCCESS)