Pode
Pode copied to clipboard
`Test-PodeTaskCompleted` Consistently Returns `$false` After Task Completion
Bug Description
When using Pode to run asynchronous tasks, I've encountered an issue where Test-PodeTaskCompleted returns $false for a task, even after ensuring the task has had sufficient time to complete. This occurs in a scenario where a task is added and invoked, and after a deliberate delay (longer than the task's expected completion time), Test-PodeTaskCompleted is used to check the task's completion status.
Steps to Reproduce
- Run the provided Pode server script that listens on port 8082.
- Access the
GET /start-taskendpoint through a browser or a tool likecurl. - Observe the JSON response indicating whether the task is completed.
Expected Behavior
After the task has been given enough time to complete (in this case, 5 seconds of task execution followed by a 10-second script delay), Test-PodeTaskCompleted should return $true, indicating the task has been completed.
Actual Behavior
Test-PodeTaskCompleted returns $false, suggesting the task has not been completed, despite the sufficient wait time provided.
The issue I think, is related to Get-PodeTask that doesn't return information regarding the runspace.
Sample Code
# Start the Pode server
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
if (Test-Path -Path "$($path)/src/Pode.psm1" -PathType Leaf) {
Import-Module "$($path)/src/Pode.psm1" -Force -ErrorAction Stop
}
else {
Import-Module -Name 'Pode'
}
Start-PodeServer {
Add-PodeEndpoint -Address localhost -Port 8082 -Protocol Http
# Define an endpoint to start a task
Add-PodeRoute -Method Get -Path '/start-task' -ScriptBlock {
# Define and start a simple task
$taskId = (New-Guid).ToString()
Add-PodeTask -name $taskId -ScriptBlock {
Start-Sleep -Seconds 5 # Simulate task work
return 'Task completed'
}
Invoke-PodeTask -Name $taskId | Out-Null
Start-Sleep -Seconds 10
$task2 = Get-PodeTask -Name $taskId
# Attempt to check if the task is completed
$isCompleted = Test-PodeTaskCompleted -Task $task2
# Return the completion status
Write-PodeJsonResponse -Value @{
TaskId = $taskId
IsCompleted = $isCompleted
}
}
}
The object from Invoke-PodeTask is what should be passed to Test-PodeTaskCompleted.
(linked to #1037 - which has planned improvements for Tasks)
I know that it works. But it's not suitable for rest asynchronous calls. Because I cannot implement a get task call
Yeah I agree, and it's why #1037 exists 😄
As a workaround it should be possible to store the Task object from Invoke-PodeTask in State, and retrieve them that way when needed in other routes 🤔
Closing as from #1393 there's now a Get-PodeTaskProcess which can be used to retrieve the Task process object for Test-PodeTaskCompleted