diff options
Diffstat (limited to 'VNLib.Plugins.Extensions.Loading/RoutingExtensions.cs')
-rw-r--r-- | VNLib.Plugins.Extensions.Loading/RoutingExtensions.cs | 128 |
1 files changed, 54 insertions, 74 deletions
diff --git a/VNLib.Plugins.Extensions.Loading/RoutingExtensions.cs b/VNLib.Plugins.Extensions.Loading/RoutingExtensions.cs index 0c2c222..9242522 100644 --- a/VNLib.Plugins.Extensions.Loading/RoutingExtensions.cs +++ b/VNLib.Plugins.Extensions.Loading/RoutingExtensions.cs @@ -50,48 +50,41 @@ namespace VNLib.Plugins.Extensions.Loading.Routing public static T Route<T>(this PluginBase plugin, string? pluginConfigPathName) where T : IEndpoint { Type endpointType = typeof(T); - try + //If the config attribute is not set, then ignore the config variables + if (string.IsNullOrWhiteSpace(pluginConfigPathName)) { - //If the config attribute is not set, then ignore the config variables - if (string.IsNullOrWhiteSpace(pluginConfigPathName)) - { - ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase) }); - _ = constructor ?? throw new EntryPointNotFoundException($"No constructor found for {endpointType.Name}"); - //Create the new endpoint and pass the plugin instance - T endpoint = (T)constructor.Invoke(new object[] { plugin }); - //Register event handlers for the endpoint - ScheduleIntervals(plugin, endpoint, endpointType, null); - //Route the endpoint - plugin.Route(endpoint); - - //Store ref to plugin for endpoint - _pluginRefs.Add(endpoint, plugin); - - return endpoint; - } - else - { - ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase), typeof(IReadOnlyDictionary<string, JsonElement>) }); - //Make sure the constructor exists - _ = constructor ?? throw new EntryPointNotFoundException($"No constructor found for {endpointType.Name}"); - //Get config variables for the endpoint - IReadOnlyDictionary<string, JsonElement> conf = plugin.GetConfig(pluginConfigPathName); - //Create the new endpoint and pass the plugin instance along with the configuration object - T endpoint = (T)constructor.Invoke(new object[] { plugin, conf }); - //Register event handlers for the endpoint - ScheduleIntervals(plugin, endpoint, endpointType, conf); - //Route the endpoint - plugin.Route(endpoint); + ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase) }); + _ = constructor ?? throw new EntryPointNotFoundException($"No constructor found for {endpointType.Name}"); + //Create the new endpoint and pass the plugin instance + T endpoint = (T)constructor.Invoke(new object[] { plugin }); + //Register event handlers for the endpoint + ScheduleIntervals(plugin, endpoint, endpointType, null); + //Route the endpoint + plugin.Route(endpoint); - //Store ref to plugin for endpoint - _pluginRefs.Add(endpoint, plugin); + //Store ref to plugin for endpoint + _pluginRefs.Add(endpoint, plugin); - return endpoint; - } + return endpoint; } - catch (TargetInvocationException te) when (te.InnerException != null) + else { - throw te.InnerException; + ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase), typeof(IReadOnlyDictionary<string, JsonElement>) }); + //Make sure the constructor exists + _ = constructor ?? throw new EntryPointNotFoundException($"No constructor found for {endpointType.Name}"); + //Get config variables for the endpoint + IReadOnlyDictionary<string, JsonElement> conf = plugin.GetConfig(pluginConfigPathName); + //Create the new endpoint and pass the plugin instance along with the configuration object + T endpoint = (T)constructor.Invoke(new object[] { plugin, conf }); + //Register event handlers for the endpoint + ScheduleIntervals(plugin, endpoint, endpointType, conf); + //Route the endpoint + plugin.Route(endpoint); + + //Store ref to plugin for endpoint + _pluginRefs.Add(endpoint, plugin); + + return endpoint; } } @@ -124,57 +117,44 @@ namespace VNLib.Plugins.Extensions.Loading.Routing private static void ScheduleIntervals<T>(PluginBase plugin, T endpointInstance, Type epType, IReadOnlyDictionary<string, JsonElement>? endpointLocalConfig) where T : IEndpoint { - List<EventHandle> registered = new(); - try - { - //Get all methods that have the configureable async interval attribute specified - IEnumerable<Tuple<ConfigurableAsyncIntervalAttribute, AsyncSchedulableCallback>> confIntervals = epType.GetMethods() + //Get all methods that have the configureable async interval attribute specified + IEnumerable<Tuple<ConfigurableAsyncIntervalAttribute, AsyncSchedulableCallback>> confIntervals = epType.GetMethods() .Where(m => m.GetCustomAttribute<ConfigurableAsyncIntervalAttribute>() != null) .Select(m => new Tuple<ConfigurableAsyncIntervalAttribute, AsyncSchedulableCallback> (m.GetCustomAttribute<ConfigurableAsyncIntervalAttribute>()!, m.CreateDelegate<AsyncSchedulableCallback>(endpointInstance))); - //If the endpoint has a local config, then use it to find the interval - if (endpointLocalConfig != null) - { + //If the endpoint has a local config, then use it to find the interval + if (endpointLocalConfig != null) + { - //Schedule event handlers on the current plugin - foreach (Tuple<ConfigurableAsyncIntervalAttribute, AsyncSchedulableCallback> interval in confIntervals) + //Schedule event handlers on the current plugin + foreach (Tuple<ConfigurableAsyncIntervalAttribute, AsyncSchedulableCallback> interval in confIntervals) + { + int value = endpointLocalConfig[interval.Item1.IntervalPropertyName].GetInt32(); + //Get the timeout from its resolution variable + TimeSpan timeout = interval.Item1.Resolution switch { - int value = endpointLocalConfig[interval.Item1.IntervalPropertyName].GetInt32(); - //Get the timeout from its resolution variable - TimeSpan timeout = interval.Item1.Resolution switch - { - IntervalResultionType.Seconds => TimeSpan.FromSeconds(value), - IntervalResultionType.Minutes => TimeSpan.FromMinutes(value), - IntervalResultionType.Hours => TimeSpan.FromHours(value), - _ => TimeSpan.FromMilliseconds(value), - }; - //Schedule - registered.Add(plugin.ScheduleInterval(interval.Item2, timeout)); - } + IntervalResultionType.Seconds => TimeSpan.FromSeconds(value), + IntervalResultionType.Minutes => TimeSpan.FromMinutes(value), + IntervalResultionType.Hours => TimeSpan.FromHours(value), + _ => TimeSpan.FromMilliseconds(value), + }; + //Schedule + plugin.ScheduleInterval(interval.Item2, timeout); } + } - //Get all methods that have the async interval attribute specified - IEnumerable<Tuple<AsyncIntervalAttribute, AsyncSchedulableCallback>> intervals = epType.GetMethods() + //Get all methods that have the async interval attribute specified + IEnumerable<Tuple<AsyncIntervalAttribute, AsyncSchedulableCallback>> intervals = epType.GetMethods() .Where(m => m.GetCustomAttribute<AsyncIntervalAttribute>() != null) .Select(m => new Tuple<AsyncIntervalAttribute, AsyncSchedulableCallback>( m.GetCustomAttribute<AsyncIntervalAttribute>()!, m.CreateDelegate<AsyncSchedulableCallback>(endpointInstance)) ); - //Schedule event handlers on the current plugin - foreach (Tuple<AsyncIntervalAttribute, AsyncSchedulableCallback> interval in intervals) - { - registered.Add(plugin.ScheduleInterval(interval.Item2, interval.Item1.Interval)); - } - } - catch + //Schedule event handlers on the current plugin + foreach (Tuple<AsyncIntervalAttribute, AsyncSchedulableCallback> interval in intervals) { - //Stop all event handles - foreach (EventHandle evh in registered) - { - evh.Dispose(); - } - throw; + plugin.ScheduleInterval(interval.Item2, interval.Item1.Interval); } } } |