Skip to content

Commit

Permalink
tentative to use a new terminate token
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Dec 4, 2024
1 parent 414d785 commit 68f14b0
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Private/Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/Private/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/Private/PodeServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' = $_ }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Private/Schedules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 10 additions & 7 deletions src/Private/Server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -424,16 +426,16 @@ 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.
#>
function Reset-PodeCancellationToken {
param(
[Parameter(Mandatory = $true)]
[validateset( 'Cancellation' , 'Restart', 'Dump', 'Pause', 'Resume' )]
[validateset( 'Cancellation' , 'Restart', 'Dump', 'Suspend', 'Resume', 'Terminate' )]
[string]
$Type
)
Expand Down Expand Up @@ -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 `
Expand Down Expand Up @@ -553,8 +556,8 @@ function Suspend-PodeServerInternal {
$_ | Write-PodeErrorLog
}
finally {
Reset-PodeCancellationToken -Type Pause

Reset-PodeCancellationToken -Type Suspend
#Reset-PodeCancellationToken -Type Cancellation
}
}

Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/Public/Core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -366,6 +366,7 @@ function Close-PodeServer {
param()

$PodeContext.Tokens.Cancellation.Cancel()
$PodeContext.Tokens.Terminate.Cancel()
}

<#
Expand Down Expand Up @@ -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()
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/unit/Server.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @{
Expand Down

0 comments on commit 68f14b0

Please sign in to comment.