Skip to content

Commit

Permalink
Dump result from vSphere Agents to JSON only if *no* helper was used
Browse files Browse the repository at this point in the history
* The vPoller Helpers should be used when we need to perform
  any data translation, e.g. terminating data for C clients
  • Loading branch information
dnaeon committed Sep 12, 2014
1 parent 6cff378 commit f9ae086
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/vpoller/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,6 @@ def run_helper(self, helper, msg, data):
data (dict): The data to be processed
"""
if helper not in self.helpers:
return data

logging.debug(
'Invoking helper module %s for processing of data',
helper
Expand Down Expand Up @@ -448,26 +445,33 @@ def wait_for_tasks(self):
result = self.process_client_msg(msg)

# Process data using a helper before sending it to client?
if 'helper' in msg:
r = self.run_helper(
if 'helper' in msg and msg['helper'] in self.helpers:
data = self.run_helper(
helper=msg['helper'],
msg=msg,
data=result
)
result = r

else:
# No helper specified, dump data to JSON
try:
data = json.dumps(result, ensure_ascii=False)
except ValueError as e:
logging.warning('Cannot serialize result: %s', e)
r = {
'success': 1,
'msg': 'Cannot serialize result: %s' % e
}
data = json.dumps(r)

# Send data to client
self.worker_socket.send(_id, zmq.SNDMORE)
self.worker_socket.send(_empty, zmq.SNDMORE)
try:
# Add a NULL terminator at the end of the result
# so that C clients can properly get the data we send
data = json.dumps(result, ensure_ascii=False)
data += '\0'
self.worker_socket.send_unicode(data)
except TypeError as e:
logging.warning('Cannot serialize result: %s', e)
r = {'success': 1, 'msg': 'Cannot serialize result: %s' % e}
self.worker_socket.send_json(r)
logging.warning('Cannot send result: %s', e)
r = {'success': 1, 'msg': 'Cannot send result: %s' % e}
self.worker_socket.send_unicode(json.dumps(r))

def create_sockets(self):
"""
Expand Down

0 comments on commit f9ae086

Please sign in to comment.