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-<VERSION>-' as a prefix.

Change-Id: Ib34ae424e5461b3b1f8df601a6e7a1fd99c48c05
This commit is contained in:
David Shrewsbury 2012-11-26 14:25:05 -05:00
parent 27b1393bec
commit ae8952dea7
4 changed files with 27 additions and 10 deletions

View File

@ -4,10 +4,10 @@ Description
Purpose Purpose
------- -------
A Python-based Gearman worker that handles work for the job queue named A Python-based Gearman worker that handles messages for the Gearman job queue
'lbaas-HOSTNAME'. It receives JSON data describing a load balancer, and sharing the same name as the local hostname. The messages that it receives are
returns this same JSON object, but with status fields added to describe JSON objects describing a load balancer, and returns this same JSON object, but
the state of the LB. with status fields added to describe the state of the LB.
Installation Installation
------------ ------------

View File

@ -55,6 +55,8 @@ class LBaaSController(object):
return self._action_enable() return self._action_enable()
elif action == 'DELETE': elif action == 'DELETE':
return self._action_delete() return self._action_delete()
elif action == 'DISCOVER':
return self._action_discover()
else: else:
self.logger.error("Invalid `%s` value: %s" % self.logger.error("Invalid `%s` value: %s" %
(self.ACTION_FIELD, action)) (self.ACTION_FIELD, action))
@ -66,6 +68,19 @@ class LBaaSController(object):
self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE self.msg[self.RESPONSE_FIELD] = self.RESPONSE_FAILURE
return self.msg 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): def _action_update(self):
""" """
Create/Update a Load Balancer. Create/Update a Load Balancer.

View File

@ -49,14 +49,9 @@ def handler(worker, job):
def config_thread(logger, driver, servers, reconnect_sleep): def config_thread(logger, driver, servers, reconnect_sleep):
""" Worker thread function. """ """ 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 should be a unique value, like UUID
hostname = socket.gethostname() hostname = socket.gethostname()
logger.info("[worker] Registering task %s" % hostname)
task_name = "lbaas-%s-%s" % (msg_fmt_version, hostname)
logger.info("[worker] Registering task %s" % task_name)
worker = CustomJSONGearmanWorker(servers) worker = CustomJSONGearmanWorker(servers)
worker.set_client_id(hostname) worker.set_client_id(hostname)

View File

@ -127,3 +127,10 @@ class TestWorkerController(unittest.TestCase):
response = controller.run() response = controller.run()
self.assertIn(c.RESPONSE_FIELD, response) self.assertIn(c.RESPONSE_FIELD, response)
self.assertEquals(response[c.RESPONSE_FIELD], c.RESPONSE_FAILURE) 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)