feat(bot): 重构定时任务逻辑并优化日志配置

- 将 `BlockingScheduler` 替换为 `BackgroundScheduler`
- 将 `clean_temp_cache` 函数移至 `bot.py` 并优化文件清理逻辑
- 调整日志文件路径为 `log_path + "error.log"`
- 移除 `platform.py` 中冗余的缓存清理函数
This commit is contained in:
SlyAimer 2025-03-03 14:00:40 +08:00
parent d6116f7b2c
commit 3a7151bb6d
2 changed files with 24 additions and 22 deletions

30
bot.py
View file

@ -1,23 +1,39 @@
import os
import glob
import logging
import nonebot import nonebot
from nonebot.adapters.qq import Adapter as QQAdapter from pathlib import Path
from nonebot import logger from nonebot import logger
from nonebot.log import default_format from nonebot.log import default_format
from apscheduler.schedulers.blocking import BlockingScheduler from nonebot.adapters.qq import Adapter as QQAdapter
from src.configs.path_config import log_path from apscheduler.schedulers.background import BackgroundScheduler
from src.plugins.platform import clean_temp_cache from src.configs.path_config import log_path,temp_path,video_path
nonebot.init() nonebot.init()
driver = nonebot.get_driver() driver = nonebot.get_driver()
driver.register_adapter(QQAdapter) # 注册QQ适配器 driver.register_adapter(QQAdapter) # 注册QQ适配器
nonebot.load_from_toml("pyproject.toml") nonebot.load_from_toml("pyproject.toml")
logger.add(log_path, level="ERROR", format=default_format, rotation="1 week") logger.add(log_path+"error.log", level="ERROR", format=default_format, rotation="1 week")
from src.clover_sqlite.data_init.db_connect import disconnect, init from src.clover_sqlite.data_init.db_connect import disconnect, init
driver.on_startup(init) driver.on_startup(init)
driver.on_shutdown(disconnect) driver.on_shutdown(disconnect)
scheduler = BlockingScheduler()
def clean_temp_cache():
"""定时清理缓存文件"""
path_list = [Path(temp_path), Path(video_path)]
print("开始清理文件")
for folder_path in path_list:
files = get_files_in_folder(folder_path)
for file in files:
os.remove(file)
def get_files_in_folder(folder_path: Path):
return [Path(f) for f in glob.glob(str(folder_path / "*")) if Path(f).is_file()]
scheduler = BackgroundScheduler()
scheduler.add_job(clean_temp_cache, 'cron', hour=0, minute=0) scheduler.add_job(clean_temp_cache, 'cron', hour=0, minute=0)
scheduler.start()
if __name__ == "__main__": if __name__ == "__main__":
scheduler.start()
nonebot.run() nonebot.run()

View file

@ -1,14 +1,11 @@
import os
import glob
import time import time
import psutil import psutil
import platform import platform
import logging
from pathlib import Path from pathlib import Path
from nonebot.rule import to_me from nonebot.rule import to_me
from nonebot.plugin import on_command from nonebot.plugin import on_command
from nonebot.adapters.qq import Message, MessageSegment from nonebot.adapters.qq import Message, MessageSegment
from src.configs.path_config import temp_path,video_path
repository = on_command("repo", rule=to_me(), priority=10, block=True) repository = on_command("repo", rule=to_me(), priority=10, block=True)
@ -46,15 +43,4 @@ async def get_platform_info():
await platform_info.finish(content) await platform_info.finish(content)
def clean_temp_cache():
"""定时清理缓存文件"""
path_list = [temp_path, video_path]
for folder_path in path_list:
files = get_files_in_folder(folder_path)
for file in files:
os.remove(file)
def get_files_in_folder(folder_path: Path):
return [Path(f) for f in glob.glob(str(folder_path / "*")) if Path(f).is_file()]