Skip to content

Commit

Permalink
Added start token
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Dec 10, 2024
1 parent a1c15a7 commit 57a9d00
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 65 deletions.
149 changes: 104 additions & 45 deletions src/Private/CancellationToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
function Reset-PodeCancellationToken {
param(
[Parameter(Mandatory = $true)]
[validateset( 'Cancellation' , 'Restart', 'Suspend', 'Resume', 'Terminate' )]
[validateset( 'Cancellation' , 'Restart', 'Suspend', 'Resume', 'Terminate', 'Start' )]
[string[]]
$Type
)
Expand All @@ -41,52 +41,66 @@ function Reset-PodeCancellationToken {
})
}


<#
.SYNOPSIS
Closes specified cancellation tokens in the Pode context.
Closes and disposes of specified cancellation tokens in the Pode context.
.DESCRIPTION
The `Close-PodeCancellationToken` function ensures proper cleanup of disposable cancellation tokens
within the `$PodeContext`. It takes one or more token types as input and disposes of the corresponding tokens.
The `Close-PodeCancellationToken` function ensures proper cleanup of disposable cancellation tokens
within the `$PodeContext`. It allows you to specify one or more token types to close and dispose of,
or you can dispose of all tokens if no type is specified.
Supported token types include:
- `Cancellation`
- `Restart`
- `Suspend`
- `Resume`
- `Terminate`
Supported token types include:
- `Cancellation`
- `Restart`
- `Suspend`
- `Resume`
- `Terminate`
- `Start`
This function is useful for managing and cleaning up resources during the lifecycle of a Pode application.
This function is essential for managing resources during the lifecycle of a Pode application,
especially when cleaning up during shutdown or restarting.
.PARAMETER Type
Specifies the type(s) of cancellation tokens to close. Valid values are:
`Cancellation`, `Restart`, `Suspend`, `Resume`, `Terminate`.
Specifies the type(s) of cancellation tokens to close. Valid values are:
`Cancellation`, `Restart`, `Suspend`, `Resume`, `Terminate`, `Start`.
If this parameter is not specified, all tokens in `$PodeContext.Tokens` will be disposed of.
.EXAMPLE
Close-PodeCancellationToken -Type 'Suspend'
Closes and disposes of the `Suspend` cancellation token in the Pode context.
.EXAMPLE
Close-PodeCancellationToken -Type 'Suspend'
Closes the `Suspend` cancellation token in the Pode context.
Close-PodeCancellationToken -Type 'Restart', 'Terminate'
Closes and disposes of the `Restart` and `Terminate` cancellation tokens in the Pode context.
.EXAMPLE
Close-PodeCancellationToken -Type 'Restart', 'Terminate'
Closes the `Restart` and `Terminate` cancellation tokens in the Pode context.
Close-PodeCancellationToken
Closes and disposes of all tokens in the Pode context.
.NOTES
This is an internal function and may change in future releases of Pode.
This is an internal function and may change in future releases of Pode.
#>


function Close-PodeCancellationToken {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateSet('Cancellation', 'Restart', 'Suspend', 'Resume', 'Terminate')]
[Parameter()]
[ValidateSet('Cancellation', 'Restart', 'Suspend', 'Resume', 'Terminate', 'Start' )]
[string[]]
$Type
)

foreach ($tokenType in $Type) {
# Ensure cleanup of disposable tokens
Close-PodeDisposable -Disposable $PodeContext.Tokens[$tokenType]
if ($null -eq $Type) {
$PodeContext.Tokens.Values | Close-PodeDisposable
}
else {
foreach ($tokenType in $Type) {
# Ensure cleanup of disposable tokens
Close-PodeDisposable -Disposable $PodeContext.Tokens[$tokenType]
}
}
}

Expand Down Expand Up @@ -127,42 +141,87 @@ function Test-PodeSuspensionToken {
return $suspended
}


<#
.SYNOPSIS
Creates a set of cancellation tokens for managing Pode application states.
Creates a set of cancellation tokens for managing Pode application states.
.DESCRIPTION
The `New-PodeSuspensionToken` function initializes and returns a hashtable containing
multiple cancellation tokens used for managing various states in a Pode application.
These tokens enable coordinated control over application operations, such as cancellation,
restart, suspension, resumption, and termination.
The returned hashtable includes the following keys:
- `Cancellation`: A token for general cancellation tasks.
- `Restart`: A token for managing application restarts.
- `Suspend`: A token for handling suspension operations.
- `Resume`: A token for resuming operations after suspension.
- `Terminate`: A token for managing application termination.
The `New-PodeSuspensionToken` function initializes and returns a hashtable containing
multiple cancellation tokens used for managing various states in a Pode application.
These tokens provide coordinated control over application operations, such as cancellation,
restart, suspension, resumption, termination, and start operations.
The returned hashtable includes the following keys:
- `Cancellation`: A token specifically for managing endpoint cancellation tasks.
- `Restart`: A token for managing application restarts.
- `Suspend`: A token for handling suspension operations.
- `Resume`: A token for resuming operations after suspension.
- `Terminate`: A token for managing application termination.
- `Start`: A token for monitoring application startup.
.EXAMPLE
$tokens = New-PodeSuspensionToken
Initializes a set of cancellation tokens and stores them in the `$tokens` variable.
$tokens = New-PodeSuspensionToken
Initializes a set of cancellation tokens and stores them in the `$tokens` variable.
.OUTPUTS
[hashtable]
A hashtable containing initialized cancellation tokens.
[hashtable]
A hashtable containing initialized cancellation tokens.
.NOTES
This is an internal function and may change in future releases of Pode.
This is an internal function and may change in future releases of Pode.
#>

