fix 异步定时服务

This commit is contained in:
jxxghp
2025-08-25 21:19:07 +08:00
parent 91a124ab8f
commit 6379cda148
4 changed files with 12 additions and 14 deletions
+2 -2
View File
@@ -25,7 +25,7 @@ from app.log import logger
from app.schemas import ConfigChangeEventData from app.schemas import ConfigChangeEventData
from app.schemas import FileItem from app.schemas import FileItem
from app.schemas.types import SystemConfigKey, EventType from app.schemas.types import SystemConfigKey, EventType
from app.utils.singleton import Singleton from app.utils.singleton import SingletonClass
lock = Lock() lock = Lock()
snapshot_lock = Lock() snapshot_lock = Lock()
@@ -54,7 +54,7 @@ class FileMonitorHandler(FileSystemEventHandler):
file_size=Path(event.dest_path).stat().st_size) file_size=Path(event.dest_path).stat().st_size)
class Monitor(metaclass=Singleton): class Monitor(metaclass=SingletonClass):
""" """
目录监控处理链,单例模式 目录监控处理链,单例模式
""" """
+2 -2
View File
@@ -78,7 +78,7 @@ class FastAPIMonitor:
# 告警状态 # 告警状态
self.alerts: List[str] = [] self.alerts: List[str] = []
logger.info("FastAPI性能监控器已初始化") logger.debug("FastAPI性能监控器已初始化")
def record_request(self, request: Request, response: Response, response_time: float): def record_request(self, request: Request, response: Response, response_time: float):
""" """
@@ -172,7 +172,7 @@ class FastAPIMonitor:
'count': 0, 'count': 0,
'total_time': 0, 'total_time': 0,
'errors': 0, 'errors': 0,
'avg_time': 0 'avg_time': 0.0
}) })
for req in self.request_history: for req in self.request_history:
+6 -8
View File
@@ -23,13 +23,13 @@ from app.core.config import settings
from app.core.event import eventmanager, Event from app.core.event import eventmanager, Event
from app.core.plugin import PluginManager from app.core.plugin import PluginManager
from app.db.systemconfig_oper import SystemConfigOper from app.db.systemconfig_oper import SystemConfigOper
from app.helper.message import MessageHelper
from app.helper.sites import SitesHelper # noqa from app.helper.sites import SitesHelper # noqa
from app.helper.message import MessageHelper
from app.helper.wallpaper import WallpaperHelper from app.helper.wallpaper import WallpaperHelper
from app.log import logger from app.log import logger
from app.schemas import Notification, NotificationType, Workflow, ConfigChangeEventData from app.schemas import Notification, NotificationType, Workflow, ConfigChangeEventData
from app.schemas.types import EventType, SystemConfigKey from app.schemas.types import EventType, SystemConfigKey
from app.utils.singleton import Singleton from app.utils.singleton import SingletonClass
from app.utils.timer import TimerUtils from app.utils.timer import TimerUtils
lock = threading.Lock() lock = threading.Lock()
@@ -39,7 +39,7 @@ class SchedulerChain(ChainBase):
pass pass
class Scheduler(metaclass=Singleton): class Scheduler(metaclass=SingletonClass):
""" """
定时任务管理 定时任务管理
""" """
@@ -57,6 +57,8 @@ class Scheduler(metaclass=Singleton):
self._auth_count = 0 self._auth_count = 0
# 用户认证失败消息发送 # 用户认证失败消息发送
self._auth_message = False self._auth_message = False
# 当前事件循环
self.loop = asyncio.get_event_loop()
self.init() self.init()
@eventmanager.register(EventType.ConfigChanged) @eventmanager.register(EventType.ConfigChanged)
@@ -454,11 +456,7 @@ class Scheduler(metaclass=Singleton):
""" """
启动协程 启动协程
""" """
try: return asyncio.run_coroutine_threadsafe(coro, self.loop)
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.get_event_loop()
return asyncio.run_coroutine_threadsafe(coro, loop)
# 获取定时任务 # 获取定时任务
job = self.__prepare_job(job_id) job = self.__prepare_job(job_id)
+2 -2
View File
@@ -35,10 +35,10 @@ async def lifespan(app: FastAPI):
定义应用的生命周期事件 定义应用的生命周期事件
""" """
print("Starting up...") print("Starting up...")
# 初始化模块
init_modules()
# 初始化路由 # 初始化路由
init_routers(app) init_routers(app)
# 初始化模块
init_modules()
# 恢复插件备份 # 恢复插件备份
SystemChain().restore_plugins() SystemChain().restore_plugins()
# 初始化插件 # 初始化插件