From 85fcb4a6c862165677982268f88180024ac0f701 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 1 Jun 2025 20:54:25 +0200 Subject: [PATCH] Refactor hot reload system to improve event handling & monitor delete events as well --- .../PluginSystem/PluginFactory.HotReload.cs | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.HotReload.cs b/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.HotReload.cs index 4eb3b0c3..6cd8eb55 100644 --- a/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.HotReload.cs +++ b/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.HotReload.cs @@ -15,32 +15,11 @@ public static partial class PluginFactory LOG.LogInformation($"Start hot reloading plugins for path '{HOT_RELOAD_WATCHER.Path}'."); try { - var messageBus = Program.SERVICE_PROVIDER.GetRequiredService(); - HOT_RELOAD_WATCHER.IncludeSubdirectories = true; HOT_RELOAD_WATCHER.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName; HOT_RELOAD_WATCHER.Filter = "*.lua"; - HOT_RELOAD_WATCHER.Changed += async (_, args) => - { - var changeType = args.ChangeType.ToString().ToLowerInvariant(); - if (!await HOT_RELOAD_SEMAPHORE.WaitAsync(0)) - { - LOG.LogInformation($"File changed ({changeType}): {args.FullPath}. Already processing another change."); - return; - } - - try - { - LOG.LogInformation($"File changed ({changeType}): {args.FullPath}. Reloading plugins..."); - await LoadAll(); - await messageBus.SendMessage(null, Event.PLUGINS_RELOADED); - } - finally - { - HOT_RELOAD_SEMAPHORE.Release(); - } - }; - + HOT_RELOAD_WATCHER.Changed += HotReloadEventHandler; + HOT_RELOAD_WATCHER.Deleted += HotReloadEventHandler; HOT_RELOAD_WATCHER.EnableRaisingEvents = true; } catch (Exception e) @@ -52,4 +31,32 @@ public static partial class PluginFactory LOG.LogInformation("Hot reloading plugins set up."); } } + + private static async void HotReloadEventHandler(object _, FileSystemEventArgs args) + { + try + { + var changeType = args.ChangeType.ToString().ToLowerInvariant(); + if (!await HOT_RELOAD_SEMAPHORE.WaitAsync(0)) + { + LOG.LogInformation($"File changed ({changeType}): {args.FullPath}. Already processing another change."); + return; + } + + try + { + LOG.LogInformation($"File changed ({changeType}): {args.FullPath}. Reloading plugins..."); + await LoadAll(); + await MessageBus.INSTANCE.SendMessage(null, Event.PLUGINS_RELOADED); + } + finally + { + HOT_RELOAD_SEMAPHORE.Release(); + } + } + catch (Exception e) + { + LOG.LogError(e, $"Error while handling hot reload event for file '{args.FullPath}' with change type '{args.ChangeType}'."); + } + } } \ No newline at end of file