From 414d785f1e80e3a83aa850415434ec2a8de6838c Mon Sep 17 00:00:00 2001 From: mdaneri Date: Tue, 3 Dec 2024 17:02:36 -0800 Subject: [PATCH] minor changes --- src/Private/Context.ps1 | 10 ++++++---- src/Private/Server.ps1 | 21 ++++++++++++--------- src/Public/Core.ps1 | 8 +++++--- tests/unit/Server.Tests.ps1 | 9 +++++---- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/Private/Context.ps1 b/src/Private/Context.ps1 index 70a6d1903..d2dab04ca 100644 --- a/src/Private/Context.ps1 +++ b/src/Private/Context.ps1 @@ -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 diff --git a/src/Private/Server.ps1 b/src/Private/Server.ps1 index ce8f68ec2..d6cd181fe 100644 --- a/src/Private/Server.ps1 +++ b/src/Private/Server.ps1 @@ -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 @@ -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 ) @@ -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 { @@ -550,7 +553,7 @@ function Suspend-PodeServerInternal { $_ | Write-PodeErrorLog } finally { - Reset-PodeCancellationToken -Type SuspendResume + Reset-PodeCancellationToken -Type Pause } } @@ -609,6 +612,6 @@ function Resume-PodeServerInternal { } finally { # Reinitialize the CancellationTokenSource for future suspension/resumption - Reset-PodeCancellationToken -Type SuspendResume + Reset-PodeCancellationToken -Type Resume } } \ No newline at end of file diff --git a/src/Public/Core.ps1 b/src/Public/Core.ps1 index e777c2157..852249056 100644 --- a/src/Public/Core.ps1 +++ b/src/Public/Core.ps1 @@ -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 } } @@ -401,7 +403,7 @@ function Resume-PodeServer { [CmdletBinding()] param() if ( $PodeContext.Server.Suspended) { - $PodeContext.Tokens.SuspendResume.Cancel() + $PodeContext.Tokens.Resume.Cancel() } } @@ -423,7 +425,7 @@ function Suspend-PodeServer { [CmdletBinding()] param() if (! $PodeContext.Server.Suspended) { - $PodeContext.Tokens.SuspendResume.Cancel() + $PodeContext.Tokens.Pause.Cancel() } } diff --git a/tests/unit/Server.Tests.ps1 b/tests/unit/Server.Tests.ps1 index 0b756a58d..e8c5a7a7f 100644 --- a/tests/unit/Server.Tests.ps1 +++ b/tests/unit/Server.Tests.ps1 @@ -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 = @{