Example #1
0
File: logs.py Project: yuanl/jina
        with open(self.filepath) as fp:
            fp.seek(0, 2)
            daemon_logger.success(f'{self.filepath} is ready for streaming')
            while websocket in self.active_clients:
                line = fp.readline()  # also possible to read an empty line
                if line:
                    payload = None
                    try:
                        payload = json.loads(line)
                    except json.decoder.JSONDecodeError:
                        daemon_logger.warning(f'JSON decode error on {line}')

                    if payload:
                        from websockets import ConnectionClosedOK
                        try:
                            await websocket.send_json(payload)
                        except ConnectionClosedOK:
                            break
                else:
                    await asyncio.sleep(0.1)

    async def on_disconnect(self, websocket: WebSocket, close_code: int) -> None:
        self.active_clients.remove(websocket)
        daemon_logger.info(f'{self.client_details} is disconnected')


# TODO: adding websocket in this way do not generate any docs
#  see: https://github.com/tiangolo/fastapi/issues/1983
router.add_websocket_route(path='/logstream/{workspace_id}/{log_id}',
                           endpoint=LogStreamingEndpoint)