< 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@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 21
Coverable lines: 21
Total lines: 78
Line coverage: 0%
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 08/26/2025 - 01:25:22 Line coverage: 60% (9/15) Total lines: 67 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/03/2025 - 13:55:51 Line coverage: 61.5% (8/13) Total lines: 59 Tag: Kestrun/Kestrun@80ce2a54be2f719c7be1c21a92a8156bfdc48eb409/04/2025 - 18:11:31 Line coverage: 61.5% (8/13) Total lines: 60 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0509/07/2025 - 18:41:40 Line coverage: 58.3% (7/12) Total lines: 62 Tag: Kestrun/Kestrun@2192d4ccb46312ce89b7f7fda1aa8c915bfa228409/08/2025 - 20:34:03 Line coverage: 57.1% (8/14) Total lines: 66 Tag: Kestrun/Kestrun@3790ee5884494a7a2a829344a47743e0bf492e7210/13/2025 - 16:52:37 Line coverage: 0% (0/21) Total lines: 78 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

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('Everywhere')]
 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
 034        $Server = Resolve-KestrunServer -Server $Server
 035        $writeConsole = $false
 36        try {
 037            $null = [Console]::KeyAvailable
 038            $writeConsole = -not $Quiet.IsPresent
 39        } catch {
 040            Write-KrLog -Level Information -Message 'No console available; running in non-interactive mode.'
 041            $writeConsole = $false
 42        }
 43    }
 44    process {
 045        if ($null -ne $Context -and $null -ne $Context.Response) {
 46            # If called within a route, send a response before stopping
 047            Write-KrTextResponse -InputObject 'Server is stopping...' -StatusCode 202
 048            Start-Sleep -Seconds 1
 049            $Server.StopAsync() | Out-Null
 50            return
 51        }
 52        # Stop the Kestrel server
 053        $Server.StopAsync() | Out-Null
 54        # Ensure the server is stopped on exit
 055        if ($writeConsole) {
 056            Write-Host 'Stopping Kestrun server...' -NoNewline
 57        }
 58
 59        # If NoWait is specified, return immediately
 060        if ($NoWait.IsPresent) {
 61            return
 62        }
 63
 064        while ($Server.IsRunning) {
 065            Start-Sleep -Seconds 1
 066            if ($writeConsole) {
 067                Write-Host '#' -NoNewline
 68            }
 69        }
 70
 071        [Kestrun.KestrunHostManager]::Destroy($Server.ApplicationName)
 72
 073        if ($writeConsole) {
 074            Write-Host 'Kestrun server stopped.'
 75        }
 76    }
 77}
 78

Methods/Properties

Stop-KrServer()