Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add opcode for notification of redis DB #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/swsssdk/configdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ def listen(self):
except ValueError:
pass #Ignore non table-formated redis entries

def __fire_with_op(self, table, key, data, op_str='add'):
if self.handlers.has_key(table):
handler = self.handlers[table]
handler(table, key, data, op_str)

def listen_with_op(self):
"""Start listen Redis keyspace events and will trigger corresponding handlers when content of a table changes.
"""
self.pubsub = self.redis_clients[self.CONFIG_DB].pubsub()
self.pubsub.psubscribe("__keyspace@{}__:*".format(self.db_map[self.CONFIG_DB]['db']))
for item in self.pubsub.listen():
if item['type'] == 'pmessage':
key = item['channel'].split(':', 1)[1]
try:
(table, row) = key.split(self.TABLE_NAME_SEPARATOR, 1)
if self.handlers.has_key(table):
client = self.redis_clients[self.CONFIG_DB]
data = self.__raw_to_typed(client.hgetall(key))
op = client.keys(key)
op_str = 'add'
if len(op) == 0:
op_str = 'del'
self.__fire_with_op(table, row, data, op_str)
except ValueError:
pass #Ignore non table-formated redis entries

def __raw_to_typed(self, raw_data):
if raw_data == None:
return None
Expand Down