function New-PodeSuspensionToken {
# Initialize and return a hashtable containing various cancellation tokens.
return @{
# A cancellation token specifically for managing endpoint cancellation tasks.
Cancellation = [System.Threading.CancellationTokenSource]::new()

# A cancellation token specifically for managing application restart operations.
Restart = [System.Threading.CancellationTokenSource]::new()

# A cancellation token for suspending operations in the Pode application.
Suspend = [System.Threading.CancellationTokenSource]::new()

# A cancellation token for resuming operations after a suspension.
Resume = [System.Threading.CancellationTokenSource]::new()

# A cancellation token for managing application termination.
Terminate = [System.Threading.CancellationTokenSource]::new()

# A cancellation token for monitoring application startup.
Start = [System.Threading.CancellationTokenSource]::new()
}
}
}


<#
.SYNOPSIS
Waits for the Pode server to start by monitoring the cancellation token.
.DESCRIPTION
This function repeatedly checks the `$PodeContext.Tokens.Start` cancellation token to determine if the Pode server has started.
It pauses execution using `Start-Sleep` until the cancellation request is received.
.EXAMPLE
Wait-PodeStartToken
This example waits for the Pode server to start before proceeding with the rest of the script.
.NOTES
This function is designed for internal use and may change in future releases of Pode.
.PARAMETER None
This function does not take any parameters.
.OUTPUTS
None
#>
function Wait-PodeStartToken {
while ( !$PodeContext.Tokens.Start.IsCancellationRequested) {
Start-Sleep 1
}
}



2 changes: 2 additions & 0 deletions src/Private/FileWatchers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function Start-PodeFileWatcherRunspace {
[int]
$ThreadId
)
# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken
do {
try {
while ($Watcher.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
Expand Down
3 changes: 3 additions & 0 deletions src/Private/Gui.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ function Start-PodeGuiRunspace {
}

$script = {
# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken

try {
# if there are multiple endpoints, flag warning we're only using the first - unless explicitly set
if ($null -eq $PodeContext.Server.Gui.Endpoint) {
Expand Down
2 changes: 1 addition & 1 deletion src/Private/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ function Close-PodeServerInternal {
try {
# remove all the cancellation tokens
Write-Verbose 'Disposing cancellation tokens'
Close-PodeCancellationToken -Type Cancellation, Terminate, Restart, Suspend, Resume
Close-PodeCancellationToken #-Type Cancellation, Terminate, Restart, Suspend, Resume, Start

# dispose mutex/semaphores
Write-Verbose 'Diposing mutex and semaphores'
Expand Down
8 changes: 8 additions & 0 deletions src/Private/PodeServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ function Start-PodeWebServer {
[int]
$ThreadId
)
# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken
do {
try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
Expand Down Expand Up @@ -309,6 +311,9 @@ function Start-PodeWebServer {
[Parameter(Mandatory = $true)]
$Listener
)
# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken

do {
try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
Expand Down Expand Up @@ -392,6 +397,9 @@ function Start-PodeWebServer {
$ThreadId
)

# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken

do {
try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
Expand Down
3 changes: 3 additions & 0 deletions src/Private/Schedules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ function Start-PodeScheduleRunspace {
$script = {
try {

# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken

# select the schedules that trigger on-start
$_now = [DateTime]::Now

Expand Down
12 changes: 12 additions & 0 deletions src/Private/Server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ function Start-PodeInternalServer {

Show-PodeConsoleInfo -ShowHeader

# Trigger the start
$PodeContext.Tokens.Start.Cancel()

}
catch {
throw
Expand Down Expand Up @@ -257,6 +260,7 @@ function Show-PodeConsoleInfo {

function Restart-PodeInternalServer {
try {
Reset-PodeCancellationToken -Type Start
# inform restart
# Restarting server...
Write-PodeHost $PodeLocale.restartingServerMessage -NoNewline -ForegroundColor Yellow
Expand Down Expand Up @@ -383,6 +387,14 @@ function Restart-PodeInternalServer {
# restart the server
$PodeContext.Metrics.Server.RestartCount++

# reset tokens if needed
if ( $PodeContext.Tokens.Cancellation.IsCancellationRequested) {
Reset-PodeCancellationToken -Type Cancellation
}
if ( $PodeContext.Tokens.Suspend.IsCancellationRequested) {
Reset-PodeCancellationToken -Type Suspend
}

# Update the server's suspended state
$PodeContext.Server.SuspensionState.Suspended = $false
Start-PodeInternalServer
Expand Down
3 changes: 3 additions & 0 deletions src/Private/SmtpServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ function Start-PodeSmtpServer {
$ThreadId
)

# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken

do {
try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
Expand Down
2 changes: 2 additions & 0 deletions src/Private/TcpServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ function Start-PodeTcpServer {
[int]
$ThreadId
)
# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken

do {
try {
Expand Down
4 changes: 3 additions & 1 deletion src/Private/Timers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ function Start-PodeTimerRunspace {
}

$script = {
try {
# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken

try {
while (!$PodeContext.Tokens.Terminate.IsCancellationRequested) {
# Check for suspension token and wait for the debugger to reset if active
Test-PodeSuspensionToken
Expand Down
2 changes: 2 additions & 0 deletions src/Private/WebSockets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ function Start-PodeWebSocketRunspace {
[int]
$ThreadId
)
# Waits for the Pode server to fully start before proceeding with further operations.
Wait-PodeStartToken

do {
try {
Expand Down
Loading

0 comments on commit 57a9d00

Please sign in to comment.