| | 1 | | using System.Text; |
| | 2 | | using Kestrun.Hosting; |
| | 3 | | using Serilog; |
| | 4 | |
|
| | 5 | | namespace Kestrun.Utilities; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Provides extension methods for hosting Kestrun servers. |
| | 9 | | /// </summary> |
| | 10 | | public static class HostingExtensions |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Starts <paramref name="server"/>, blocks until the process receives |
| | 14 | | /// Ctrl-C / SIGTERM (or <paramref name="stopToken"/> is cancelled), |
| | 15 | | /// then calls <c>StopAsync</c> and disposes the host. |
| | 16 | | /// </summary> |
| | 17 | | /// <remarks> |
| | 18 | | /// <para>Intended for console apps’ <c>Main</c>; keeps boiler-plate out |
| | 19 | | /// of every sample you write.</para> |
| | 20 | | /// <para>If <paramref name="configureConsole"/> is <see langword="true"/> |
| | 21 | | /// (default) the method hooks <see cref="Console.CancelKeyPress"/>. |
| | 22 | | /// In a service/daemon scenario you can pass <see langword="false"/> |
| | 23 | | /// and supply your own cancellation token.</para> |
| | 24 | | /// </remarks> |
| | 25 | | public static async Task RunUntilShutdownAsync( |
| | 26 | | this KestrunHost server, |
| | 27 | | bool configureConsole = true, |
| | 28 | | Encoding? consoleEncoding = null, |
| | 29 | | Action? onStarted = null, |
| | 30 | | Action<Exception>? onShutdownError = null, |
| | 31 | | CancellationToken stopToken = default) |
| | 32 | | { |
| 0 | 33 | | ArgumentNullException.ThrowIfNull(server); |
| 0 | 34 | | if (consoleEncoding != null) |
| | 35 | | { |
| 0 | 36 | | Console.OutputEncoding = consoleEncoding; |
| 0 | 37 | | Console.InputEncoding = consoleEncoding; |
| | 38 | | } |
| 0 | 39 | | using var linked = CancellationTokenSource.CreateLinkedTokenSource(stopToken); |
| 0 | 40 | | var done = new TaskCompletionSource<object?>( |
| 0 | 41 | | TaskCreationOptions.RunContinuationsAsynchronously); |
| | 42 | |
|
| | 43 | | // ① optional Ctrl-C handler |
| 0 | 44 | | if (configureConsole) |
| | 45 | | { |
| 0 | 46 | | Console.CancelKeyPress += Handler; |
| | 47 | | } |
| | 48 | |
|
| | 49 | | // ② start + user callback |
| 0 | 50 | | await server.StartAsync(linked.Token); |
| 0 | 51 | | onStarted?.Invoke(); |
| 0 | 52 | | Log.Information("Kestrun server started. Press Ctrl+C to stop."); |
| | 53 | |
|
| | 54 | | // ③ wait for either Ctrl-C or external cancellation |
| 0 | 55 | | using (stopToken.Register(() => done.TrySetResult(null))) |
| | 56 | | { |
| 0 | 57 | | _ = await done.Task; |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | // ④ graceful shutdown |
| | 61 | | try |
| | 62 | | { |
| 0 | 63 | | await server.StopAsync(); |
| 0 | 64 | | } |
| 0 | 65 | | catch (Exception ex) |
| | 66 | | { |
| 0 | 67 | | onShutdownError?.Invoke(ex); |
| 0 | 68 | | Log.Debug(ex, "Ignored exception during server shutdown."); |
| 0 | 69 | | } |
| | 70 | | finally |
| | 71 | | { |
| 0 | 72 | | server.Dispose(); |
| 0 | 73 | | if (configureConsole) |
| | 74 | | { |
| 0 | 75 | | Console.CancelKeyPress -= Handler; |
| | 76 | | } |
| | 77 | | } |
| | 78 | |
|
| | 79 | | // local function so we can unregister it |
| | 80 | | void Handler(object? _, ConsoleCancelEventArgs e) |
| | 81 | | { |
| 0 | 82 | | e.Cancel = true; // prevent immediate process kill |
| 0 | 83 | | linked.Cancel(); // wake up the await |
| 0 | 84 | | _ = done.TrySetResult(null); |
| 0 | 85 | | } |
| 0 | 86 | | } |
| | 87 | | } |