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