Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Dec 4, 2024
1 parent 9cdaf4e commit 414d785
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
10 changes: 6 additions & 4 deletions src/Private/Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,12 @@ function New-PodeContext {

# create new cancellation tokens
$ctx.Tokens = @{
Cancellation = [System.Threading.CancellationTokenSource]::new()
Restart = [System.Threading.CancellationTokenSource]::new()
Dump = [System.Threading.CancellationTokenSource]::new()
SuspendResume = [System.Threading.CancellationTokenSource]::new()
Cancellation = [System.Threading.CancellationTokenSource]::new()
Restart = [System.Threading.CancellationTokenSource]::new()
Dump = [System.Threading.CancellationTokenSource]::new()
Pause = [System.Threading.CancellationTokenSource]::new()
Resume = [System.Threading.CancellationTokenSource]::new()
#Terminate = [System.Threading.CancellationTokenSource]::new()
}

# requests that should be logged
Expand Down
21 changes: 12 additions & 9 deletions src/Private/Server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ function Restart-PodeInternalServer {
Reset-PodeCancellationToken -Type Cancellation
Reset-PodeCancellationToken -Type Restart
Reset-PodeCancellationToken -Type Dump
Reset-PodeCancellationToken -Type Pause
Reset-PodeCancellationToken -Type Resume

# reload the configuration
$PodeContext.Server.Configuration = Open-PodeConfiguration -Context $PodeContext
Expand All @@ -401,36 +403,37 @@ function Restart-PodeInternalServer {
<#
.SYNOPSIS
Resets the cancellation token for a specific type in Pode.
.DESCRIPTION
The `Reset-PodeCancellationToken` function disposes of the existing cancellation token
for the specified type and reinitializes it with a new token. This ensures proper cleanup
of disposable resources associated with the cancellation token.
.PARAMETER Type
The type of cancellation token to reset. This is a mandatory parameter and must be
provided as a string.
.EXAMPLES
.EXAMPLE
# Reset the cancellation token for the 'Cancellation' type
Reset-PodeCancellationToken -Type Cancellation
.EXAMPLE
# Reset the cancellation token for the 'Restart' type
Reset-PodeCancellationToken -Type Restart
.EXAMPLE
# Reset the cancellation token for the 'Dump' type
Reset-PodeCancellationToken -Type Dump
# Reset the cancellation token for the 'SuspendResume' type
Reset-PodeCancellationToken -Type SuspendResume
.EXAMPLE
# Reset the cancellation token for the 'Pause' type
Reset-PodeCancellationToken -Type Pause
.NOTES
This function is used to manage cancellation tokens in Pode's internal context.
#>
function Reset-PodeCancellationToken {
param(
[Parameter(Mandatory = $true)]
[validateset( 'Cancellation' , 'Restart', 'Dump', 'Pause', 'Resume' )]
[string]
$Type
)
Expand Down Expand Up @@ -515,7 +518,7 @@ function Suspend-PodeServerInternal {

# Retrieve all runspaces related to Pode ordered by name so the Main runspace are the first to be suspended (To avoid the process hunging)
$runspaces = Get-Runspace | Where-Object { $_.Name -like 'Pode_*' -and `
$_.Name -notlike '*__pode_session_inmem_cleanup__*' } | Sort-Object Name
$_.Name -notlike '*__pode_session_inmem_cleanup__*' } | Sort-Object Name

foreach ($runspace in $runspaces) {
try {
Expand Down Expand Up @@ -550,7 +553,7 @@ function Suspend-PodeServerInternal {
$_ | Write-PodeErrorLog
}
finally {
Reset-PodeCancellationToken -Type SuspendResume
Reset-PodeCancellationToken -Type Pause

}
}
Expand Down Expand Up @@ -609,6 +612,6 @@ function Resume-PodeServerInternal {
}
finally {
# Reinitialize the CancellationTokenSource for future suspension/resumption
Reset-PodeCancellationToken -Type SuspendResume
Reset-PodeCancellationToken -Type Resume
}
}
8 changes: 5 additions & 3 deletions src/Public/Core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,14 @@ function Start-PodeServer {
Invoke-PodeDumpInternal -Format $PodeContext.Server.Debug.Dump.Format -Path $PodeContext.Server.Debug.Dump.Path -MaxDepth $PodeContext.Server.Debug.Dump.MaxDepth
}

if (($PodeContext.Tokens.Suspend.SuspendResume) -or (Test-PodeSuspendPressed -Key $key)) {
if (($PodeContext.Tokens.Suspend.IsCancellationRequested) -or ($PodeContext.Tokens.Resume.IsCancellationRequested) -or (Test-PodeSuspendPressed -Key $key)) {
Clear-PodeKeyPressed
if ( $PodeContext.Server.Suspended) {
$PodeContext.Tokens.Resume.Cancel()
Resume-PodeServerInternal
}
else {
$PodeContext.Tokens.Pause.Cancel()
Suspend-PodeServerInternal
}
}
Expand Down Expand Up @@ -401,7 +403,7 @@ function Resume-PodeServer {
[CmdletBinding()]
param()
if ( $PodeContext.Server.Suspended) {
$PodeContext.Tokens.SuspendResume.Cancel()
$PodeContext.Tokens.Resume.Cancel()
}
}

Expand All @@ -423,7 +425,7 @@ function Suspend-PodeServer {
[CmdletBinding()]
param()
if (! $PodeContext.Server.Suspended) {
$PodeContext.Tokens.SuspendResume.Cancel()
$PodeContext.Tokens.Pause.Cancel()
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/unit/Server.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ Describe 'Restart-PodeInternalServer' {
It 'Resetting the server values' {
$PodeContext = @{
Tokens = @{
Cancellation = [System.Threading.CancellationTokenSource]::new()
Restart = [System.Threading.CancellationTokenSource]::new()
Dump = [System.Threading.CancellationTokenSource]::new()
SuspendResume = [System.Threading.CancellationTokenSource]::new()
Cancellation = [System.Threading.CancellationTokenSource]::new()
Restart = [System.Threading.CancellationTokenSource]::new()
Dump = [System.Threading.CancellationTokenSource]::new()
Pause = [System.Threading.CancellationTokenSource]::new()
Resume = [System.Threading.CancellationTokenSource]::new()
}
Server = @{
Routes = @{
Expand Down

0 comments on commit 414d785

Please sign in to comment.