Dispatch: support multiple targets for a key

This commit is contained in:
Brian S. Stephan 2013-05-03 16:01:45 -05:00
parent f09a41bce0
commit 88e470183b
1 changed files with 12 additions and 12 deletions

View File

@ -88,17 +88,17 @@ class Dispatch(Module):
message the message to send to the channel message the message to send to the channel
Returns: Returns:
tuple of strings: the destination channel, the string sent to it list of all notified targets
""" """
key, dest = self._get_key_and_destination(key) notified = []
for key, dest in self._get_key_and_destination(key):
if key is not None and dest is not None:
msg = "[{0:s}] {1:s}".format(key, message.encode('utf-8', 'ignore')) msg = "[{0:s}] {1:s}".format(key, message.encode('utf-8', 'ignore'))
self.sendmsg(dest, msg) self.sendmsg(dest, msg)
notified.append(dest)
return dest, msg return notified
def _get_key_and_destination(self, key): def _get_key_and_destination(self, key):
"""Retrieve the configured channel for the given key. """Retrieve the configured channel for the given key.
@ -107,11 +107,11 @@ class Dispatch(Module):
key the type of message. this will be converted uppercase key the type of message. this will be converted uppercase
Returns: Returns:
tuple of strings: the key (from the db), the destination channel list of tuple of strings: (the key, the destination channel)
""" """
channel = None targets = []
db = self.get_db() db = self.get_db()
try: try:
cur = db.cursor(mdb.cursors.DictCursor) cur = db.cursor(mdb.cursors.DictCursor)
@ -120,11 +120,11 @@ class Dispatch(Module):
WHERE key_ = %s WHERE key_ = %s
''' '''
cur.execute(query, (key.upper(),)) cur.execute(query, (key.upper(),))
result = cur.fetchone() results = cur.fetchall()
if result: for result in results:
return result['key_'], result['dest'] targets.append((result['key_'], result['dest']))
else:
return None, None return targets
except mdb.Error as e: except mdb.Error as e:
self.log.error("database error while getting destination") self.log.error("database error while getting destination")
self.log.exception(e) self.log.exception(e)