| | | 1 | | using Kestrun.Languages; |
| | | 2 | | using Kestrun.SharedState; |
| | | 3 | | using SerilogLogger = Serilog.ILogger; |
| | | 4 | | using Serilog.Events; |
| | | 5 | | |
| | | 6 | | namespace Kestrun.Health; |
| | | 7 | | /// <summary> |
| | | 8 | | /// A health probe implemented via a VB.NET script. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <param name="name">The name of the probe.</param> |
| | | 11 | | /// <param name="tags">The tags associated with the probe.</param> |
| | | 12 | | /// <param name="runner">The script runner to execute the probe.</param> |
| | | 13 | | /// <param name="locals">The local variables for the script.</param> |
| | | 14 | | /// <param name="logger">The logger to use for logging.</param> |
| | | 15 | | internal sealed class VbScriptProbe( |
| | | 16 | | string name, |
| | | 17 | | IEnumerable<string>? tags, |
| | | 18 | | Func<CsGlobals, Task<ProbeResult>> runner, |
| | | 19 | | IReadOnlyDictionary<string, object?>? locals, |
| | 0 | 20 | | SerilogLogger logger) : Probe(name, tags, logger), IProbe |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// The script runner to execute the probe. |
| | | 24 | | /// </summary> |
| | 0 | 25 | | private readonly Func<CsGlobals, Task<ProbeResult>> _runner = runner ?? throw new ArgumentNullException(nameof(runne |
| | 0 | 26 | | private readonly IReadOnlyDictionary<string, object?>? _locals = locals; |
| | | 27 | | |
| | | 28 | | /// <inheritdoc/> |
| | | 29 | | public override async Task<ProbeResult> CheckAsync(CancellationToken ct = default) |
| | | 30 | | { |
| | 0 | 31 | | var globals = _locals is { Count: > 0 } |
| | 0 | 32 | | ? new CsGlobals(SharedStateStore.Snapshot(), _locals) |
| | 0 | 33 | | : new CsGlobals(SharedStateStore.Snapshot()); |
| | | 34 | | try |
| | | 35 | | { |
| | 0 | 36 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 37 | | { |
| | 0 | 38 | | Logger.Debug("VbScriptProbe {Probe} executing", Name); |
| | | 39 | | } |
| | 0 | 40 | | var result = await _runner(globals).WaitAsync(ct).ConfigureAwait(false) |
| | 0 | 41 | | ?? new ProbeResult(ProbeStatus.Unhealthy, "Script returned null result"); |
| | 0 | 42 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 43 | | { |
| | 0 | 44 | | Logger.Debug("VbScriptProbe {Probe} completed status={Status}", Name, result.Status); |
| | | 45 | | } |
| | 0 | 46 | | return result; |
| | | 47 | | } |
| | 0 | 48 | | catch (OperationCanceledException) when (ct.IsCancellationRequested) |
| | | 49 | | { |
| | 0 | 50 | | throw; |
| | | 51 | | } |
| | 0 | 52 | | catch (Exception ex) |
| | | 53 | | { |
| | 0 | 54 | | Logger.Error(ex, "VB.NET health probe {Probe} failed.", Name); |
| | 0 | 55 | | return new ProbeResult(ProbeStatus.Unhealthy, $"Exception: {ex.Message}"); |
| | | 56 | | } |
| | 0 | 57 | | } |
| | | 58 | | } |