Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

[BUG] warmupTrigger doesn't work as documented #1135

Closed thomasfrederikhoeck closed 1 year ago

thomasfrederikhoeck commented 2 years ago

Investigative information

Please provide the following:

Repro steps

Provide the steps required to reproduce the problem:
  1. Create a very basic function with a httpTriggerand a warmupTriggerfollowing the official guide https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-warmup?tabs=in-process&pivots=programming-language-python:
## httptrigger/function.json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}
## httptrigger/__init__.py
import logging
import azure.functions as func

async def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info("Succes!")
    return func.HttpResponse("Succes", status_code=200)
## warmup/function.json
{
    "bindings": [
        {
            "type": "warmupTrigger",
            "direction": "in",
            "name": "warmupContext"
        }
    ]
}
## warmup/__init__.py
import logging
import azure.functions as func

def main(warmupContext : func.Context) -> None:
    logging.warning('Function App instance is warm šŸŒžšŸŒžšŸŒž')
  1. Run the function.
  2. Get the following error:
    For detailed output, run func with --verbose flag.
    [2022-10-26T09:21:14.653Z] Worker process started and initialized.
    [2022-10-26T09:21:17.301Z] Host lock lease acquired by instance ID '0000000000000000000000002C1B88F1'.
    [2022-10-26T09:21:18.829Z] Worker failed to function id 65568825-e196-4213-9fc3-b55a123c4b89.
    [2022-10-26T09:21:18.831Z] Result: Failure
    Exception: FunctionLoadError: cannot load the warmup function: type of warmupContext binding in function.json "warmupTrigger" does not match its Python annotation "Context"
    Stack:   File "C:\Users\tfh\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 320, in _handle__function_load_request   
    self._functions.add_function(
    File "C:\Users\tfh\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\functions.py", line 217, in add_function
    raise FunctionLoadError(

If I try and remove the type it is never run which can be seen as this Exception is never hit

import azure.functions as func

def main(warmupContext) -> None:
    raise Exception('Function App instance is warm šŸŒžšŸŒžšŸŒž')

Expected behavior

Provide a description of the expected behavior.

It should locally when I run it as according to the guide

Support for the warmup trigger is provided by default in all development environments. You don't have to manually install the package or register the extension.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-warmup?tabs=in-process&pivots=programming-language-python

Actual behavior

Provide a description of the actual behavior observed.

Raises the error:

For detailed output, run func with --verbose flag.
[2022-10-26T09:21:14.653Z] Worker process started and initialized.
[2022-10-26T09:21:17.301Z] Host lock lease acquired by instance ID '0000000000000000000000002C1B88F1'.
[2022-10-26T09:21:18.829Z] Worker failed to function id 65568825-e196-4213-9fc3-b55a123c4b89.
[2022-10-26T09:21:18.831Z] Result: Failure
Exception: FunctionLoadError: cannot load the warmup function: type of warmupContext binding in function.json "warmupTrigger" does not match its Python annotation "Context"
Stack:   File "C:\Users\tfh\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 320, in _handle__function_load_request   
    self._functions.add_function(
  File "C:\Users\tfh\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.8/WINDOWS/X64\azure_functions_worker\functions.py", line 217, in add_function
    raise FunctionLoadError(

Known workarounds

Provide a description of any known workarounds.

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-functions==1.8.0 

Related information

Provide any related information
thomasfrederikhoeck commented 2 years ago

Hi @gavin-aguiar . I can see @vrdmr assigned you to this. Have you been able to replicate?

thomasfrederikhoeck commented 1 year ago

@gavin-aguiar @vrdmr is this actively worked on? :-)

ramya894 commented 1 year ago

@thomasfrederikhoeck We are investigating on this will update you the findings soon.

thomasfrederikhoeck commented 1 year ago

@ramya894 sounds good!

ramya894 commented 1 year ago

@thomasfrederikhoeck We are able to reproduce the problem and working on the solution to fix it. Thanks.

gavin-aguiar commented 1 year ago

Hi @thomasfrederikhoeck , apologies for the late response. We are looking into this error. In the mean time for mitigation, remove the type in the definition as you mentioned. Locally, you can invoke the warmup trigger by calling http://localhost:7071/admin/warmup. In Azure, the warmup trigger will be called only during scale out operations.

thomasfrederikhoeck commented 1 year ago

@gavin-aguiar Thank you. I have tried the work around and that works. However it still requires me to call the endpoint. Will the fix you have merged automatically call the warmup trigger when starting a local debug?

YunchuWang commented 1 year ago

@thomasfrederikhoeck Hi Thomas, in local the warmup trigger wont be automatically called. its only invoked by runtime in scaling scenarios, i.e in dedicated or elastic premium plans.

thomasfrederikhoeck commented 1 year ago

Gotcha. Then I think it would be meanigfull to show the endpoint in the startup messeage. In the following it is very hard to see that you need to call http://localhost:7071/admin/warmup to run it.

image