Ejemplo n.º 1
0
async def interpreter(redisUrl, redisPassword, redisUser, args):
    client = RedisClient(redisUrl, redisPassword, redisUser)
    await client.connect()

    quit = False

    while not quit:
        printed = False

        if len(args) == 0:
            line = input('> ')
            args = line.split()
        else:
            quit = True

        cmd = args[0]
        cmd = cmd.upper()

        if len(args) > 1:
            cmdArgs = args[1:]
            response = await client.send(cmd, *cmdArgs)
        else:
            response = await client.send(cmd)

        if not printed:
            print(response)

        if not quit:
            args = []

    client.close()
Ejemplo n.º 2
0
async def waitForAllConnectionsToBeReady(url, password, user, timeout: int):
    start = time.time()

    while True:
        sys.stderr.write('.')
        sys.stderr.flush()

        try:
            redis = RedisClient(url, password, user)
            await redis.connect()
            await redis.send('PING')
            redis.close()
            break
        except Exception as e:
            if time.time() - start > timeout:
                sys.stderr.write('\n')
                raise

            logging.warning(e)

            waitTime = 0.1
            await asyncio.sleep(waitTime)

    sys.stderr.write('\n')
Ejemplo n.º 3
0
async def redisSubscriber(
    client: RedisClient,
    stream: str,
    position: Optional[str],
    messageHandlerClass: RedisSubscriberMessageHandlerClass,  # noqa
    obj,
):
    messageHandler = messageHandlerClass(obj)

    logPrefix = f'subscriber[{stream}]: {client}'

    streamExists = False
    redisHost = client.host
    clientId = -1

    if client:
        # query the stream size
        try:
            streamExists = await client.send('EXISTS', stream)
            clientId = await client.send('CLIENT', 'ID', key=stream)
            redisHost = await getHostForKey(client, stream)
        except Exception as e:
            logging.error(f"{logPrefix} cannot retreive stream metadata: {e}")
            client = None

    initInfo = {
        'success': client is not None,
        'redis_node': redisHost,
        'redis_client_id': clientId,
        'stream_exists': streamExists,
        'stream_name': stream,
    }

    try:
        await messageHandler.on_init(initInfo)
    except Exception as e:
        logging.error(f'{logPrefix} cannot initialize message handler: {e}')
        client = None

    if client is None:
        return messageHandler

    # lastId = '0-0'
    lastId = '$' if position is None else position

    try:
        # wait for incoming events.
        while True:
            results = await client.send('XREAD', 'BLOCK', b'0', b'STREAMS',
                                        stream, lastId)

            results = results[stream.encode()]

            for result in results:
                lastId = result[0].decode()
                msg = result[1]
                data = msg[b'json']

                msgCksum = msg.get(b'sha1')
                if msgCksum is not None:
                    cksum = sha1(data).hexdigest().encode()
                    if cksum != msgCksum:
                        err = f'{lastId}: invalid xread msg cksum'
                        logging.error(err)
                        continue

                payloadSize = len(data)
                try:
                    msg = json.loads(data)
                except json.JSONDecodeError:
                    msgEncoded = base64.b64encode(data).decode()
                    err = f'{lastId}: malformed json: base64: {msgEncoded} raw: {data}'
                    logging.error(err)
                    continue

                ret = await messageHandler.handleMsg(msg, lastId, payloadSize)
                if not ret:
                    break

    except asyncio.CancelledError:
        messageHandler.log('Cancelling redis subscription')
        raise

    except Exception as e:
        messageHandler.log(e)
        backtrace = traceback.format_exc()
        messageHandler.log(
            f'{logPrefix} Generic Exception caught in {backtrace}')

    finally:
        messageHandler.log('Closing redis subscription')

        # When finished, close the connection.
        client.close()

        return messageHandler