2024-12-12 20:04:36 +08:00
|
|
|
# app/tasks/cleanup_task.py
|
|
|
|
import asyncio
|
|
|
|
import time
|
|
|
|
from app.services.stream_manager import stream_manager, stop_stream
|
|
|
|
from app.utils.logger import get_logger
|
2024-12-13 18:21:25 +08:00
|
|
|
from app.config import expired_timeout, cleanup_interval
|
2024-12-12 20:04:36 +08:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
async def cleanup_streams():
|
|
|
|
while True:
|
2024-12-13 18:21:25 +08:00
|
|
|
await asyncio.sleep(cleanup_interval)
|
2024-12-12 20:04:36 +08:00
|
|
|
now = int(time.time() * 1000)
|
|
|
|
logger.info("定时任务执行:清理空闲流")
|
2024-12-16 14:31:28 +08:00
|
|
|
print(stream_manager)
|
2024-12-12 20:04:36 +08:00
|
|
|
for stream_id, stream in list(stream_manager.items()):
|
2024-12-13 18:21:25 +08:00
|
|
|
if now - stream["create_time"] > expired_timeout:
|
2024-12-12 20:04:36 +08:00
|
|
|
stop_stream(stream_id)
|
|
|
|
logger.info(f"清理空闲流:{stream_id}")
|