Pode icon indicating copy to clipboard operation
Pode copied to clipboard

`Test-PodeTaskCompleted` Consistently Returns `$false` After Task Completion

Open mdaneri opened this issue 1 year ago • 3 comments

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

  1. Run the provided Pode server script that listens on port 8082.
  2. Access the GET /start-task endpoint through a browser or a tool like curl.
  3. 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
        }
    }
}

mdaneri avatar Mar 04 '24 01:03 mdaneri

The object from Invoke-PodeTask is what should be passed to Test-PodeTaskCompleted.

(linked to #1037 - which has planned improvements for Tasks)

Badgerati avatar Mar 11 '24 22:03 Badgerati

I know that it works. But it's not suitable for rest asynchronous calls. Because I cannot implement a get task call

mdaneri avatar Mar 11 '24 22:03 mdaneri

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 🤔

Badgerati avatar Mar 11 '24 23:03 Badgerati

Closing as from #1393 there's now a Get-PodeTaskProcess which can be used to retrieve the Task process object for Test-PodeTaskCompleted

Badgerati avatar Sep 21 '24 17:09 Badgerati