Merge pull request #2051 from InfinityPacer/main

This commit is contained in:
jxxghp 2024-05-11 06:25:50 +08:00 committed by GitHub
commit 4186613a86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,7 @@
import concurrent import concurrent
import concurrent.futures import concurrent.futures
import os import os
import threading
import time import time
import traceback import traceback
from typing import List, Any, Dict, Tuple, Optional from typing import List, Any, Dict, Tuple, Optional
@ -25,8 +26,14 @@ from app.utils.system import SystemUtils
class PluginMonitorHandler(FileSystemEventHandler): class PluginMonitorHandler(FileSystemEventHandler):
# 计时器
__reload_timer = None
# 防抖时间间隔
__debounce_interval = 0.5
# 最近一次修改时间
__last_modified = 0 __last_modified = 0
__timeout = 1 # 修改间隔
__timeout = 2
def on_modified(self, event): def on_modified(self, event):
""" """
@ -50,6 +57,20 @@ class PluginMonitorHandler(FileSystemEventHandler):
if line.startswith("class") and "(_PluginBase)" in line: if line.startswith("class") and "(_PluginBase)" in line:
pid = line.split("class ")[1].split("(_PluginBase)")[0] pid = line.split("class ")[1].split("(_PluginBase)")[0]
if pid: if pid:
# 防抖处理,通过计时器延迟加载
if self.__reload_timer:
self.__reload_timer.cancel()
self.__reload_timer = threading.Timer(self.__debounce_interval, self.__reload_plugin, [pid])
self.__reload_timer.start()
except Exception as e:
logger.error(f"插件文件修改后重载出错:{str(e)}")
@staticmethod
def __reload_plugin(pid):
"""
重新加载插件
"""
try:
logger.info(f"插件 {pid} 文件修改,重新加载...") logger.info(f"插件 {pid} 文件修改,重新加载...")
PluginManager().reload_plugin(pid) PluginManager().reload_plugin(pid)
except Exception as e: except Exception as e: