Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test-PodeTaskCompleted Consistently Returns $false After Task Completion #1249

Closed
mdaneri opened this issue Mar 4, 2024 · 4 comments
Closed
Labels

Comments

@mdaneri
Copy link
Contributor

mdaneri commented Mar 4, 2024

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
        }
    }
}
@Badgerati
Copy link
Owner

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

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

@mdaneri
Copy link
Contributor Author

mdaneri commented Mar 11, 2024

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

@Badgerati
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

2 participants