Azure / azure-functions-python-worker

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

[BUG] AsgiFunctionApp is missing BindingApi and SettingsApi mixins #1422

Open mortendaehli opened 4 months ago

mortendaehli commented 4 months ago

Investigative information

Repro steps

  1. Create Azure Functions Python V2 docker template
  2. Create AsgiFunctionApp using FastAPI
  3. Add any BindingApi functions such as queue_output
  4. Run the application in Docker (need to install Core tools and run in verbose to get the actual Exception. Without it, it fails silently)

Expected behavior

I should be able to add BindingApi and SettingsApi such as queue output, function name, etc. to my application while running FastAPI.

Actual behavior

No functions are loaded when running in Docker.

If I add the Core Tools and run --verbose:

[2024-02-05T08:29:00.675Z] Worker failed to index functions
[2024-02-05T08:29:00.676Z] Result: Failure
[2024-02-05T08:29:00.677Z] Exception: AttributeError: 'AsgiFunctionApp' object has no attribute 'queue_output'
[2024-02-05T08:29:00.677Z] Stack:   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.11/LINUX/X64/azure_functions_worker/dispatcher.py", line 345, in _handle__functions_metadata_request
[2024-02-05T08:29:00.677Z]     fx_metadata_results = self.index_functions(function_path)
[2024-02-05T08:29:00.677Z]                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2024-02-05T08:29:00.677Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.11/LINUX/X64/azure_functions_worker/dispatcher.py", line 617, in index_functions
[2024-02-05T08:29:00.677Z]     indexed_functions = loader.index_function_app(function_path)
[2024-02-05T08:29:00.677Z]                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2024-02-05T08:29:00.677Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.11/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 44, in call
[2024-02-05T08:29:00.677Z]     return func(*args, **kwargs)
[2024-02-05T08:29:00.677Z]            ^^^^^^^^^^^^^^^^^^^^^
[2024-02-05T08:29:00.677Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.11/LINUX/X64/azure_functions_worker/loader.py", line 214, in index_function_app
[2024-02-05T08:29:00.677Z]     imported_module = importlib.import_module(module_name)
[2024-02-05T08:29:00.677Z]                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2024-02-05T08:29:00.677Z]   File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
[2024-02-05T08:29:00.677Z]     return _bootstrap._gcd_import(name[level:], package, level)
[2024-02-05T08:29:00.677Z]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2024-02-05T08:29:00.677Z]   File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
[2024-02-05T08:29:00.677Z]   File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
[2024-02-05T08:29:00.677Z]   File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
[2024-02-05T08:29:00.677Z]   File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
[2024-02-05T08:29:00.677Z]   File "<frozen importlib._bootstrap_external>", line 940, in exec_module
[2024-02-05T08:29:00.677Z]   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
[2024-02-05T08:29:00.677Z]   File "/home/site/wwwroot/function_app.py", line 35, in <module>
[2024-02-05T08:29:00.677Z]     ingestion.app_factory(app)
[2024-02-05T08:29:00.677Z]   File "/home/site/wwwroot/src/data_hub/ingestion.py", line 22, in app_factory
[2024-02-05T08:29:00.677Z]     @app.queue_output(arg_name="msg", queue_name="model-data-ingestion-queue", connection="AzureWebJobsStorage")  # type: ignore[misc]
[2024-02-05T08:29:00.677Z]      ^^^^^^^^^^^^^^^^
[2024-02-05T08:29:00.677Z] .
[2024-02-05T08:29:00.683Z] 0 functions found (Worker)
[2024-02-05T08:29:00.699Z] 0 functions loaded

Known workarounds

This monkey patch works:

class FastAPIFunctionApp(func.AsgiFunctionApp, func.BindingApi, func.SettingsApi):
    """
    Adding missing BindingApi and SettingsApi to the AsgiFunctionApp
    """
    pass

Contents of the requirements.txt file:

azure-functions
fastapi

Related information

YunchuWang commented 3 months ago

thanks for reporting this issue! a pr is out for fixing this and will be included in next release.

mortendaehli commented 3 months ago

thanks for reporting this issue! a pr is out for fixing this and will be included in next release.

Thanks a lot! This is much appreciated!

DCMattyG commented 2 months ago

Great to see this! Will this finally allow the default name of "http_app_func" to be modified for things like FastAPI apps?

seidnerj commented 2 months ago

Any news on when will this fix be released? thanks in advance!