From 1f77979cdb18a9d5316852320b12c8e111294cce Mon Sep 17 00:00:00 2001 From: mdaneri Date: Tue, 4 Feb 2025 09:36:34 -0800 Subject: [PATCH] FIxes FIx Suspend-PodeServer and Resume-PodeServer public functions FIx endpoint receiving data before Pode start event --- src/Private/CancellationToken.ps1 | 36 ++++++------------------------- src/Private/Console.ps1 | 15 +++++-------- src/Private/Server.ps1 | 8 ++++--- src/Public/Core.ps1 | 7 ++---- 4 files changed, 19 insertions(+), 47 deletions(-) diff --git a/src/Private/CancellationToken.ps1 b/src/Private/CancellationToken.ps1 index 13fbd140d..185df84f1 100644 --- a/src/Private/CancellationToken.ps1 +++ b/src/Private/CancellationToken.ps1 @@ -340,7 +340,7 @@ function Wait-PodeCancellationTokenRequest { # Wait for the token to be reset, with exponential back-off $count = 1 - while ($PodeContext.Tokens[$Type].IsCancellationRequested) { + while (! $PodeContext.Tokens[$Type].IsCancellationRequested) { Start-Sleep -Milliseconds (100 * $count) $count = [System.Math]::Min($count + 1, 20) } @@ -366,9 +366,6 @@ function Wait-PodeCancellationTokenRequest { - `Start` - `Disable` -.PARAMETER Wait - If specified, waits until the token's cancellation request becomes active before returning the result. - .OUTPUTS [bool] Returns `$true` if the specified token has an active cancellation request, otherwise `$false`. @@ -377,11 +374,6 @@ function Wait-PodeCancellationTokenRequest { Checks if the Restart token has an active cancellation request and returns `$true` or `$false`. -.EXAMPLE - Test-PodeCancellationTokenRequest -Type 'Suspend' -Wait - - Waits until the Suspend token has an active cancellation request before returning `$true` or `$false`. - .NOTES This function is an internal utility for Pode and may be subject to change in future releases. #> @@ -390,20 +382,12 @@ function Test-PodeCancellationTokenRequest { [Parameter(Mandatory = $true)] [ValidateSet('Cancellation', 'Restart', 'Suspend', 'Resume', 'Terminate', 'Start', 'Disable')] [string] - $Type, - - [switch] - $Wait + $Type ) # Check if the specified token has an active cancellation request $cancelled = $PodeContext.Tokens[$Type].IsCancellationRequested - # If -Wait is specified, block until the token's cancellation request becomes active - if ($Wait) { - Wait-PodeCancellationTokenRequest -Type $Type - } - return $cancelled } @@ -418,10 +402,6 @@ function Test-PodeCancellationTokenRequest { its operations. It interacts with the Pode server's context and state to perform the necessary operations based on the allowed actions and current state. -.PARAMETER serverState - The current state of the Pode server, retrieved using Get-PodeServerState, - which determines whether actions like suspend, disable, or restart can be executed. - .NOTES This is an internal function and may change in future releases of Pode. @@ -431,11 +411,9 @@ function Test-PodeCancellationTokenRequest { #> function Resolve-PodeCancellationToken { - param( - [Parameter(Mandatory = $true)] - [Pode.PodeServerState] - $ServerState - ) + + #Retrieve the current state of the Pode server + $serverState = Get-PodeServerState if ($PodeContext.Server.AllowedActions.Restart -and (Test-PodeCancellationTokenRequest -Type Restart)) { Restart-PodeInternalServer @@ -461,11 +439,11 @@ function Resolve-PodeCancellationToken { } # Handle suspend/resume actions if ($PodeContext.Server.AllowedActions.Suspend) { - if ((Test-PodeCancellationTokenRequest -Type Resume) -and ($ServerState -eq [Pode.PodeServerState]::Suspended)) { + if ((Test-PodeCancellationTokenRequest -Type Resume) -and ($ServerState -eq [Pode.PodeServerState]::Resuming)) { Resume-PodeServerInternal -Timeout $PodeContext.Server.AllowedActions.Timeout.Resume return } - elseif ((Test-PodeCancellationTokenRequest -Type Suspend) -and ($ServerState -eq [Pode.PodeServerState]::Running)) { + elseif ((Test-PodeCancellationTokenRequest -Type Suspend) -and ($ServerState -eq [Pode.PodeServerState]::Suspending)) { Suspend-PodeServerInternal -Timeout $PodeContext.Server.AllowedActions.Timeout.Suspend return } diff --git a/src/Private/Console.ps1 b/src/Private/Console.ps1 index dd50728fe..91cb4a442 100644 --- a/src/Private/Console.ps1 +++ b/src/Private/Console.ps1 @@ -944,10 +944,6 @@ function Get-PodeConsoleKey { The `Invoke-PodeConsoleAction` function uses a hashtable to define and centralize key mappings, allowing for easier updates and a cleaner implementation. -.PARAMETER serverState - The current state of the Pode server, retrieved using Get-PodeServerState, - which determines whether actions like suspend, disable, or restart can be executed. - .NOTES This function is part of Pode's internal utilities and may change in future releases. @@ -957,11 +953,10 @@ function Get-PodeConsoleKey { Processes the next key press or cancellation token to execute the corresponding server action. #> function Invoke-PodeConsoleAction { - param( - [Parameter(Mandatory = $true)] - [Pode.PodeServerState] - $ServerState - ) + + # Retrieve the current state of the server (e.g., Running, Suspended). + $serverState = Get-PodeServerState + # Get the next key press if console input is enabled $Key = Get-PodeConsoleKey if ($null -ne $key) { @@ -1427,7 +1422,7 @@ function Test-PodeHasConsole { return ([Pode.NativeMethods]::IsTerminal($handleTypeMap.Input) -and ` [Pode.NativeMethods]::IsTerminal($handleTypeMap.Output) -and ` [Pode.NativeMethods]::IsTerminal($handleTypeMap.Error) - ) + ) } return $false } diff --git a/src/Private/Server.ps1 b/src/Private/Server.ps1 index a87fe1546..37630a9c2 100644 --- a/src/Private/Server.ps1 +++ b/src/Private/Server.ps1 @@ -183,16 +183,18 @@ function Start-PodeInternalServer { } } - # Trigger the start - Close-PodeCancellationTokenRequest -Type Start # set the start time of the server (start and after restart) $PodeContext.Metrics.Server.StartTime = [datetime]::UtcNow + # Trigger the start + Close-PodeCancellationTokenRequest -Type Start + + Show-PodeConsoleInfo + # run running event hooks Invoke-PodeEvent -Type Running - Show-PodeConsoleInfo } catch { diff --git a/src/Public/Core.ps1 b/src/Public/Core.ps1 index 047fa891b..5d7054bd7 100644 --- a/src/Public/Core.ps1 +++ b/src/Public/Core.ps1 @@ -302,22 +302,19 @@ function Start-PodeServer { # Sit in a loop waiting for server termination/cancellation or a restart request. while (!(Test-PodeCancellationTokenRequest -Type Terminate)) { - # Retrieve the current state of the server (e.g., Running, Suspended). - $serverState = Get-PodeServerState # If console input is not disabled, invoke any actions based on console commands. if (!$PodeContext.Server.Console.DisableConsoleInput) { - Invoke-PodeConsoleAction -ServerState $serverState + Invoke-PodeConsoleAction } # Resolve cancellation token requests (e.g., Restart, Enable/Disable, Suspend/Resume). - Resolve-PodeCancellationToken -ServerState $serverState + Resolve-PodeCancellationToken # Pause for 1 second before re-checking the state and processing the next action. Start-Sleep -Seconds 1 } - if ($PodeContext.Server.IsIIS -and $PodeContext.Server.IIS.Shutdown) { # (IIS Shutdown) Write-PodeHost $PodeLocale.iisShutdownMessage -NoNewLine -ForegroundColor Yellow