From ae8952dea7883df3f0c810271cf9fee3831d3bed Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Mon, 26 Nov 2012 14:25:05 -0500 Subject: [PATCH] Add support for version discovery to worker. The worker now supports a DISCOVER message that can be sent to it to discover which version of the JSON message format that it supports. The Gearman function name that it registers is now simply the hostname and no longer includes 'lbaas--' as a prefix. Change-Id: Ib34ae424e5461b3b1f8df601a6e7a1fd99c48c05 --- doc/worker/about.rst | 8 ++++---- libra/worker/controller.py | 15 +++++++++++++++ libra/worker/worker.py | 7 +------ tests/test_worker_controller.py | 7 +++++++ 4 files changed, 27 insertions(+), 10 deletions(-) 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)