| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using Kestrun.Hosting; |
| | | 3 | | using Serilog; |
| | | 4 | | using Serilog.Events; |
| | | 5 | | |
| | | 6 | | namespace Kestrun; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides management functionality for KestrunHost instances, including creation, retrieval, starting, stopping, and |
| | | 10 | | /// </summary> |
| | | 11 | | public static class KestrunHostManager |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Holds the collection of KestrunHost instances, keyed by their names. |
| | | 15 | | /// </summary> |
| | 1 | 16 | | private static readonly ConcurrentDictionary<string, KestrunHost> _instances = new(StringComparer.OrdinalIgnoreCase) |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// The name of the default KestrunHost instance, if one has been set. |
| | | 20 | | /// </summary> |
| | | 21 | | private static string? _defaultName; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The root path for Kestrun operations. |
| | | 25 | | /// </summary> |
| | | 26 | | private static string? _kestrunRoot; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the collection of names for all KestrunHost instances. |
| | | 30 | | /// </summary> |
| | 3 | 31 | | public static IReadOnlyCollection<string> InstanceNames => (IReadOnlyCollection<string>)_instances.Keys; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets or sets the root path for Kestrun operations. |
| | | 35 | | /// </summary> |
| | | 36 | | public static string? KestrunRoot |
| | | 37 | | { |
| | 4 | 38 | | get => _kestrunRoot; |
| | | 39 | | set |
| | | 40 | | { |
| | 21 | 41 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 42 | | { |
| | 1 | 43 | | throw new ArgumentException("Kestrun root path cannot be null or empty.", nameof(value)); |
| | | 44 | | } |
| | | 45 | | |
| | 20 | 46 | | if (Directory.GetCurrentDirectory() != value) |
| | | 47 | | { |
| | 2 | 48 | | Directory.SetCurrentDirectory(value); |
| | | 49 | | } |
| | 20 | 50 | | _kestrunRoot = value; |
| | 20 | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Creates a new KestrunHost instance using the provided factory function. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="name">The name of the KestrunHost instance to create.</param> |
| | | 59 | | /// <param name="factory">A factory function that returns a new KestrunHost instance.</param> |
| | | 60 | | /// <param name="setAsDefault">Whether to set this instance as the default.</param> |
| | | 61 | | /// <param name="enablePowershellMiddleware">Whether to enable PowerShell middleware for this instance.</param> |
| | | 62 | | /// <returns>The created KestrunHost instance.</returns> |
| | | 63 | | public static KestrunHost Create(string name, Func<KestrunHost> factory, bool setAsDefault = false, bool enablePower |
| | | 64 | | { |
| | 21 | 65 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 66 | | { |
| | 0 | 67 | | throw new ArgumentException("Instance name is required.", nameof(name)); |
| | | 68 | | } |
| | | 69 | | |
| | 21 | 70 | | if (_instances.ContainsKey(name)) |
| | | 71 | | { |
| | 2 | 72 | | throw new InvalidOperationException($"A KestrunHost instance with the name '{name}' already exists."); |
| | | 73 | | } |
| | | 74 | | |
| | 19 | 75 | | var host = factory(); |
| | 19 | 76 | | host.PowershellMiddlewareEnabled = enablePowershellMiddleware; |
| | 19 | 77 | | _instances[name] = host; |
| | | 78 | | |
| | 19 | 79 | | if (setAsDefault || _defaultName == null) |
| | | 80 | | { |
| | 12 | 81 | | _defaultName = name; |
| | | 82 | | } |
| | | 83 | | |
| | 19 | 84 | | return host; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Creates a new KestrunHost instance with the specified name and optional module paths, using the default logger. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="name">The name of the KestrunHost instance to create.</param> |
| | | 91 | | /// <param name="modulePathsObj">Optional array of module paths to load.</param> |
| | | 92 | | /// <param name="setAsDefault">Whether to set this instance as the default.</param> |
| | | 93 | | /// <param name="enablePowershellMiddleware">Whether to enable PowerShell middleware for this instance.</param> |
| | | 94 | | /// <returns>The created KestrunHost instance.</returns> |
| | | 95 | | public static KestrunHost Create(string name, |
| | | 96 | | string[]? modulePathsObj = null, bool setAsDefault = false, bool enablePowershellMiddleware = false) => |
| | 0 | 97 | | Create(name, Log.Logger, modulePathsObj, setAsDefault, enablePowershellMiddleware); |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Creates a new KestrunHost instance with the specified name, logger, root path, and optional module paths. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="name">The name of the KestrunHost instance to create.</param> |
| | | 103 | | /// <param name="logger">The Serilog logger to use for the host.</param> |
| | | 104 | | /// <param name="modulePathsObj">Optional array of module paths to load.</param> |
| | | 105 | | /// <param name="setAsDefault">Whether to set this instance as the default.</param> |
| | | 106 | | /// <param name="enablePowershellMiddleware">Whether to enable PowerShell middleware for this instance.</param> |
| | | 107 | | /// <returns>The created KestrunHost instance.</returns> |
| | | 108 | | public static KestrunHost Create(string name, Serilog.ILogger logger, |
| | | 109 | | string[]? modulePathsObj = null, bool setAsDefault = false, bool enablePowershellMiddleware = false) |
| | | 110 | | { |
| | 2 | 111 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 112 | | { |
| | 0 | 113 | | throw new ArgumentException("Instance name is required.", nameof(name)); |
| | | 114 | | } |
| | | 115 | | |
| | 2 | 116 | | if (_instances.ContainsKey(name)) |
| | | 117 | | { |
| | 0 | 118 | | throw new InvalidOperationException($"A KestrunHost instance with the name '{name}' already exists."); |
| | | 119 | | } |
| | | 120 | | |
| | 2 | 121 | | if (KestrunRoot is null) |
| | | 122 | | { |
| | 1 | 123 | | throw new InvalidOperationException("Kestrun root path must be set before creating a KestrunHost instance.") |
| | | 124 | | } |
| | | 125 | | |
| | 1 | 126 | | var host = new KestrunHost(name, logger, KestrunRoot, modulePathsObj) |
| | 1 | 127 | | { |
| | 1 | 128 | | PowershellMiddlewareEnabled = enablePowershellMiddleware, |
| | 1 | 129 | | DefaultHost = setAsDefault |
| | 1 | 130 | | }; |
| | | 131 | | |
| | 1 | 132 | | _instances[name] = host; |
| | | 133 | | |
| | 1 | 134 | | if (setAsDefault || _defaultName == null) |
| | | 135 | | { |
| | 1 | 136 | | _defaultName = name; |
| | | 137 | | } |
| | | 138 | | |
| | 1 | 139 | | return host; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Determines whether a KestrunHost instance with the specified name exists. |
| | | 144 | | /// </summary> |
| | | 145 | | /// <param name="name">The name of the KestrunHost instance to check for existence.</param> |
| | | 146 | | /// <returns>True if an instance with the specified name exists; otherwise, false.</returns> |
| | 4 | 147 | | public static bool Contains(string name) => _instances.ContainsKey(name); |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Attempts to retrieve a KestrunHost instance by its name. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="name">The name of the KestrunHost instance to retrieve.</param> |
| | | 153 | | /// <param name="host">When this method returns, contains the KestrunHost instance associated with the specified nam |
| | | 154 | | /// <returns>True if the instance was found; otherwise, false.</returns> |
| | | 155 | | public static bool TryGet(string name, out KestrunHost? host) => |
| | 8 | 156 | | _instances.TryGetValue(name, out host); |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Retrieves a KestrunHost instance by its name. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <param name="name">The name of the KestrunHost instance to retrieve.</param> |
| | | 162 | | /// <returns>The KestrunHost instance if found; otherwise, null.</returns> |
| | | 163 | | public static KestrunHost? Get(string name) => |
| | 4 | 164 | | _instances.TryGetValue(name, out var host) ? host : null; |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Gets the default KestrunHost instance, if one has been set. |
| | | 168 | | /// </summary> |
| | 7 | 169 | | public static KestrunHost? Default => _defaultName != null && _instances.TryGetValue(_defaultName, out var host) ? h |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Sets the specified KestrunHost instance as the default. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <param name="name">The name of the KestrunHost instance to set as default.</param> |
| | | 175 | | /// <exception cref="InvalidOperationException">Thrown if no instance with the specified name exists.</exception> |
| | | 176 | | public static void SetDefault(string name) |
| | | 177 | | { |
| | 4 | 178 | | if (!_instances.ContainsKey(name)) |
| | | 179 | | { |
| | 2 | 180 | | throw new InvalidOperationException($"No KestrunHost instance named '{name}' exists."); |
| | | 181 | | } |
| | | 182 | | |
| | 2 | 183 | | _defaultName = name; |
| | 2 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Determines whether the specified KestrunHost instance is currently running. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <param name="name">The name of the KestrunHost instance to check.</param> |
| | | 190 | | /// <returns>True if the instance is running; otherwise, false.</returns> |
| | 1 | 191 | | public static bool IsRunning(string name) => TryGet(name, out var host) && host != null && host.IsRunning; |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Starts the specified KestrunHost instance asynchronously. |
| | | 195 | | /// </summary> |
| | | 196 | | /// <param name="name">The name of the KestrunHost instance to start.</param> |
| | | 197 | | /// <param name="ct">A cancellation token to observe while waiting for the task to complete.</param> |
| | | 198 | | public static async Task StartAsync(string name, CancellationToken ct = default) |
| | | 199 | | { |
| | 1 | 200 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | | 201 | | { |
| | 1 | 202 | | Log.Debug("Starting (Async) KestrunHost instance '{Name}'", name); |
| | | 203 | | } |
| | | 204 | | |
| | 1 | 205 | | if (TryGet(name, out var host)) |
| | | 206 | | { |
| | 0 | 207 | | if (host is not null) |
| | | 208 | | { |
| | 0 | 209 | | await host.StartAsync(ct); |
| | | 210 | | } |
| | | 211 | | else |
| | | 212 | | { |
| | 0 | 213 | | throw new InvalidOperationException($"KestrunHost instance '{name}' is null."); |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | else |
| | | 217 | | { |
| | 1 | 218 | | throw new InvalidOperationException($"No KestrunHost instance named '{name}' exists."); |
| | | 219 | | } |
| | 0 | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Stops the specified KestrunHost instance asynchronously. |
| | | 224 | | /// </summary> |
| | | 225 | | /// <param name="name">The name of the KestrunHost instance to stop.</param> |
| | | 226 | | /// <param name="ct">A cancellation token to observe while waiting for the task to complete.</param> |
| | | 227 | | public static async Task StopAsync(string name, CancellationToken ct = default) |
| | | 228 | | { |
| | 2 | 229 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | | 230 | | { |
| | 2 | 231 | | Log.Debug("Stopping (Async) KestrunHost instance '{Name}'", name); |
| | | 232 | | } |
| | | 233 | | |
| | 2 | 234 | | if (TryGet(name, out var host)) |
| | | 235 | | { |
| | 1 | 236 | | if (host is not null) |
| | | 237 | | { |
| | 1 | 238 | | await host.StopAsync(ct); |
| | | 239 | | } |
| | | 240 | | else |
| | | 241 | | { |
| | 0 | 242 | | throw new InvalidOperationException($"KestrunHost instance '{name}' is null."); |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | else |
| | | 246 | | { |
| | 1 | 247 | | throw new InvalidOperationException($"No KestrunHost instance named '{name}' exists."); |
| | | 248 | | } |
| | 1 | 249 | | } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Stops the specified KestrunHost instance synchronously. |
| | | 253 | | /// </summary> |
| | | 254 | | /// <param name="name">The name of the KestrunHost instance to stop.</param> |
| | | 255 | | public static void Stop(string name) |
| | | 256 | | { |
| | 2 | 257 | | if (_instances.TryGetValue(name, out var host)) |
| | | 258 | | { |
| | 0 | 259 | | host.Stop(); |
| | | 260 | | } |
| | 2 | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// Stops all KestrunHost instances asynchronously. |
| | | 265 | | /// </summary> |
| | | 266 | | /// <param name="ct">A cancellation token to observe while waiting for the tasks to complete.</param> |
| | | 267 | | public static async Task StopAllAsync(CancellationToken ct = default) |
| | | 268 | | { |
| | 2 | 269 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | | 270 | | { |
| | 2 | 271 | | Log.Debug("Stopping all KestrunHost instances (Async)"); |
| | | 272 | | } |
| | | 273 | | |
| | 12 | 274 | | foreach (var kv in _instances) |
| | | 275 | | { |
| | 4 | 276 | | await kv.Value.StopAsync(ct); |
| | | 277 | | } |
| | 2 | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Destroys the specified KestrunHost instance and disposes its resources. |
| | | 282 | | /// </summary> |
| | | 283 | | /// <param name="name">The name of the KestrunHost instance to destroy.</param> |
| | | 284 | | /// <param name="disposeLogger">Whether to dispose the Serilog logger if this was the last instance.</param> |
| | | 285 | | /// <remarks> |
| | | 286 | | /// If this is the last instance, the logger will be disposed to release any resources. |
| | | 287 | | /// </remarks> |
| | | 288 | | public static void Destroy(string name, bool disposeLogger = false) |
| | | 289 | | { |
| | 20 | 290 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | | 291 | | { |
| | 20 | 292 | | Log.Debug("Destroying KestrunHost instance '{Name}'", name); |
| | | 293 | | } |
| | | 294 | | |
| | 20 | 295 | | if (_instances.TryRemove(name, out var host)) |
| | | 296 | | { |
| | 20 | 297 | | host.Dispose(); |
| | 20 | 298 | | if (_defaultName == name) |
| | | 299 | | { |
| | 19 | 300 | | _defaultName = _instances.Keys.FirstOrDefault(); |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | |
| | 20 | 304 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | | 305 | | { |
| | 20 | 306 | | Log.Debug("KestrunHost instance '{Name}' destroyed", name); |
| | | 307 | | } |
| | | 308 | | } |
| | | 309 | | else |
| | | 310 | | { |
| | 0 | 311 | | if (Log.IsEnabled(LogEventLevel.Warning)) |
| | | 312 | | { |
| | 0 | 313 | | Log.Warning("No KestrunHost instance named '{Name}' exists to destroy", name); |
| | | 314 | | } |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | // If no more instances, clear default and close logger |
| | 20 | 318 | | if (_instances.IsEmpty) |
| | | 319 | | { |
| | 13 | 320 | | _defaultName = null; |
| | | 321 | | // If no more instances, close logger |
| | 13 | 322 | | if (disposeLogger) { Log.CloseAndFlush(); } |
| | | 323 | | } |
| | 20 | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Destroys all KestrunHost instances and disposes their resources. |
| | | 328 | | /// </summary> |
| | | 329 | | public static void DestroyAll() |
| | | 330 | | { |
| | 19 | 331 | | if (Log.IsEnabled(LogEventLevel.Debug)) |
| | | 332 | | { |
| | 19 | 333 | | Log.Debug("Destroying all KestrunHost instances"); |
| | | 334 | | } |
| | | 335 | | |
| | 72 | 336 | | foreach (var name in _instances.Keys.ToList()) |
| | | 337 | | { |
| | 17 | 338 | | Destroy(name); |
| | | 339 | | } |
| | 19 | 340 | | } |
| | | 341 | | } |