fix: ComfyUI memory manager - only consider recent history (last 5min)
- Fix bug where old history entries were counted as recent activity - Added recent_threshold parameter (default 300s) - is_recent_history() now checks timestamp of last history item
This commit is contained in:
@@ -9,7 +9,7 @@ import logging
|
||||
import subprocess
|
||||
import sys
|
||||
import signal
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -19,13 +19,15 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_INTERVAL = 60
|
||||
DEFAULT_TIMEOUT = 600
|
||||
DEFAULT_RECENT_THRESHOLD = 300
|
||||
COMFYUI_URL = "http://localhost:8188"
|
||||
CONTAINER_NAME = "comfyui"
|
||||
|
||||
class ComfyUIMemoryManager:
|
||||
def __init__(self, interval=DEFAULT_INTERVAL, timeout=DEFAULT_TIMEOUT):
|
||||
def __init__(self, interval=DEFAULT_INTERVAL, timeout=DEFAULT_TIMEOUT, recent_threshold=DEFAULT_RECENT_THRESHOLD):
|
||||
self.interval = interval
|
||||
self.timeout = timeout
|
||||
self.recent_threshold = recent_threshold
|
||||
self.last_activity = datetime.now()
|
||||
self.running = True
|
||||
|
||||
@@ -68,15 +70,23 @@ class ComfyUIMemoryManager:
|
||||
pass
|
||||
return 0, 0
|
||||
|
||||
def get_history_count(self):
|
||||
def is_recent_history(self):
|
||||
try:
|
||||
response = requests.get(f"{COMFYUI_URL}/history?max_items=1", timeout=10)
|
||||
if response.status_code == 200:
|
||||
history = response.json()
|
||||
return len(history)
|
||||
if history:
|
||||
last_item = list(history.values())[0]
|
||||
prompt_time = last_item.get("prompt_time", 0)
|
||||
if prompt_time:
|
||||
last_run = datetime.fromtimestamp(prompt_time)
|
||||
age = (datetime.now() - last_run).total_seconds()
|
||||
if age < self.recent_threshold:
|
||||
self.last_activity = last_run
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return 0
|
||||
return False
|
||||
|
||||
def restart_container(self):
|
||||
logger.info("🔄 Redémarrage du container ComfyUI...")
|
||||
@@ -91,21 +101,20 @@ class ComfyUIMemoryManager:
|
||||
|
||||
def run(self):
|
||||
logger.info(f"🚀 ComfyUI Memory Manager démarré")
|
||||
logger.info(f" Vérification: {self.interval}s | Timeout inactivité: {self.timeout}s")
|
||||
logger.info(f" Vérification: {self.interval}s | Timeout: {self.timeout}s | Recent: {self.recent_threshold}s")
|
||||
|
||||
while self.running:
|
||||
try:
|
||||
vram_percent = self.get_vram_percent()
|
||||
running, pending = self.get_queue_status()
|
||||
history_count = self.get_history_count()
|
||||
has_recent_history = self.is_recent_history()
|
||||
|
||||
idle_seconds = (datetime.now() - self.last_activity).total_seconds()
|
||||
|
||||
if running > 0 or pending > 0:
|
||||
self.last_activity = datetime.now()
|
||||
status = "active"
|
||||
elif history_count > 0:
|
||||
self.last_activity = datetime.now()
|
||||
elif has_recent_history:
|
||||
status = "recent"
|
||||
else:
|
||||
status = "idle"
|
||||
@@ -143,6 +152,7 @@ def main():
|
||||
parser = argparse.ArgumentParser(description="ComfyUI Memory Manager")
|
||||
parser.add_argument("--interval", type=int, default=DEFAULT_INTERVAL)
|
||||
parser.add_argument("--timeout", type=int, default=DEFAULT_TIMEOUT)
|
||||
parser.add_argument("--recent", type=int, default=DEFAULT_RECENT_THRESHOLD)
|
||||
parser.add_argument("--debug", action="store_true")
|
||||
|
||||
args = parser.parse_args()
|
||||
@@ -150,7 +160,11 @@ def main():
|
||||
if args.debug:
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
|
||||
manager = ComfyUIMemoryManager(interval=args.interval, timeout=args.timeout)
|
||||
manager = ComfyUIMemoryManager(
|
||||
interval=args.interval,
|
||||
timeout=args.timeout,
|
||||
recent_threshold=args.recent
|
||||
)
|
||||
manager.run()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user