|
1 | 1 | #! /usr/bin/env python3 |
| 2 | +import json |
2 | 3 | import logging |
3 | 4 | import os |
4 | 5 | import shutil |
|
12 | 13 | import uvicorn |
13 | 14 | from commonwealth.utils.apis import GenericErrorHandlingRoute |
14 | 15 | from commonwealth.utils.commands import run_command |
15 | | -from commonwealth.utils.general import delete_everything |
| 16 | +from commonwealth.utils.general import delete_everything, delete_everything_stream |
16 | 17 | from commonwealth.utils.logs import InterceptHandler, init_logger |
17 | 18 | from fastapi import FastAPI, HTTPException, status |
18 | | -from fastapi.responses import HTMLResponse |
| 19 | +from fastapi.responses import HTMLResponse, StreamingResponse |
19 | 20 | from fastapi_versioning import VersionedFastAPI, version |
20 | 21 | from loguru import logger |
21 | 22 |
|
@@ -257,6 +258,19 @@ def setup_ssh() -> None: |
257 | 258 | logger.info("SSH setup done") |
258 | 259 |
|
259 | 260 |
|
| 261 | +@app.post("/services/remove_log_stream", status_code=status.HTTP_200_OK) |
| 262 | +@version(1, 0) |
| 263 | +async def remove_log_services_stream(i_know_what_i_am_doing: bool = False) -> StreamingResponse: |
| 264 | + """Stream the deletion of log files, providing real-time updates about each file being deleted.""" |
| 265 | + check_what_i_am_doing(i_know_what_i_am_doing) |
| 266 | + |
| 267 | + async def generate(): |
| 268 | + async for info in delete_everything_stream(Path(LOG_FOLDER_PATH)): |
| 269 | + yield json.dumps(info) + "\n" |
| 270 | + |
| 271 | + return StreamingResponse(generate(), media_type="application/x-ndjson") |
| 272 | + |
| 273 | + |
260 | 274 | if __name__ == "__main__": |
261 | 275 | setup_ssh() |
262 | 276 | # Register ssh client and remove message from the following commands |
|
0 commit comments