| | | 1 | | using System.Reflection; |
| | | 2 | | using Kestrun.Languages; |
| | | 3 | | using Kestrun.Scripting; |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp.Scripting; |
| | | 6 | | using Microsoft.CodeAnalysis.Scripting; |
| | | 7 | | using Microsoft.CodeAnalysis.VisualBasic; |
| | | 8 | | using KestrunCompilationErrorException = Kestrun.Scripting.CompilationErrorException; |
| | | 9 | | using RoslynCompilationErrorException = Microsoft.CodeAnalysis.Scripting.CompilationErrorException; |
| | | 10 | | using SerilogLogger = Serilog.ILogger; |
| | | 11 | | |
| | | 12 | | namespace Kestrun.Health; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Creates <see cref="IProbe"/> implementations backed by dynamic scripts. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class ScriptProbeFactory |
| | | 18 | | { |
| | | 19 | | internal static IProbe Create( |
| | | 20 | | string name, |
| | | 21 | | IEnumerable<string>? tags, |
| | | 22 | | ScriptLanguage language, |
| | | 23 | | string code, |
| | | 24 | | SerilogLogger logger, |
| | | 25 | | Func<KestrunRunspacePoolManager>? runspaceAccessor, |
| | | 26 | | IReadOnlyDictionary<string, object?>? arguments, |
| | | 27 | | string[]? extraImports, |
| | | 28 | | Assembly[]? extraRefs) |
| | | 29 | | { |
| | 0 | 30 | | ArgumentNullException.ThrowIfNull(logger); |
| | 0 | 31 | | ArgumentException.ThrowIfNullOrWhiteSpace(code); |
| | | 32 | | |
| | 0 | 33 | | return language switch |
| | 0 | 34 | | { |
| | 0 | 35 | | ScriptLanguage.PowerShell => CreatePowerShellProbe(name, tags, code, logger, runspaceAccessor, arguments), |
| | 0 | 36 | | ScriptLanguage.CSharp => CreateCSharpProbe(name, tags, code, logger, arguments, extraImports, extraRefs), |
| | 0 | 37 | | ScriptLanguage.VBNet => CreateVbProbe(name, tags, code, logger, arguments, extraImports, extraRefs), |
| | 0 | 38 | | ScriptLanguage.Native => throw new NotSupportedException("Use AddProbe(Func<...>) for native probes."), |
| | 0 | 39 | | ScriptLanguage.FSharp => throw new NotImplementedException("F# health probes are not yet supported."), |
| | 0 | 40 | | ScriptLanguage.Python => throw new NotImplementedException("Python health probes are not yet supported."), |
| | 0 | 41 | | ScriptLanguage.JavaScript => throw new NotImplementedException("JavaScript health probes are not yet support |
| | 0 | 42 | | _ => throw new ArgumentOutOfRangeException(nameof(language), language, null) |
| | 0 | 43 | | }; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Builds and returns a PowerShell script probe. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="name">The name of the probe.</param> |
| | | 50 | | /// <param name="tags">The tags associated with the probe.</param> |
| | | 51 | | /// <param name="code">The PowerShell code to execute.</param> |
| | | 52 | | /// <param name="logger">The logger to use for logging.</param> |
| | | 53 | | /// <param name="runspaceAccessor">Accessor for the PowerShell runspace pool manager.</param> |
| | | 54 | | /// <param name="arguments">Arguments to pass to the script.</param> |
| | | 55 | | /// <returns>A new <see cref="IProbe"/> instance representing the PowerShell script probe.</returns> |
| | | 56 | | private static IProbe CreatePowerShellProbe( |
| | | 57 | | string name, |
| | | 58 | | IEnumerable<string>? tags, |
| | | 59 | | string code, |
| | | 60 | | SerilogLogger logger, |
| | | 61 | | Func<KestrunRunspacePoolManager>? runspaceAccessor, |
| | | 62 | | IReadOnlyDictionary<string, object?>? arguments) |
| | | 63 | | { |
| | 0 | 64 | | ArgumentNullException.ThrowIfNull(runspaceAccessor); |
| | 0 | 65 | | return new PowerShellScriptProbe(name, tags, code, logger, runspaceAccessor, arguments); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Builds and returns a C# script probe. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="name">The name of the probe.</param> |
| | | 72 | | /// <param name="tags">The tags associated with the probe.</param> |
| | | 73 | | /// <param name="code">The C# code to compile.</param> |
| | | 74 | | /// <param name="logger">The logger to use for logging.</param> |
| | | 75 | | /// <param name="arguments">The arguments for the script.</param> |
| | | 76 | | /// <param name="extraImports">Additional namespaces to import.</param> |
| | | 77 | | /// <param name="extraRefs">Additional assemblies to reference.</param> |
| | | 78 | | /// <returns>A new <see cref="IProbe"/> instance representing the C# script probe.</returns> |
| | | 79 | | private static IProbe CreateCSharpProbe( |
| | | 80 | | string name, |
| | | 81 | | IEnumerable<string>? tags, |
| | | 82 | | string code, |
| | | 83 | | SerilogLogger logger, |
| | | 84 | | IReadOnlyDictionary<string, object?>? arguments, |
| | | 85 | | string[]? extraImports, |
| | | 86 | | Assembly[]? extraRefs) |
| | | 87 | | { |
| | | 88 | | try |
| | | 89 | | { |
| | 0 | 90 | | var runner = BuildCSharpRunner(code, extraImports, extraRefs); |
| | 0 | 91 | | return new CSharpScriptProbe(name, tags, runner, arguments, logger); |
| | | 92 | | } |
| | 0 | 93 | | catch (RoslynCompilationErrorException ex) |
| | | 94 | | { |
| | 0 | 95 | | logger.Error(ex, "Failed to compile C# health probe {Probe}.", name); |
| | 0 | 96 | | throw; |
| | | 97 | | } |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Builds and returns a VB.NET script probe. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <param name="name">The name of the probe.</param> |
| | | 104 | | /// <param name="tags">The tags associated with the probe.</param> |
| | | 105 | | /// <param name="code">The VB.NET code to compile.</param> |
| | | 106 | | /// <param name="logger">The logger to use for logging.</param> |
| | | 107 | | /// <param name="arguments">The arguments for the script.</param> |
| | | 108 | | /// <param name="extraImports">Additional namespaces to import.</param> |
| | | 109 | | /// <param name="extraRefs">Additional assemblies to reference.</param> |
| | | 110 | | /// <returns>A new <see cref="IProbe"/> instance representing the VB.NET script probe.</returns> |
| | | 111 | | /// <remarks> |
| | | 112 | | /// This method compiles the provided VB.NET code into a script runner that can be executed asynchronously. |
| | | 113 | | /// It allows for additional namespaces and assemblies to be included in the compilation context. |
| | | 114 | | /// </remarks> |
| | | 115 | | private static IProbe CreateVbProbe( |
| | | 116 | | string name, |
| | | 117 | | IEnumerable<string>? tags, |
| | | 118 | | string code, |
| | | 119 | | SerilogLogger logger, |
| | | 120 | | IReadOnlyDictionary<string, object?>? arguments, |
| | | 121 | | string[]? extraImports, |
| | | 122 | | Assembly[]? extraRefs) |
| | | 123 | | { |
| | | 124 | | try |
| | | 125 | | { |
| | 0 | 126 | | var runner = VBNetDelegateBuilder.Compile<ProbeResult>(code, logger, extraImports, extraRefs, arguments, Lan |
| | 0 | 127 | | return new VbScriptProbe(name, tags, runner, arguments, logger); |
| | | 128 | | } |
| | 0 | 129 | | catch (KestrunCompilationErrorException ex) |
| | | 130 | | { |
| | 0 | 131 | | logger.Error(ex, "Failed to compile VB.NET health probe {Probe}.", name); |
| | 0 | 132 | | throw; |
| | | 133 | | } |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Builds a C# script runner for the given code, imports, and references. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="code">The C# code to compile.</param> |
| | | 140 | | /// <param name="extraImports">Additional namespaces to import.</param> |
| | | 141 | | /// <param name="extraRefs">Additional assemblies to reference.</param> |
| | | 142 | | /// <returns>A script runner that can execute the compiled code.</returns> |
| | | 143 | | /// <remarks> |
| | | 144 | | /// This method compiles the provided C# code into a script runner that can be executed asynchronously. |
| | | 145 | | /// It allows for additional namespaces and assemblies to be included in the compilation context. |
| | | 146 | | /// </remarks> |
| | | 147 | | private static ScriptRunner<ProbeResult> BuildCSharpRunner(string code, string[]? extraImports, Assembly[]? extraRef |
| | | 148 | | { |
| | 0 | 149 | | var options = ScriptOptions.Default |
| | 0 | 150 | | .AddReferences(DelegateBuilder.BuildBaselineReferences()) |
| | 0 | 151 | | .AddReferences(typeof(ProbeResult).Assembly, typeof(ScriptProbeFactory).Assembly) |
| | 0 | 152 | | .WithImports(DelegateBuilder.PlatformImports) |
| | 0 | 153 | | .AddImports("Kestrun", "Kestrun.Health", "Kestrun.SharedState"); |
| | | 154 | | |
| | 0 | 155 | | if (extraImports is { Length: > 0 }) |
| | | 156 | | { |
| | 0 | 157 | | options = options.WithImports(options.Imports.Concat(extraImports).Distinct(StringComparer.Ordinal)); |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | if (extraRefs is { Length: > 0 }) |
| | | 161 | | { |
| | 0 | 162 | | var additional = extraRefs |
| | 0 | 163 | | .Where(static r => !string.IsNullOrEmpty(r.Location) && File.Exists(r.Location)) |
| | 0 | 164 | | .Select(static r => MetadataReference.CreateFromFile(r.Location)); |
| | 0 | 165 | | options = options.AddReferences(additional); |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | var script = CSharpScript.Create<ProbeResult>(code, options, typeof(CsGlobals)); |
| | 0 | 169 | | var diagnostics = script.Compile(); |
| | 0 | 170 | | return diagnostics.Any(static d => d.Severity == DiagnosticSeverity.Error) |
| | 0 | 171 | | ? throw new RoslynCompilationErrorException("C# health probe compilation failed.", diagnostics) |
| | 0 | 172 | | : script.CreateDelegate(); |
| | | 173 | | } |
| | | 174 | | } |