| | | 1 | | using Kestrun.Languages; |
| | | 2 | | using Microsoft.CodeAnalysis; |
| | | 3 | | using Microsoft.CodeAnalysis.Scripting; |
| | | 4 | | using RoslynCompilationErrorException = Microsoft.CodeAnalysis.Scripting.CompilationErrorException; |
| | | 5 | | using SerilogLogger = Serilog.ILogger; |
| | | 6 | | using Serilog.Events; |
| | | 7 | | using Kestrun.Hosting; |
| | | 8 | | |
| | | 9 | | namespace Kestrun.Health; |
| | | 10 | | /// <summary> |
| | | 11 | | /// A health probe implemented via a C# script. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="host">The Kestrun host instance.</param> |
| | | 14 | | /// <param name="name">The name of the probe.</param> |
| | | 15 | | /// <param name="tags">The tags associated with the probe.</param> |
| | | 16 | | /// <param name="runner">The script runner to execute the probe.</param> |
| | | 17 | | /// <param name="locals">The local variables for the script.</param> |
| | | 18 | | internal sealed class CSharpScriptProbe( |
| | | 19 | | KestrunHost host, |
| | | 20 | | string name, |
| | | 21 | | IEnumerable<string>? tags, |
| | | 22 | | ScriptRunner<ProbeResult> runner, |
| | 0 | 23 | | IReadOnlyDictionary<string, object?>? locals) : Probe(name, tags), IProbe |
| | | 24 | | { |
| | 0 | 25 | | private SerilogLogger Logger => host.Logger; |
| | | 26 | | /// <summary> |
| | | 27 | | /// The script runner to execute the probe. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | private readonly ScriptRunner<ProbeResult> _runner = runner ?? throw new ArgumentNullException(nameof(runner)); |
| | | 30 | | /// <summary> |
| | | 31 | | /// The local variables for the script. |
| | | 32 | | /// </summary> |
| | 0 | 33 | | private readonly IReadOnlyDictionary<string, object?>? _locals = locals; |
| | | 34 | | |
| | | 35 | | /// <inheritdoc/> |
| | | 36 | | public override async Task<ProbeResult> CheckAsync(CancellationToken ct = default) |
| | | 37 | | { |
| | 0 | 38 | | var globals = _locals is { Count: > 0 } |
| | 0 | 39 | | ? new CsGlobals(host.SharedState.Snapshot(), _locals) |
| | 0 | 40 | | : new CsGlobals(host.SharedState.Snapshot()); |
| | | 41 | | try |
| | | 42 | | { |
| | 0 | 43 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 44 | | { |
| | 0 | 45 | | Logger.Debug("CSharpScriptProbe {Probe} executing", Name); |
| | | 46 | | } |
| | 0 | 47 | | var result = await _runner(globals, ct).ConfigureAwait(false) |
| | 0 | 48 | | ?? new ProbeResult(ProbeStatus.Unhealthy, "Script returned null result"); |
| | 0 | 49 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 50 | | { |
| | 0 | 51 | | Logger.Debug("CSharpScriptProbe {Probe} completed status={Status}", Name, result.Status); |
| | | 52 | | } |
| | 0 | 53 | | return result; |
| | | 54 | | } |
| | 0 | 55 | | catch (OperationCanceledException) when (ct.IsCancellationRequested) |
| | | 56 | | { |
| | 0 | 57 | | throw; |
| | | 58 | | } |
| | 0 | 59 | | catch (RoslynCompilationErrorException ex) |
| | | 60 | | { |
| | 0 | 61 | | Logger.Error(ex, "C# health probe {Probe} failed to execute.", Name); |
| | 0 | 62 | | return new ProbeResult(ProbeStatus.Unhealthy, string.Join("; ", ex.Diagnostics.Select(static d => d.GetMessa |
| | | 63 | | } |
| | 0 | 64 | | catch (Exception ex) |
| | | 65 | | { |
| | 0 | 66 | | Logger.Error(ex, "C# health probe {Probe} threw an exception.", Name); |
| | 0 | 67 | | return new ProbeResult(ProbeStatus.Unhealthy, $"Exception: {ex.Message}"); |
| | | 68 | | } |
| | 0 | 69 | | } |
| | | 70 | | } |