import socket, time, json, base64

HOST = '10.24.8.82'

def R():
    s = socket.create_connection((HOST, 6379), 5); s.settimeout(10)
    return s

def cmd(s, *a, w=0.5):
    p = b'*%d\r\n' % len(a)
    for x in a:
        x = x.encode() if isinstance(x, str) else x
        p += b'$%d\r\n%s\r\n' % (len(x), x)
    s.sendall(p); time.sleep(w)
    o = b''
    try:
        while True:
            d = s.recv(65536)
            if not d: break
            o += d
            if len(d) < 65536: break
    except socket.timeout: pass
    return o.decode(errors='replace')

found = {}
deadline = time.time() + 1500
s = R()
while time.time() < deadline:
    for db in ['0', '1']:
        cmd(s, 'SELECT', db)
        keys = cmd(s, 'KEYS', '*', w=0.4)
        for k in ['unacked', 'unacked_index']:
            if k in keys and ('unacked', db) not in found:
                h = cmd(s, 'HGETALL', k, w=0.8)
                found[('unacked', db)] = True
                print('=== unacked in db%s ===' % db)
                print(h[:3000])
        import re
        for m in re.finditer(r'\$celeryev\.[a-z0-9-]+', keys):
            q = m.group(0)[1:]
            if ('evq', db, q) not in found:
                found[('evq', db, q)] = True
                lr = cmd(s, 'LRANGE', q, '0', '-1', w=0.8)
                if lr.strip() not in ('*0', '(empty list)') and len(lr) > 20:
                    print('=== events in %s (db%s) ===' % (q, db))
                    print(lr[:2500])
    time.sleep(2)
print('watch done, found:', len(found))
