From 55859158fbd0bf54473a0baeb486045a025c7c5d Mon Sep 17 00:00:00 2001 From: vnugent Date: Sun, 24 Mar 2024 21:01:06 -0400 Subject: Squashed commit of the following: commit 6c1667be23597513537f8190e2f55d65eb9b7c7a Author: vnugent Date: Fri Mar 22 12:01:53 2024 -0400 refactor: Overhauled native library loading and lazy init commit ebf688f2f974295beabf7b5def7e6f6f150551d0 Author: vnugent Date: Wed Mar 20 22:16:17 2024 -0400 refactor: Update compression header files and macros + Ci build commit 9c7b564911080ccd5cbbb9851a0757b05e1e9047 Author: vnugent Date: Tue Mar 19 21:54:49 2024 -0400 refactor: JWK overhaul & add length getter to FileUpload commit 6d8c3444e09561e5957491b3cc1ae858e0abdd14 Author: vnugent Date: Mon Mar 18 16:13:20 2024 -0400 feat: Add FNV1a software checksum and basic correction tests commit 00d182088cecefc08ca80b1faee9bed3f215f40b Author: vnugent Date: Fri Mar 15 01:05:27 2024 -0400 chore: #6 Use utils filewatcher instead of built-in commit d513c10d9895c6693519ef1d459c6a5a76929436 Author: vnugent Date: Sun Mar 10 21:58:14 2024 -0400 source tree project location updated --- lib/Plugins.Runtime/src/AssemblyWatcher.cs | 91 +++++++------------------- lib/Plugins.Runtime/src/PluginStackBuilder.cs | 6 +- lib/Plugins.Runtime/src/RuntimePluginLoader.cs | 17 +++-- 3 files changed, 35 insertions(+), 79 deletions(-) (limited to 'lib/Plugins.Runtime') diff --git a/lib/Plugins.Runtime/src/AssemblyWatcher.cs b/lib/Plugins.Runtime/src/AssemblyWatcher.cs index 09b49dc..69c53ed 100644 --- a/lib/Plugins.Runtime/src/AssemblyWatcher.cs +++ b/lib/Plugins.Runtime/src/AssemblyWatcher.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Runtime @@ -22,95 +22,52 @@ * along with VNLib.Plugins.Runtime. If not, see http://www.gnu.org/licenses/. */ +using System; using System.IO; using System.Threading; -using System.Collections.Generic; using VNLib.Utils; +using VNLib.Utils.IO; using VNLib.Utils.Extensions; -namespace VNLib.Plugins.Runtime -{ - internal sealed class AssemblyWatcher : IPluginAssemblyWatcher - { - private readonly object _lock = new (); - private readonly Dictionary _watchers; - public AssemblyWatcher() - { - _watchers = new(); - } +namespace VNLib.Plugins.Runtime +{ - /// - public void StopWatching(IPluginReloadEventHandler handler) - { - lock (_lock) - { - //Find old watcher by its handler, then dispose it - if (_watchers.Remove(handler, out AsmFileWatcher? watcher)) - { - //dispose the watcher - watcher.Dispose(); - } - } - } + internal static class AssemblyWatcher + { - /// - public void WatchAssembly(IPluginReloadEventHandler handler, IPluginAssemblyLoader loader) + internal static IDisposable WatchAssembly(IPluginReloadEventHandler handler, IPluginAssemblyLoader loader) { - lock(_lock) - { - if(_watchers.Remove(handler, out AsmFileWatcher? watcher)) - { - //dispose the watcher - watcher.Dispose(); - } + ArgumentNullException.ThrowIfNull(handler); + ArgumentNullException.ThrowIfNull(loader); - //Queue up a new watcher - watcher = new(loader, handler); + DebouncedFSEventHandler dbh = new(loader, handler); + FileWatcher.Subscribe(loader.Config.AssemblyFile, dbh); - //Store watcher - _watchers.Add(handler, watcher); - } + return dbh; } - private sealed class AsmFileWatcher : VnDisposeable + internal sealed class DebouncedFSEventHandler : VnDisposeable, IFSChangeHandler { - public IPluginReloadEventHandler Handler { get; } + private readonly IPluginReloadEventHandler _handler; private readonly IPluginAssemblyLoader _loaderSource; private readonly Timer _delayTimer; - private readonly FileSystemWatcher _watcher; private bool _pause; - public AsmFileWatcher(IPluginAssemblyLoader LoaderSource, IPluginReloadEventHandler handler) + public DebouncedFSEventHandler(IPluginAssemblyLoader loader, IPluginReloadEventHandler handler) { - Handler = handler; - _loaderSource = LoaderSource; - - string dir = Path.GetDirectoryName(LoaderSource.Config.AssemblyFile)!; - - //Configure watcher to notify only when the assembly file changes - _watcher = new FileSystemWatcher(dir) - { - Filter = "*.dll", - EnableRaisingEvents = false, - IncludeSubdirectories = true, - NotifyFilter = NotifyFilters.LastWrite, - }; - - //Configure listener - _watcher.Changed += OnFileChanged; - _watcher.Created += OnFileChanged; - - _watcher.EnableRaisingEvents = true; + _handler = handler; + _loaderSource = loader; //setup delay timer to wait on the config - _delayTimer = new(OnTimeout, this, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); + _delayTimer = new(OnTimeout, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); } - void OnFileChanged(object sender, FileSystemEventArgs e) + /// + void IFSChangeHandler.OnFileChanged(FileSystemEventArgs e) { //if were already waiting to process an event, we dont need to stage another if (_pause) @@ -130,7 +87,7 @@ namespace VNLib.Plugins.Runtime _delayTimer.Stop(); //Fire event, let exception crash app - Handler.OnPluginUnloaded(_loaderSource); + _handler.OnPluginUnloaded(_loaderSource); //Clear pause flag _pause = false; @@ -140,9 +97,7 @@ namespace VNLib.Plugins.Runtime { _delayTimer.Dispose(); - //Detach event handler and dispose watcher - _watcher.Changed -= OnFileChanged; - _watcher.Dispose(); + FileWatcher.Unsubscribe(_loaderSource.Config.AssemblyFile, this); } } } diff --git a/lib/Plugins.Runtime/src/PluginStackBuilder.cs b/lib/Plugins.Runtime/src/PluginStackBuilder.cs index eed08e2..ef3ffc9 100644 --- a/lib/Plugins.Runtime/src/PluginStackBuilder.cs +++ b/lib/Plugins.Runtime/src/PluginStackBuilder.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Runtime @@ -83,8 +83,10 @@ namespace VNLib.Plugins.Runtime /// The current builder instance for chaining public PluginStackBuilder WithConfigurationReader(IPluginConfigReader pluginConfig) { + ArgumentNullException.ThrowIfNull(pluginConfig); + //Store binary copy - PluginConfig = pluginConfig ?? throw new ArgumentNullException(nameof(pluginConfig)); + PluginConfig = pluginConfig; return this; } diff --git a/lib/Plugins.Runtime/src/RuntimePluginLoader.cs b/lib/Plugins.Runtime/src/RuntimePluginLoader.cs index 60edf55..23bbcab 100644 --- a/lib/Plugins.Runtime/src/RuntimePluginLoader.cs +++ b/lib/Plugins.Runtime/src/RuntimePluginLoader.cs @@ -38,10 +38,9 @@ namespace VNLib.Plugins.Runtime /// public sealed class RuntimePluginLoader : VnDisposeable, IPluginReloadEventHandler { - private static readonly IPluginAssemblyWatcher Watcher = new AssemblyWatcher(); - - private readonly IPluginAssemblyLoader Loader; + private readonly IPluginAssemblyLoader Loader; private readonly ILogProvider? Log; + private readonly IDisposable? Watcher; /// /// Gets the plugin assembly loader configuration information @@ -61,13 +60,15 @@ namespace VNLib.Plugins.Runtime /// public RuntimePluginLoader(IPluginAssemblyLoader loader, ILogProvider? log) { - Log = log; - Loader = loader ?? throw new ArgumentNullException(nameof(loader)); + ArgumentNullException.ThrowIfNull(loader); + + Log = log; + Loader = loader; //Configure watcher if requested if (loader.Config.WatchForReload) { - Watcher.WatchAssembly(this, loader); + Watcher = AssemblyWatcher.WatchAssembly(this, loader); } //Init container @@ -200,10 +201,8 @@ namespace VNLib.Plugins.Runtime /// protected override void Free() { - //Stop watching for events - Watcher.StopWatching(this); - //Cleanup + Watcher?.Dispose(); Controller.Dispose(); Loader.Dispose(); } -- cgit