shenchucheng
commited on
Commit
•
629e5d0
1
Parent(s):
8fc1895
add ttl for storage
Browse files
app.py
CHANGED
@@ -4,9 +4,11 @@ from __future__ import annotations
|
|
4 |
|
5 |
import asyncio
|
6 |
import contextlib
|
|
|
7 |
import pathlib
|
8 |
import re
|
9 |
import shutil
|
|
|
10 |
import traceback
|
11 |
import uuid
|
12 |
from collections import deque
|
@@ -274,6 +276,40 @@ app.mount(
|
|
274 |
set_llm_stream_logfunc(llm_stream_log)
|
275 |
|
276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
def main():
|
278 |
server_config = CONFIG.get("SERVER_UVICORN", {})
|
279 |
uvicorn.run(app="__main__:app", **server_config)
|
|
|
4 |
|
5 |
import asyncio
|
6 |
import contextlib
|
7 |
+
import os
|
8 |
import pathlib
|
9 |
import re
|
10 |
import shutil
|
11 |
+
import time
|
12 |
import traceback
|
13 |
import uuid
|
14 |
from collections import deque
|
|
|
276 |
set_llm_stream_logfunc(llm_stream_log)
|
277 |
|
278 |
|
279 |
+
def gen_file_modified_time(folder_path):
|
280 |
+
yield os.path.getmtime(folder_path)
|
281 |
+
for root, _, files in os.walk(folder_path):
|
282 |
+
for file in files:
|
283 |
+
file_path = os.path.join(root, file)
|
284 |
+
yield os.path.getmtime(file_path)
|
285 |
+
|
286 |
+
|
287 |
+
async def clear_storage(ttl: float = 1800):
|
288 |
+
storage = pathlib.Path(CONFIG.get("LOCAL_ROOT", "storage"))
|
289 |
+
logger.info("task `clear_storage` start running")
|
290 |
+
|
291 |
+
while True:
|
292 |
+
current_time = time.time()
|
293 |
+
for i in os.listdir(storage):
|
294 |
+
i = storage / i
|
295 |
+
try:
|
296 |
+
last_time = max(gen_file_modified_time(i))
|
297 |
+
if current_time - last_time > ttl:
|
298 |
+
shutil.rmtree(i)
|
299 |
+
await asyncio.sleep(0)
|
300 |
+
logger.info(f"Deleted directory: {i}")
|
301 |
+
except Exception:
|
302 |
+
logger.exception(f"check {i} error")
|
303 |
+
await asyncio.sleep(60)
|
304 |
+
|
305 |
+
|
306 |
+
@app.on_event("startup")
|
307 |
+
async def startup_event():
|
308 |
+
# 运行在服务启动后立即执行的任务
|
309 |
+
loop = asyncio.get_running_loop()
|
310 |
+
loop.create_task(clear_storage())
|
311 |
+
|
312 |
+
|
313 |
def main():
|
314 |
server_config = CONFIG.get("SERVER_UVICORN", {})
|
315 |
uvicorn.run(app="__main__:app", **server_config)
|