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

Difficulty Accessing Comprehensive Asynchronous Task Information in Pode #1248

Open
mdaneri opened this issue Mar 4, 2024 · 1 comment
Open

Comments

@mdaneri
Copy link
Contributor

mdaneri commented Mar 4, 2024

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

}
@Badgerati
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants