Implement logs and stats
This commit is contained in:
parent
18ef42023f
commit
0c2af63888
|
@ -3,6 +3,7 @@ from sse_starlette.sse import EventSourceResponse
|
||||||
import base64
|
import base64
|
||||||
import docker
|
import docker
|
||||||
import aiodocker
|
import aiodocker
|
||||||
|
from asyncio import sleep
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
|
@ -44,25 +45,32 @@ async def commnd(server, command):
|
||||||
|
|
||||||
|
|
||||||
@router.get("/server/{server}/logs")
|
@router.get("/server/{server}/logs")
|
||||||
async def logs(server: str, request: Request):
|
async def logs(server: str, request: Request, follow: bool = True, tail: int = 1000):
|
||||||
container = await getContainer(server)
|
container = await getContainer(server)
|
||||||
#event_generator = logGenerator(request, server)
|
#event_generator = logGenerator(request, server)
|
||||||
return EventSourceResponse(container.log(stdout=True, follow=True, tail=5000))
|
if follow:
|
||||||
|
return EventSourceResponse(logStream(container, tail))
|
||||||
|
else:
|
||||||
|
return await container.log(stdout=True, follow=False, tail=tail)
|
||||||
|
|
||||||
async def logGenerator(request, server):
|
async def logStream(container, tail):
|
||||||
container = await getContainer(server)
|
async for line in container.log(stdout=True, follow=True, tail=tail):
|
||||||
for line in container.log(stdout=True, follow=True, tail=5000):
|
|
||||||
if await request.is_disconnected():
|
|
||||||
print("client disconnected!!!")
|
|
||||||
break
|
|
||||||
yield line
|
yield line
|
||||||
|
yield '\n'
|
||||||
|
|
||||||
# @router.get("/server/{server}/stats")
|
@router.get("/server/{server}/stats")
|
||||||
# async def stats(server: str):
|
async def stats(server: str, request: Request, stream: bool = True, delay: int = 2):
|
||||||
|
container = await getContainer(server)
|
||||||
# container = await getContainer(server)
|
if stream:
|
||||||
# return EventSourceResponse(container.stats(stream=True))
|
return EventSourceResponse(statStream(request, container, delay))
|
||||||
|
return await container.stats(stream=False)
|
||||||
|
|
||||||
|
async def statStream(request, container, delay):
|
||||||
|
while True:
|
||||||
|
yield await container.stats(stream=False)
|
||||||
|
_delay = delay - 1 if delay else 0
|
||||||
|
print(_delay)
|
||||||
|
await sleep(_delay)
|
||||||
|
|
||||||
async def getContainer(server):
|
async def getContainer(server):
|
||||||
return await docker.containers.get(server)
|
return await docker.containers.get(server)
|
Loading…
Reference in New Issue