Skip to content

Commit

Permalink
looking good
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Dec 5, 2024
1 parent 68f14b0 commit edc604e
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 39 deletions.
2 changes: 2 additions & 0 deletions examples/Tasks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Start-PodeServer {
Add-PodeTask -Name 'Test2' -ScriptBlock {
param($value)
Start-Sleep -Seconds 10
"a $($value) is comming" | Out-Default
Start-Sleep -Seconds 100
"a $($value) is never late, it arrives exactly when it means to" | Out-Default
}

Expand Down
22 changes: 19 additions & 3 deletions examples/Web-Dump.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
http://localhost:8081/openapi
Documentation:
http://localhost:8081/docs
5
.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Web-Dump.ps1
Expand All @@ -39,7 +39,7 @@ try {
catch { throw }

# Start Pode server with specified script block
Start-PodeServer -Threads 4 -ScriptBlock {
Start-PodeServer -Threads 4 -EnablePool Tasks -ScriptBlock {

# listen on localhost:8081
Add-PodeEndpoint -Address localhost -Port 8081 -Protocol Http
Expand Down Expand Up @@ -94,7 +94,12 @@ Start-PodeServer -Threads 4 -ScriptBlock {
}
} | Set-PodeOARouteInfo -Summary 'Dump state' -Description 'Dump the memory state of the server.' -Tags 'dump' -OperationId 'dump'-PassThru |
Set-PodeOARequest -Parameters (New-PodeOAStringProperty -Name 'format' -Description 'Dump export format.' -Enum 'json', 'clixml', 'txt', 'bin', 'yaml' -Default 'json' | ConvertTo-PodeOAParameter -In Query )
}

Add-PodeRoute -Method Get -Path '/task/async' -PassThru -ScriptBlock {
Invoke-PodeTask -Name 'Test' -ArgumentList @{ value = 'wizard' } | Out-Null
Write-PodeJsonResponse -Value @{ Result = 'jobs done' }
}| Set-PodeOARouteInfo -Summary 'Task'
}

Add-PodeVerb -Verb 'HELLO' -ScriptBlock {
Write-PodeTcpClient -Message 'HI'
Expand Down Expand Up @@ -148,4 +153,15 @@ Start-PodeServer -Threads 4 -ScriptBlock {
$name = Read-PodeTcpClient -CRLFMessageEnd
Write-PodeTcpClient -Message "Hi, $($name)!"
}


Add-PodeTask -Name 'Test' -ScriptBlock {
param($value)
Start-PodeSleep -Seconds 10
"a $($value) is comming" | Out-Default
Start-PodeSleep -Seconds 100
"a $($value) is never late, it arrives exactly when it means to" | Out-Default
}


}
46 changes: 31 additions & 15 deletions src/Private/Dump.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ function Invoke-PodeDumpInternal {

# Retrieve all runspaces related to Pode ordered by name
$runspaces = Get-Runspace | Where-Object { $_.Name -like 'Pode_*' -and `
$_.Name -notlike '*__pode_session_inmem_cleanup__*' } | Sort-Object Name
$_.Name -notlike '*__pode_session_inmem_cleanup__*' -and `
$_.Name -notlike 'Pode_*_Listener_*' -and `
$_.Name -notlike 'Pode_*_KeepAlive_*' -and `
$_.Name -notlike 'Pode_Signals_Broadcaster_*'
} | Sort-Object Name


$runspaceDetails = @{}
Expand Down Expand Up @@ -234,8 +238,8 @@ function Invoke-PodeDumpInternal {
Write-PodeHost -ForegroundColor Yellow "Memory dump saved to $dumpFilePath"
}
end {

Reset-PodeCancellationToken -Type 'Dump'
Reset-PodeCancellationToken -Type Cancellation
Reset-PodeCancellationToken -Type Dump
}
}

Expand Down Expand Up @@ -287,25 +291,33 @@ function Get-PodeRunspaceVariablesViaDebugger {

# Initialize variables collection
$variables = @(@{})

for ($i = 0; $i -le 3; $i++) {

try {

# Attach the debugger and break all

# Wait for the event to be triggered or timeout
Write-PodeHost "Waiting for $($Runspace.Name) to enter in debug ." -NoNewLine

$debugger = [Pode.Embedded.DebuggerHandler]::new($Runspace)
Enable-RunspaceDebug -BreakAll -Runspace $Runspace

# Wait for the event to be triggered or timeout
$startTime = [DateTime]::UtcNow
Start-Sleep -Milliseconds 500

Write-PodeHost "Waiting for $($Runspace.Name) to enter in debug ." -NoNewLine
Write-PodeHost '.' -NoNewLine

# Suspend the runspace
if (Suspend-PodeRunspace -Runspace $Runspace) {
# Retrieve and output the collected variables from the embedded C# code
$variables = $debugger.Variables
break
while (!$debugger.IsEventTriggered) {
Start-Sleep -Milliseconds 1000
Write-PodeHost '.' -NoNewLine
if (([DateTime]::UtcNow - $startTime).TotalSeconds -ge $Timeout) {
Write-PodeHost "Failed (Timeout reached after $Timeout seconds.)"
return $variables[0]
}
}

return $debugger.Variables
}
catch {
# Log the error details using Write-PodeErrorLog.
Expand Down Expand Up @@ -710,23 +722,26 @@ function Suspend-PodeRunspace {
param (
$Runspace
)

# Attach the debugger and break all
$debugger = [Pode.Embedded.DebuggerHandler]::new($Runspace)
Enable-RunspaceDebug -BreakAll -Runspace $Runspace
# Start-Sleep -Milliseconds 500
# Enable-RunspaceDebug -BreakAll -Runspace $Runspace

# Wait for the event to be triggered or timeout
$startTime = [DateTime]::UtcNow
Start-Sleep -Milliseconds 1000
Start-Sleep -Milliseconds 500
# Write-PodeHost '..' -NoNewLine

# Send-PodeInterrupt -Name $Runspace.Name
#Send-PodeInterrupt -Name $Runspace.Name

Write-PodeHost '.' -NoNewLine

while (!$debugger.IsEventTriggered) {
Start-Sleep -Milliseconds 1000

if (([int]([DateTime]::UtcNow - $startTime).TotalSeconds) % 5 -eq 0) {
<# if (([int]([DateTime]::UtcNow - $startTime).TotalSeconds) % 5 -eq 0) {
if (Send-PodeInterrupt -Name $Runspace.Name) {
Write-PodeHost '*' -NoNewLine
}
Expand All @@ -736,7 +751,8 @@ function Suspend-PodeRunspace {
}
else {
Write-PodeHost '.' -NoNewLine
}
}#>
Write-PodeHost '.' -NoNewLine
if (([DateTime]::UtcNow - $startTime).TotalSeconds -ge $Timeout) {
Write-PodeHost "Failed (Timeout reached after $Timeout seconds.)"
return $false
Expand Down
16 changes: 14 additions & 2 deletions src/Private/FileWatchers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ function Start-PodeFileWatcherRunspace {
)

try {
while ($Watcher.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Watcher.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ($PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
$evt = (Wait-PodeTask -Task $Watcher.GetFileEventAsync($PodeContext.Tokens.Cancellation.Token))

try {
Expand Down Expand Up @@ -144,7 +150,13 @@ function Start-PodeFileWatcherRunspace {
)

try {
while ($Watcher.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Watcher.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ($PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
Start-Sleep -Seconds 1
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Private/Gui.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Start-PodeGuiRunspace {
# poll the server for a response
$count = 0

while (!$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while (!$PodeContext.Tokens.Terminate.IsCancellationRequested) {
try {
$null = Invoke-WebRequest -Method Get -Uri $uri -UseBasicParsing -ErrorAction Stop
if (!$?) {
Expand Down
8 changes: 7 additions & 1 deletion src/Private/Logging.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,13 @@ function Start-PodeLoggingRunspace {

$script = {
try {
while (!$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while (!$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ($PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
try {
# if there are no logs to process, just sleep for a few seconds - but after checking the batch
if ($PodeContext.LogsToProcess.Count -eq 0) {
Expand Down
32 changes: 28 additions & 4 deletions src/Private/PodeServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ function Start-PodeWebServer {
)

try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ($PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
# get request and response
$context = (Wait-PodeTask -Task $Listener.GetContextAsync($PodeContext.Tokens.Cancellation.Token))

Expand Down Expand Up @@ -307,7 +313,13 @@ function Start-PodeWebServer {
)

try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ($PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
$message = (Wait-PodeTask -Task $Listener.GetServerSignalAsync($PodeContext.Tokens.Cancellation.Token))

try {
Expand Down Expand Up @@ -385,7 +397,13 @@ function Start-PodeWebServer {
)

try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ($PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
$context = (Wait-PodeTask -Task $Listener.GetClientSignalAsync($PodeContext.Tokens.Cancellation.Token))

try {
Expand Down Expand Up @@ -464,7 +482,13 @@ function Start-PodeWebServer {
)

try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ($PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
Start-Sleep -Seconds 1
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/Private/Schedules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ function Start-PodeScheduleRunspace {
# first, sleep for a period of time to get to 00 seconds (start of minute)
Start-Sleep -Seconds (60 - [DateTime]::Now.Second)

while (!$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while (!$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ( $PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
try {
$_now = [DateTime]::Now

Expand Down Expand Up @@ -95,7 +101,8 @@ function Start-PodeScheduleRunspace {
# Loop in 5-second intervals until the remaining seconds are covered
while ($remainingSeconds -gt 0) {
$sleepTime = [math]::Min(5, $remainingSeconds) # Sleep for 5 seconds or remaining time
Start-Sleep -Seconds $sleepTime
# Start-Sleep -Seconds $sleepTime
Start-PodeSleep -Seconds $sleepTime
$remainingSeconds -= $sleepTime
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Private/ServiceServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Start-PodeServiceServer {
$serverScript = {

try {
while (!$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while (!$PodeContext.Tokens.Terminate.IsCancellationRequested) {
# the event object
$script:ServiceEvent = @{
Lockable = $PodeContext.Threading.Lockables.Global
Expand Down
16 changes: 14 additions & 2 deletions src/Private/SmtpServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ function Start-PodeSmtpServer {
)

try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ($PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
# get email
$context = (Wait-PodeTask -Task $Listener.GetContextAsync($PodeContext.Tokens.Cancellation.Token))

Expand Down Expand Up @@ -189,7 +195,13 @@ function Start-PodeSmtpServer {
)

try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ( $PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
Start-Sleep -Seconds 1
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/Private/TcpServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ function Start-PodeTcpServer {
)

try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ( $PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
# get email
$context = (Wait-PodeTask -Task $Listener.GetContextAsync($PodeContext.Tokens.Cancellation.Token))

Expand Down Expand Up @@ -207,7 +213,13 @@ function Start-PodeTcpServer {
)

try {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while ($Listener.IsConnected -and !$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ( $PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
Start-Sleep -Seconds 1
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/Private/Timers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ function Start-PodeTimerRunspace {
$script = {
try {

while (!$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
while (!$PodeContext.Tokens.Terminate.IsCancellationRequested) {
while ($PodeContext.Tokens.Suspend.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
while ($PodeContext.Tokens.Dump.IsCancellationRequested) {
Start-Sleep -Seconds 1
}
try {
$_now = [DateTime]::Now

Expand Down
Loading

0 comments on commit edc604e

Please sign in to comment.