From 68f14b0dda7dcccd13fd7374423fe66ddb786458 Mon Sep 17 00:00:00 2001 From: mdaneri Date: Tue, 3 Dec 2024 17:43:12 -0800 Subject: [PATCH] tentative to use a new terminate token --- src/Private/Context.ps1 | 4 ++-- src/Private/Helpers.ps1 | 7 ++++++- src/Private/PodeServer.ps1 | 2 +- src/Private/Schedules.ps1 | 2 +- src/Private/Server.ps1 | 17 ++++++++++------- src/Public/Core.ps1 | 10 ++++++---- tests/unit/Server.Tests.ps1 | 5 +++-- 7 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Private/Context.ps1 b/src/Private/Context.ps1 index d2dab04ca..b19f00c11 100644 --- a/src/Private/Context.ps1 +++ b/src/Private/Context.ps1 @@ -414,9 +414,9 @@ function New-PodeContext { Cancellation = [System.Threading.CancellationTokenSource]::new() Restart = [System.Threading.CancellationTokenSource]::new() Dump = [System.Threading.CancellationTokenSource]::new() - Pause = [System.Threading.CancellationTokenSource]::new() + Suspend = [System.Threading.CancellationTokenSource]::new() Resume = [System.Threading.CancellationTokenSource]::new() - #Terminate = [System.Threading.CancellationTokenSource]::new() + Terminate = [System.Threading.CancellationTokenSource]::new() } # requests that should be logged diff --git a/src/Private/Helpers.ps1 b/src/Private/Helpers.ps1 index bba2dcb00..ae27291b1 100644 --- a/src/Private/Helpers.ps1 +++ b/src/Private/Helpers.ps1 @@ -747,9 +747,10 @@ function Close-PodeServerInternal { ) # ensure the token is cancelled - if ($null -ne $PodeContext.Tokens.Cancellation) { + if ($null -ne $PodeContext.Tokens.Cancellation -and $null -ne $PodeContext.Tokens.Cancellation) { Write-Verbose 'Cancelling main cancellation token' $PodeContext.Tokens.Cancellation.Cancel() + $PodeContext.Tokens.Terminate.Cancel() } # stop all current runspaces @@ -764,7 +765,11 @@ function Close-PodeServerInternal { # remove all the cancellation tokens Write-Verbose 'Disposing cancellation tokens' Close-PodeDisposable -Disposable $PodeContext.Tokens.Cancellation + Close-PodeDisposable -Disposable $PodeContext.Tokens.Terminate Close-PodeDisposable -Disposable $PodeContext.Tokens.Restart + Close-PodeDisposable -Disposable $PodeContext.Tokens.Dump + Close-PodeDisposable -Disposable $PodeContext.Tokens.Suspend + Close-PodeDisposable -Disposable $PodeContext.Tokens.Resume # dispose mutex/semaphores Write-Verbose 'Diposing mutex and semaphores' diff --git a/src/Private/PodeServer.ps1 b/src/Private/PodeServer.ps1 index 34e830f0d..d0f725fca 100644 --- a/src/Private/PodeServer.ps1 +++ b/src/Private/PodeServer.ps1 @@ -293,7 +293,7 @@ function Start-PodeWebServer { # start the runspace for listening on x-number of threads 1..$PodeContext.Threads.General | ForEach-Object { - Add-PodeRunspace -Type Web -Name 'Listener' -Id $_ -ScriptBlock $listenScript -Parameters @{ 'Listener' = $listener; 'ThreadId' = $_ } + Add-PodeRunspace -Type Web -Name 'Listener' -Id $_ -ScriptBlock $listenScript -Parameters @{ 'Listener' = $listener; 'ThreadId' = $_ } } } diff --git a/src/Private/Schedules.ps1 b/src/Private/Schedules.ps1 index decdc8bf9..007f45140 100644 --- a/src/Private/Schedules.ps1 +++ b/src/Private/Schedules.ps1 @@ -88,7 +88,7 @@ function Start-PodeScheduleRunspace { # complete any schedules Complete-PodeInternalSchedule -Now $_now - + # Calculate the remaining seconds to sleep until the next minute $remainingSeconds = 60 - [DateTime]::Now.Second diff --git a/src/Private/Server.ps1 b/src/Private/Server.ps1 index d6cd181fe..b2c8fd819 100644 --- a/src/Private/Server.ps1 +++ b/src/Private/Server.ps1 @@ -269,6 +269,7 @@ function Restart-PodeInternalServer { # cancel the session token $PodeContext.Tokens.Cancellation.Cancel() + $PodeContext.Tokens.Terminate.Cancel() # close all current runspaces Close-PodeRunspace -ClosePool @@ -377,8 +378,9 @@ function Restart-PodeInternalServer { Reset-PodeCancellationToken -Type Cancellation Reset-PodeCancellationToken -Type Restart Reset-PodeCancellationToken -Type Dump - Reset-PodeCancellationToken -Type Pause + Reset-PodeCancellationToken -Type Suspend Reset-PodeCancellationToken -Type Resume + Reset-PodeCancellationToken -Type Terminate # reload the configuration $PodeContext.Server.Configuration = Open-PodeConfiguration -Context $PodeContext @@ -424,8 +426,8 @@ function Restart-PodeInternalServer { Reset-PodeCancellationToken -Type Dump .EXAMPLE - # Reset the cancellation token for the 'Pause' type - Reset-PodeCancellationToken -Type Pause + # Reset the cancellation token for the 'Suspend' type + Reset-PodeCancellationToken -Type Suspend .NOTES This function is used to manage cancellation tokens in Pode's internal context. @@ -433,7 +435,7 @@ function Restart-PodeInternalServer { function Reset-PodeCancellationToken { param( [Parameter(Mandatory = $true)] - [validateset( 'Cancellation' , 'Restart', 'Dump', 'Pause', 'Resume' )] + [validateset( 'Cancellation' , 'Restart', 'Dump', 'Suspend', 'Resume', 'Terminate' )] [string] $Type ) @@ -515,6 +517,7 @@ function Suspend-PodeServerInternal { # Update the server's suspended state $PodeContext.Server.Suspended = $true + start-sleep 4 # 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 ` @@ -553,8 +556,8 @@ function Suspend-PodeServerInternal { $_ | Write-PodeErrorLog } finally { - Reset-PodeCancellationToken -Type Pause - + Reset-PodeCancellationToken -Type Suspend + #Reset-PodeCancellationToken -Type Cancellation } } @@ -587,7 +590,7 @@ function Resume-PodeServerInternal { # Update the server's suspended state $PodeContext.Server.Suspended = $false - # Pause briefly to ensure any required internal processes have time to stabilize + # Suspend briefly to ensure any required internal processes have time to stabilize Start-Sleep -Seconds 5 # Retrieve all runspaces related to Pode diff --git a/src/Public/Core.ps1 b/src/Public/Core.ps1 index 852249056..0d2e5e78d 100644 --- a/src/Public/Core.ps1 +++ b/src/Public/Core.ps1 @@ -253,11 +253,11 @@ function Start-PodeServer { 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-PodeServer Resume-PodeServerInternal } else { - $PodeContext.Tokens.Pause.Cancel() + Suspend-PodeServer Suspend-PodeServerInternal } } @@ -316,7 +316,7 @@ function Start-PodeServer { # Terminating... Write-PodeHost $PodeLocale.terminatingMessage -NoNewLine -ForegroundColor Yellow Invoke-PodeEvent -Type Terminate - $PodeContext.Tokens.Cancellation.Cancel() + Close-PodeServer } catch { $_ | Write-PodeErrorLog @@ -366,6 +366,7 @@ function Close-PodeServer { param() $PodeContext.Tokens.Cancellation.Cancel() + $PodeContext.Tokens.Terminate.Cancel() } <# @@ -425,7 +426,8 @@ function Suspend-PodeServer { [CmdletBinding()] param() if (! $PodeContext.Server.Suspended) { - $PodeContext.Tokens.Pause.Cancel() + $PodeContext.Tokens.Suspend.Cancel() + #$PodeContext.Tokens.Cancellation.Cancel() } } diff --git a/tests/unit/Server.Tests.ps1 b/tests/unit/Server.Tests.ps1 index e8c5a7a7f..1724fce3d 100644 --- a/tests/unit/Server.Tests.ps1 +++ b/tests/unit/Server.Tests.ps1 @@ -115,8 +115,9 @@ Describe 'Restart-PodeInternalServer' { 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() + Suspend = [System.Threading.CancellationTokenSource]::new() + Resume = [System.Threading.CancellationTokenSource]::new() + Terminate = [System.Threading.CancellationTokenSource]::new() } Server = @{ Routes = @{