带有 Docker 映像的“Azure Function”不适用于“Azure Cosmos DB 触发器”

问题描述 投票:0回答:1

我们有一个带有 Docker 映像的 Azure-Function,可在 Http-Trigger 上工作...根据本文... https://learn.microsoft.com/en-us/azure/azure-functions/functions-deploy-container?tabs=acr%2Cbash%2Cazure-cli&pivots=programming-language-python

但是,当我们添加 CosmosDB 触发器时......它会失败

@app.function_name(name="CosmosDBTrigger")
@app.cosmos_db_trigger(
    arg_name="documents",
    connection="COSMOSDB_CONNECTION_STRING",
    database_name="sz-ue2XXXXX",
    container_name="****",
    lease_container_name="leases",
    create_lease_container_if_not_exists="true",
)
def CosmosDBTrigger(documents: func.DocumentList) -> str:
    if documents:
        logging.info("Document id: %s", documents[0]["id"])


2024-06-18T21:15:33.567Z ERROR - Container ****eto-s-func-01_1_153a76b5 for site sz-ue2-seto-s-func-01 did not start within expected time limit. Elapsed time = 230.4885576 sec
    2024-06-18T21:15:33.608Z ERROR - Container ****s-func-01_1_153a76b5 didn't respond to HTTP pings on port: 80, failing site start. See container logs for debugging.

问题1:“Azure function with Docker image”是否可以触发“CosmosDB”?

问题2:如果问题1的答案是肯定的,那么有Dockerfile的样本吗?

谢谢。

azure-functions azure-cosmosdb
1个回答
0
投票

要在容器功能应用程序中触发

cosmos_db trigger
,您需要在 Dockerfile 中传递连接字符串值。

Cosmosdb 触发器的先决条件

  • leases
    容器应在数据库中可用,PartitionKey 为
    /partitionKey

funcdocker_DOCUMENTDB
是我的连接名称。检查 docker 文件中的 ENV。

Dockerfile
:

FROM mcr.microsoft.com/azure-functions/python:4-python3.11

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
    funcdocker_DOCUMENTDB=AccountEndpoint=https://funcdocker.documents.azure.com:443/;AccountKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;

COPY requirements.txt /
RUN pip install -r /requirements.txt

EXPOSE 443

COPY . /home/site/wwwroot

function_app.py
:

import azure.functions as func
import logging

app = func.FunctionApp()


@app.cosmos_db_trigger(arg_name="azcosmosdb", container_name="Test",
                        database_name="ToDoList", connection="funcdocker_DOCUMENTDB") 
def cosmosdb_trigger(azcosmosdb: func.DocumentList):
    logging.info('Python CosmosDB triggered.')

OUTPUT

© www.soinside.com 2019 - 2024. All rights reserved.