| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Requests cancellation for a running task. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Signals the Kestrun Task service to cancel the specified task. |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance. |
| | | 8 | | .PARAMETER Id |
| | | 9 | | Task id to cancel. |
| | | 10 | | .PARAMETER WhatIf |
| | | 11 | | Shows what would happen if the cmdlet runs. The cmdlet is not run. |
| | | 12 | | .PARAMETER Confirm |
| | | 13 | | Prompts you for confirmation before running the cmdlet. |
| | | 14 | | .EXAMPLE |
| | | 15 | | Stop-KrTask -Id 'task-id' |
| | | 16 | | Requests cancellation for the specified task. |
| | | 17 | | .NOTES |
| | | 18 | | Requires the Kestrun Task service to be added to the server via Add-KrTasksService. |
| | | 19 | | Cancellation is cooperative; the task script must periodically check for cancellation and stop itself. |
| | | 20 | | Returns $true if the task was found and cancellation was requested; $false if the task was not found or could not be |
| | | 21 | | If the task is already completed, cancellation will not be requested and $false will be returned. |
| | | 22 | | Cancellation may not be immediate; the task may take some time to stop after cancellation is requested. |
| | | 23 | | If the task does not support cancellation, it will continue to run until completion. |
| | | 24 | | This cmdlet supports ShouldProcess for confirmation prompts. |
| | | 25 | | #> |
| | | 26 | | function Stop-KrTask { |
| | | 27 | | [KestrunRuntimeApi('Everywhere')] |
| | | 28 | | [CmdletBinding(SupportsShouldProcess)] |
| | | 29 | | [OutputType([bool])] |
| | | 30 | | param( |
| | | 31 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 32 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 33 | | |
| | | 34 | | [Parameter(Mandatory)] |
| | | 35 | | [string]$Id |
| | | 36 | | ) |
| | | 37 | | begin { |
| | 0 | 38 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 39 | | } |
| | | 40 | | process { |
| | 0 | 41 | | if ($PSCmdlet.ShouldProcess("Task $Id", 'Cancel')) { |
| | 0 | 42 | | return $Server.Tasks.Cancel($Id) |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | } |