fix 热加载增加双重防抖
This commit is contained in:
parent
0e9e626ab6
commit
4e1be23317
@ -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,8 +57,22 @@ 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:
|
||||||
logger.info(f"插件 {pid} 文件修改,重新加载...")
|
# 防抖处理,通过计时器延迟加载
|
||||||
PluginManager().reload_plugin(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} 文件修改,重新加载...")
|
||||||
|
PluginManager().reload_plugin(pid)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"插件文件修改后重载出错:{str(e)}")
|
logger.error(f"插件文件修改后重载出错:{str(e)}")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user