Pode icon indicating copy to clipboard operation
Pode copied to clipboard

Difficulty Accessing Comprehensive Asynchronous Task Information in Pode

Open mdaneri opened this issue 11 months ago • 1 comments

Description

I want to run asynchronous tasks via Add-PodeTask followed by Invoke-PodeTask. While Invoke-PodeTask successfully returns a detailed hashtable of task information immediately after invocation, I encounter difficulties accessing similar detailed information later on, especially through RESTful API endpoints. The detailed information includes properties like ID, CompletedTime, Task, Runspace, Timeout, ExpireTime, and Result. This limitation hinders my ability to monitor and manage asynchronous tasks effectively in a production environment.

Steps to Reproduce (complete sample attached)

  1. Setup a Pode server and define an endpoint to initiate an asynchronous task using Add-PodeTask and Invoke-PodeTask.
  2. Invoke the task through the defined endpoint and capture the detailed task information returned by Invoke-PodeTask.
  3. Attempt to retrieve similar task information at a later time or via a different endpoint (e.g., /api/task/:taskId).

Expected Behavior

I expect to be able to retrieve comprehensive task information (similar to what Invoke-PodeTask returns immediately after task invocation) at any point during the task's lifecycle, including after its completion, via RESTful API endpoints or other Pode functions.

Actual Behavior

Post-execution, I am only able to determine if the task is running or not. Detailed information about the task, like what is available immediately after using Invoke-PodeTask, is not accessible through RESTful API endpoints or subsequent queries.

There is also an issue with Test-PodeTaskCompleted that always returns False

$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'
}

# or just:
# Import-Module Pode

# create a basic server
Start-PodeServer -EnablePool Tasks {

    Add-PodeEndpoint -Address localhost -Port 8081 -Protocol Http
    New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
    Enable-PodeOpenApi -Path '/docs/openapi' -OpenApiVersion '3.0.3' -DisableMinimalDefinitions -NoDefaultResponses
    Add-PodeOAInfo -Title 'Async test' -Version 1.0.0
    Add-PodeOAServerEndpoint -url '/api' -Description 'default endpoint'


    Enable-PodeOAViewer -Type Swagger -Path '/docs/swagger'
    Enable-PodeOAViewer -Type ReDoc -Path '/docs/redoc' -DarkMode
    Enable-PodeOAViewer -Type RapiDoc -Path '/docs/rapidoc' -DarkMode
    Enable-PodeOAViewer -Type StopLight -Path '/docs/stoplight' -DarkMode
    Enable-PodeOAViewer -Type Explorer -Path '/docs/explorer' -DarkMode
    Enable-PodeOAViewer -Type RapiPdf -Path '/docs/rapipdf' -DarkMode

    Enable-PodeOAViewer -Editor -Path '/docs/swagger-editor'
    Enable-PodeOAViewer -Bookmarks -Path '/docs'


    Add-PodeRoute -Method Post -Path '/api/task' -ScriptBlock {
        $sleepTime = $WebEvent.Query['sleepTime']

        $name = (New-Guid).ToString()
        $startTime = Get-Date
        Add-PodeTask -Name $name -ScriptBlock {
            param($sleepTime)
            Write-PodeHost "Start $sleepTime"
            for ($progress = 0; $progress -le 100; $progress++) {
                Start-Sleep -Milliseconds $sleepTime
                Write-PodeHost -NoNewLine '.'
            }
            Write-PodeHost
            Write-PodeHost 'End'
        } -ArgumentList @{sleepTime = $sleepTime } | Out-Null
        $task = Invoke-PodeTask -Name $name
        #   $task = Get-PodeTask -Name $name
        Write-PodeHost $task -Explode -ShowType
        Write-PodeJsonResponse -Value @{
            StartTime = $startTime
            taskId    = $name
            Done      = Test-PodeTaskCompleted -Task $task
        }
    } -PassThru | Set-PodeOARouteInfo -Summary 'Create Task' -Tags 'task' -OperationId 'newTask' -PassThru |
        Set-PodeOARequest -Parameters  (  New-PodeOAIntProperty -Name 'sleepTime' | ConvertTo-PodeOAParameter -In Query -Required ) -PassThru |
        Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation'

    Add-PodeRoute -Method Get -Path '/api/task/:taskId' -ScriptBlock {
        $taskId = $WebEvent.Parameters['taskId']
        $task = Get-PodeTask -Name $taskId
        write-podehost -Object $task -Explode -ShowType
        $done = Test-PodeTaskCompleted -Task $task
        if ($done) {
            Write-PodeJsonResponse -Value @{
                # StartTime = $startTime
                taskId = $taskId
                Done   = Test-PodeTaskCompleted -Task $task
                Result = $task.Result
            }
        }
        else {
            Write-PodeJsonResponse -Value @{
                taskId = $taskId
                Done   = $false
            }

        }
    }    -PassThru | Set-PodeOARouteInfo -Summary 'Check Task' -Tags 'task' -OperationId 'getTask' -PassThru |
        Set-PodeOARequest -Parameters  (  New-PodeOAStringProperty -Name 'taskId' -Format Uuid | ConvertTo-PodeOAParameter -In Path -Required) -PassThru |
        Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation' #-Content @{'application/json' = 'ApiResponse' }

}

mdaneri avatar Mar 04 '24 01:03 mdaneri

linked to #1037 - which has planned improvements for Tasks, including a way to fetch the information of a running task.

Badgerati avatar Mar 11 '24 22:03 Badgerati

Closing, as a new Get-PodeTaskProcess was added via #1393 which now enables the ability to retrieve running Task process details similar to what Invoke-PodeTask returns.

Badgerati avatar Sep 21 '24 17:09 Badgerati