< Summary - Kestrun — Combined Coverage

Information
Class: Public.Server.Stop-KrServer
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Stop-KrServer.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
57%
Covered lines: 8
Uncovered lines: 6
Coverable lines: 14
Total lines: 66
Line coverage: 57.1%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Stop-KrServer.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Starts the Kestrun server and listens for incoming requests.
 4    .DESCRIPTION
 5        This function starts the Kestrun server, allowing it to accept incoming HTTP requests.
 6    .PARAMETER Server
 7        The Kestrun server instance to start. This parameter is mandatory.
 8    .PARAMETER NoWait
 9        If specified, the function will not wait for the server to start and will return immediately.
 10    .PARAMETER Quiet
 11        If specified, suppresses output messages during the startup process.
 12    .EXAMPLE
 13        Start-KrServer -Server $server
 14        Starts the specified Kestrun server instance and listens for incoming requests.
 15    .NOTES
 16        This function is designed to be used after the server has been configured and routes have been added.
 17        It will block the console until the server is stopped or Ctrl+C is pressed.
 18#>
 19function Stop-KrServer {
 20    [KestrunRuntimeApi('Definition')]
 21    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
 22    [CmdletBinding()]
 23    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')]
 24    param(
 25        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 26        [Kestrun.Hosting.KestrunHost]$Server,
 27        [Parameter()]
 28        [switch]$NoWait,
 29        [Parameter()]
 30        [switch]$Quiet
 31    )
 32    begin {
 33        # Ensure the server instance is resolved
 134        $Server = Resolve-KestrunServer -Server $Server
 135        if ($null -eq $Server) {
 036            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 37        }
 38    }
 39    process {
 40        # Stop the Kestrel server
 241        $Server.StopAsync() | Out-Null
 42        # Ensure the server is stopped on exit
 143        if (-not $Quiet.IsPresent) {
 044            Write-Host 'Stopping Kestrun server...' -NoNewline
 45        }
 46
 47        # If NoWait is specified, return immediately
 148        if ($NoWait.IsPresent) {
 49            return
 50        }
 51
 152        while ($Server.IsRunning) {
 053            Start-Sleep -Seconds 1
 054            if (-not $Quiet.IsPresent) {
 055                Write-Host '#' -NoNewline
 56            }
 57        }
 58
 159        [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName)
 60
 161        if (-not $Quiet.IsPresent) {
 062            Write-Host 'Kestrun server stopped.'
 63        }
 64    }
 65}
 66

Methods/Properties

Stop-KrServer()