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 docker
|
||||
import aiodocker
|
||||
from asyncio import sleep
|
||||
|
||||
router = APIRouter()
|
||||
client = docker.from_env()
|
||||
|
@ -44,25 +45,32 @@ async def commnd(server, command):
|
|||
|
||||
|
||||
@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)
|
||||
#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):
|
||||
container = await getContainer(server)
|
||||
for line in container.log(stdout=True, follow=True, tail=5000):
|
||||
if await request.is_disconnected():
|
||||
print("client disconnected!!!")
|
||||
break
|
||||
async def logStream(container, tail):
|
||||
async for line in container.log(stdout=True, follow=True, tail=tail):
|
||||
yield line
|
||||
yield '\n'
|
||||
|
||||
# @router.get("/server/{server}/stats")
|
||||
# async def stats(server: str):
|
||||
|
||||
# container = await getContainer(server)
|
||||
# return EventSourceResponse(container.stats(stream=True))
|
||||
@router.get("/server/{server}/stats")
|
||||
async def stats(server: str, request: Request, stream: bool = True, delay: int = 2):
|
||||
container = await getContainer(server)
|
||||
if stream:
|
||||
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):
|
||||
return await docker.containers.get(server)
|
Loading…
Reference in New Issue