def update_server(): logger.info('Update server information') r = redis_monitor() if not r: logger.error('Can not connect monitor redis') return name = 'PUSH:' + app.SERVICE_NAME info = {'ip': app.HOST, 'port': app.PORT} pipe = r.pipeline() pipe.zadd('AVAIL:PUSH', app.SERVICE_NAME, 0) pipe.hmset(name, info) pipe.expire(name, app.UPDATE_INTERVAL) pipe.execute()
def update_server(): logger.info('Update server information') r = redis_monitor() if not r: logger.error('Can not connect monitor redis') return name = 'MEMBERSHIP:' + app.SERVICE_NAME info = {'ip': app.HOST, 'port': app.PORT, 'protocol_version': app.PROTOCOL_VERSION} pipe = r.pipeline() pipe.zadd('AVAIL:MEMBERSHIP', app.SERVICE_NAME, 0) pipe.hmset(name, info) pipe.expire(name, app.UPDATE_INTERVAL) pipe.execute()
def push_list(): servers = [] r = redis_monitor() count = r.zcard('AVAIL:PUSH') if count > 0: scores = r.zrange('AVAIL:PUSH', 0, count - 1, withscores=True) for ps in scores: name = 'PUSH:' + ps[0] fields = r.hmget(name, 'ip', 'port') if not fields[0]: r.zrem('AVAIL:PUSH', ps[0]) continue servers.append({'ip':fields[0], 'port':fields[1]}) push_list = [] for server in servers: url = 'http://%s:%s/api/monitor' % (server['ip'], server['port']) r = requests.get(url) push_list.extend(json.loads(r.content)) return push_list
def get_pushs(): pushs = {} r = redis_monitor() count = r.zcard('AVAIL:PUSH') if count > 0: scores = r.zrange('AVAIL:PUSH', 0, count - 1, withscores=True) for ps in scores: push = {'name': ps[0], 'usage': ps[1]} name = 'PUSH:' + ps[0] fields = r.hmget(name, 'ip', 'port') if not fields[0]: r.zrem('AVAIL:PUSH', ps[0]) continue push['ip'] = fields[0] push['port'] = fields[1] pushs[ps[0]] = push return pushs
def get_memberships(): memberships = {} r = redis_monitor() count = r.zcard('AVAIL:MEMBERSHIP') if count > 0: scores = r.zrange('AVAIL:MEMBERSHIP', 0, count - 1, withscores=True) for ms in scores: membership = {'name': ms[0], 'usage': ms[1]} name = 'MEMBERSHIP:' + ms[0] fields = r.hmget(name, 'ip', 'port', 'protocol_version') if not fields[0]: r.zrem('AVAIL:MEMBERSHIP', ms[0]) continue membership['ip'] = fields[0] membership['port'] = fields[1] membership['protocol_version'] = fields[2] memberships[ms[0]] = membership return memberships
def get_monitor(): from config import REDIS_MONITOR r = redis_monitor() info = r.info() info.update(REDIS_MONITOR) return info