Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into Log-rest-syslog
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Feb 5, 2025
2 parents bf25899 + 6b23fc3 commit 545b5f1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
22 changes: 9 additions & 13 deletions src/Private/CancellationToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -386,7 +386,9 @@ function Test-PodeCancellationTokenRequest {
)

# Check if the specified token has an active cancellation request
return $PodeContext.Tokens[$Type].IsCancellationRequested
$cancelled = $PodeContext.Tokens[$Type].IsCancellationRequested

return $cancelled
}


Expand All @@ -400,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.
Expand All @@ -413,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
Expand All @@ -443,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
}
Expand Down
15 changes: 5 additions & 10 deletions src/Private/Console.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions src/Private/Server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,19 @@ 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
$PodeContext.Server.Started = $true

}
catch {
throw
Expand Down
7 changes: 2 additions & 5 deletions src/Public/Core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -306,22 +306,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
Expand Down

0 comments on commit 545b5f1

Please sign in to comment.