Skip to content

Commit

Permalink
Merge pull request #313 from SelfhostedPro/nightly
Browse files Browse the repository at this point in the history
Nightly
  • Loading branch information
SelfhostedPro authored Feb 22, 2021
2 parents 5b123a0 + cb6eb56 commit f2fb542
Show file tree
Hide file tree
Showing 7 changed files with 22,053 additions and 761 deletions.
49 changes: 37 additions & 12 deletions backend/api/routers/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi import APIRouter, Depends, HTTPException, WebSocket, status, Cookie
from fastapi import APIRouter, Depends, HTTPException, WebSocket, status, Cookie, WebSocketDisconnect
from typing import List
from websockets.exceptions import ConnectionClosedOK

from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError
Expand Down Expand Up @@ -100,8 +101,14 @@ async def logs(websocket: WebSocket, app_name: str, Authorize: AuthJWT = Depends
async for line in logs:
try:
await websocket.send_text(line)
except Exception as e:
return e
except ConnectionClosedOK as e:
await websocket.close(code=e.code)
break
except RuntimeError as e:
if e.args[0] == 'Cannot call "send" once a close message has been sent.':
break
else:
print(e)
else:
await websocket.close(code=status.WS_1011_INTERNAL_ERROR)

Expand Down Expand Up @@ -156,8 +163,15 @@ async def stats(websocket: WebSocket, app_name: str, Authorize: AuthJWT = Depend
}
try:
await websocket.send_text(json.dumps(full_stats))
except Exception as e:
return e

except ConnectionClosedOK as e:
await websocket.close(code=e.code)
break
except RuntimeError as e:
if e.args[0] == 'Cannot call "send" once a close message has been sent.':
break
else:
print(e)
else:
await websocket.close(code=status.WS_1011_INTERNAL_ERROR)

Expand Down Expand Up @@ -218,12 +232,23 @@ async def process_container(name, stats, websocket):

full_stats = {
"name": name,
"cpu_percent": cpu_percent,
"mem_current": mem_current,
"mem_total": mem_total,
"mem_percent": mem_percent,
"cpu_percent": round(cpu_percent,0),
"mem_current": round(mem_current, 0),
"mem_total": round(mem_total,0),
"mem_percent": round(mem_percent,0),
}
try:
await websocket.send_text(json.dumps(full_stats))
except Exception as e:
pass
if 'last_stats' in locals() and full_stats != last_stats:
last_stats = full_stats
await websocket.send_text(json.dumps(full_stats))
elif 'last_stats' not in locals():
last_stats = full_stats
await websocket.send_text(json.dumps(full_stats))
except ConnectionClosedOK as e:
await websocket.close(code=e.code)
break
except RuntimeError as e:
if e.args[0] == 'Cannot call "send" once a close message has been sent.':
break
else:
print(e)
2 changes: 1 addition & 1 deletion backend/api/utils/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ async def get_app_stats(app_name):
line, cpu_total, cpu_system
)
except KeyError as e:
print("error while getting new CPU stats: %r, falling back")
print(f"error while getting new CPU stats: {e}, falling back")
cpu_percent = await calculate_cpu_percent(line)

full_stats = {
Expand Down
2 changes: 1 addition & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dnspython==2.0.0
docker==4.3.1
docker-compose==1.27.3
email-validator==1.1.1
fastapi==0.61.0
fastapi==0.63.0
fastapi-jwt-auth==0.5.0
gunicorn==20.0.4
h11==0.9.0
Expand Down
Loading

0 comments on commit f2fb542

Please sign in to comment.