| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Registers a script-based health probe with the active Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Wraps the Kestrun host AddProbe overload that accepts script content. You can provide inline |
| | | 6 | | PowerShell via the -ScriptBlock parameter set, inline code tied to an explicit language, or |
| | | 7 | | the path to a script file. Optional arguments, imports, references, and tag metadata can be |
| | | 8 | | supplied to influence probe execution and filtering. |
| | | 9 | | .PARAMETER Server |
| | | 10 | | The Kestrun host instance to configure. If omitted, the current server context is resolved automatically. |
| | | 11 | | .PARAMETER Name |
| | | 12 | | Unique name for the probe. |
| | | 13 | | .PARAMETER Tags |
| | | 14 | | Optional set of tags used to include or exclude the probe when requests filter by tag. |
| | | 15 | | .PARAMETER ScriptBlock |
| | | 16 | | Inline PowerShell that returns a ProbeResult (or equivalent contract). This is the default parameter set. |
| | | 17 | | .PARAMETER Code |
| | | 18 | | Inline code interpreted in the language supplied via -Language. |
| | | 19 | | .PARAMETER Language |
| | | 20 | | Script language for inline code or script files. When registering a script block the language defaults to PowerS |
| | | 21 | | .PARAMETER CodePath |
| | | 22 | | Path to a script file. The file is read once at registration time. |
| | | 23 | | .PARAMETER Arguments |
| | | 24 | | Hashtable of values exposed to the probe at execution time. |
| | | 25 | | .PARAMETER ExtraImports |
| | | 26 | | Additional language-specific imports (namespaces) supplied to Roslyn-based probes. |
| | | 27 | | .PARAMETER ExtraRefs |
| | | 28 | | Additional assemblies referenced by Roslyn-based probes. |
| | | 29 | | .PARAMETER PassThru |
| | | 30 | | Emits the configured server instance so the call can be chained. |
| | | 31 | | .EXAMPLE |
| | | 32 | | Add-KrHealthProbe -Name SelfCheck -Tags 'core' -ScriptBlock { |
| | | 33 | | return [Kestrun.Health.ProbeResult]::new([Kestrun.Health.ProbeStatus]::Healthy, 'Service ready') |
| | | 34 | | } |
| | | 35 | | Registers a PowerShell health probe named SelfCheck tagged with 'core'. |
| | | 36 | | .EXAMPLE |
| | | 37 | | Add-KrHealthProbe -Name Database -Language CSharp -Code @" |
| | | 38 | | return await ProbeAsync(); |
| | | 39 | | "@ |
| | | 40 | | Registers an inline C# health probe. |
| | | 41 | | .EXAMPLE |
| | | 42 | | Add-KrHealthProbe -Name Cache -CodePath './Scripts/CacheProbe.cs' -Language CSharp -ExtraImports 'System.Net' |
| | | 43 | | Registers a C# health probe from a script file and adds an extra namespace import. |
| | | 44 | | #> |
| | | 45 | | function Add-KrHealthProbe { |
| | | 46 | | [KestrunRuntimeApi('Definition')] |
| | | 47 | | [CmdletBinding(DefaultParameterSetName = 'ScriptBlock')] |
| | | 48 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 49 | | param( |
| | | 50 | | [Parameter(ValueFromPipeline = $true)] |
| | | 51 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 52 | | |
| | | 53 | | [Parameter(Mandatory = $true)] |
| | | 54 | | [string]$Name, |
| | | 55 | | |
| | | 56 | | [string[]]$Tags, |
| | | 57 | | |
| | | 58 | | [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'ScriptBlock')] |
| | | 59 | | [scriptblock]$ScriptBlock, |
| | | 60 | | |
| | | 61 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 62 | | [string]$Code, |
| | | 63 | | |
| | | 64 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 65 | | [Parameter(ParameterSetName = 'File')] |
| | | 66 | | [Kestrun.Scripting.ScriptLanguage]$Language, |
| | | 67 | | |
| | | 68 | | [Parameter(Mandatory = $true, ParameterSetName = 'File')] |
| | | 69 | | [string]$CodePath, |
| | | 70 | | |
| | | 71 | | [hashtable]$Arguments, |
| | | 72 | | |
| | | 73 | | [string[]]$ExtraImports, |
| | | 74 | | |
| | | 75 | | [System.Reflection.Assembly[]]$ExtraRefs, |
| | | 76 | | |
| | | 77 | | [switch]$PassThru |
| | | 78 | | ) |
| | | 79 | | begin { |
| | 0 | 80 | | $Server = Resolve-KestrunServer -Server $Server |
| | 0 | 81 | | if ($null -eq $Server) { |
| | 0 | 82 | | throw 'Server is not initialized. Call New-KrServer first or pipe an existing host instance.' |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | process { |
| | 0 | 86 | | $codeToRegister = $null |
| | 0 | 87 | | [Kestrun.Scripting.ScriptLanguage]$effectiveLanguage = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 88 | | |
| | 0 | 89 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 90 | | 'ScriptBlock' { |
| | 0 | 91 | | $codeToRegister = $ScriptBlock.ToString() |
| | 0 | 92 | | $effectiveLanguage = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 93 | | } |
| | | 94 | | 'Code' { |
| | 0 | 95 | | $codeToRegister = $Code |
| | 0 | 96 | | $effectiveLanguage = $Language |
| | | 97 | | } |
| | | 98 | | 'File' { |
| | 0 | 99 | | if (-not (Test-Path -LiteralPath $CodePath)) { |
| | 0 | 100 | | throw "The specified code path '$CodePath' does not exist. Ensure the file exists and the path is co |
| | | 101 | | } |
| | 0 | 102 | | $codeToRegister = Get-Content -LiteralPath $CodePath -Raw |
| | 0 | 103 | | if ($PSBoundParameters.ContainsKey('Language')) { |
| | 0 | 104 | | $effectiveLanguage = $Language |
| | | 105 | | } else { |
| | 0 | 106 | | $extension = [System.IO.Path]::GetExtension($CodePath).ToLowerInvariant() |
| | 0 | 107 | | $effectiveLanguage = switch ($extension) { |
| | 0 | 108 | | '.ps1' { [Kestrun.Scripting.ScriptLanguage]::PowerShell } |
| | 0 | 109 | | '.cs' { [Kestrun.Scripting.ScriptLanguage]::CSharp } |
| | 0 | 110 | | '.vb' { [Kestrun.Scripting.ScriptLanguage]::VBNet } |
| | | 111 | | default { |
| | 0 | 112 | | throw "Cannot infer script language from extension '$extension'. Specify -Language explicitl |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | default { |
| | 0 | 118 | | throw "Unsupported parameter set '$($PSCmdlet.ParameterSetName)'." |
| | | 119 | | } |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | if ([string]::IsNullOrWhiteSpace($codeToRegister)) { |
| | 0 | 123 | | throw 'Probe code cannot be empty.' |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | $normalizedTags = $null |
| | 0 | 127 | | if ($PSBoundParameters.ContainsKey('Tags')) { |
| | 0 | 128 | | $normalizedTags = @($Tags | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | ForEach-Object { $_.Tri |
| | 0 | 129 | | if ($normalizedTags.Count -eq 0) { |
| | 0 | 130 | | $normalizedTags = $null |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | $argDictionary = $null |
| | 0 | 135 | | if ($PSBoundParameters.ContainsKey('Arguments') -and $Arguments) { |
| | 0 | 136 | | $argDictionary = [System.Collections.Generic.Dictionary[string, object]]::new() |
| | 0 | 137 | | foreach ($key in $Arguments.Keys) { |
| | 0 | 138 | | $argDictionary[$key] = $Arguments[$key] |
| | | 139 | | } |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | $imports = $null |
| | 0 | 143 | | if ($PSBoundParameters.ContainsKey('ExtraImports')) { |
| | 0 | 144 | | $imports = @($ExtraImports | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | ForEach-Object { $_.Tr |
| | 0 | 145 | | if ($imports.Count -eq 0) { |
| | 0 | 146 | | $imports = $null |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | try { |
| | 0 | 151 | | $hostResult = $Server.AddProbe($Name, $normalizedTags, $codeToRegister, $effectiveLanguage, $argDictionary, |
| | 0 | 152 | | Write-KrLog -Level Information -Message "Health probe '{0}' registered." -Values $Name |
| | 0 | 153 | | if ($PassThru.IsPresent) { |
| | 0 | 154 | | return $hostResult |
| | | 155 | | } |
| | | 156 | | } catch { |
| | 0 | 157 | | Write-KrLog -Level Error -Message "Failed to register health probe '{0}'." -Values $Name -ErrorRecord $_ |
| | | 158 | | throw |
| | | 159 | | } |
| | | 160 | | } |
| | | 161 | | } |