Files
MoviePilot/app/scheduler/maintenance.py
DDSRem 2de976f6b3 Merge pull request #7050 from DDSRem-Dev/codex-comment-migration
docs: 统一全项目中文注释和结构化契约说明
2026-10-17 19:26:29 +08:00

60 lines
1.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
调度器内置维护任务。
"""
import gc
from app.application.backup import BackupArtifact
from app.application.database import get_database_governance
from app.runtime.gc import get_memory_usage, release_allocator_memory
from app.runtime.log import logger
from app.scheduler.contract import _SchedulerOwnerBase
class SchedulerMaintenanceOwner(_SchedulerOwnerBase):
"""
调度器内置维护任务。
"""
@staticmethod
def database_backup() -> BackupArtifact:
"""
按当前宿主策略创建一次定时数据库备份。
"""
return get_database_governance().create_backup()
def clear_cache(self) -> None:
"""
清理缓存
"""
self._scheduler_services().clear_cache()
@staticmethod
def full_gc() -> None:
"""
主动内存回收
"""
memory_before = get_memory_usage()
collected = gc.collect()
# gc 只把对象还给分配器,还需让分配器把空闲页交还系统,RSS 才会真正下降
allocator = release_allocator_memory()
memory_after = get_memory_usage()
memory_freed = memory_before - memory_after
logger.info(
f"主动内存回收完成,回收对象数: {collected},分配器归还: {allocator or '未执行'},"
f"释放内存: {memory_freed:.2f} MB"
)
@staticmethod
async def agent_heartbeat() -> None:
"""
智能体心跳唤醒:检查并执行待处理的定时任务
"""
from app.application.agent import get_running_agent_manager
manager = get_running_agent_manager()
if manager is None:
logger.debug("智能助手服务未运行,跳过心跳任务")
return
await manager.heartbeat_check_jobs()