Badgerati / Pode

Pode is a Cross-Platform PowerShell web framework for creating REST APIs, Web Sites, and TCP/SMTP servers
https://badgerati.github.io/Pode
MIT License
865 stars 92 forks source link

Azure Function Add-PodeTimer exception even though it's not being used. #1435

Closed mdg-pnw closed 3 weeks ago

mdg-pnw commented 1 month ago

Describe the Bug

I'm following: https://badgerati.github.io/Pode/Hosting/AzureFunctions/#the-server

When I run this, I am getting an exception even though I don't have Add-PodeTimers in my script. It seems like it's not recognizing that it's serverless.

Steps To Reproduce

using namespace System.Net

    param($Request, $TriggerMetadata)

    $endpoint = '/api/isops'

    Start-PodeServer -Request $TriggerMetadata -ServerlessType AzureFunctions -RootPath '../www' {
        # get route that can return data
        Add-PodeRoute -Method Get -Path $endpoint -ScriptBlock {
            Write-PodeJsonResponse -Value @{ 'Data' = 'some random data' }
        }

        # post route to create some data
        Add-PodeRoute -Method Post -Path $endpoint -ScriptBlock {
            New-Thing -Name $WebEvent.Data['Name']
        }

        # put route to update some data
        Add-PodeRoute -Method Put -Path $endpoint -ScriptBlock {
            Update-Thing -Name $WebEvent.Data['Name']
        }
    }

When I run this in VSCode using the Azure Core Tools, I'm getting an exception:

EXCEPTION: The Add-PodeTimer function is not supported in a serverless context.

ScriptName       : [path]]\ManagedDependencies\2410241931327472642.r\Pode\2.11.0\Private\Server.ps1
Line             : throw $_.Exception
Statement        : throw $_.Exception
PositionMessage  : At [path]]\ManagedDependencies\2410241931327472642.r\Pode\2.11.0\Private\Server.ps1:213 char:9
                   +         throw $_.Exception
                   +         ~~~~~~~~~~~~~~~~~~
PSScriptRoot     : [path]]\ManagedDependencies\2410241931327472642.r\Pode\2.11.0\Private
PSCommandPath    : [path]]\ManagedDependencies\2410241931327472642.r\Pode\2.11.0\Private\Server.ps1
CommandOrigin    : Internal
ScriptStackTrace      : at Start-PodeInternalServer, [path]]\ManagedDependencies\2410241931327472642.r\Pode\2.11.0\Private\Server.ps1: line 213
                    at Start-PodeServer<End>, [path]]\ManagedDependencies\2410241931327472642.r\Pode\2.11.0\Public\Core.ps1: line 198
Executed 'Functions.isops' (Failed, Id=a4c589ca-0a50-440e-abdb-9ad49c7f7e62, Duration=1924ms)
                    at <ScriptBlock>, [path]\isops\run.ps1: line 7

Expected Behavior

I'm expecting the page to load.

Platform

Additional Context

I'm running this in VSCode before uploading it to Azure. I'm not sure if it's checking something on my local system to determine whether it's serverless or not. I would expect that if -ServerlessType is set, then that would be the context it would use.

Badgerati commented 3 weeks ago

Hi @mdg-pnw,

I've managed to reproduce the issue, and committed a fix for it just now.

The error was happening because Pode's caching feature configures a Timer to expire items, when running via Serverless this isn't needed.