Azure / azure-functions-python-worker

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

[BUG] cosmos trigger v2 decorative model without functions.json #1386

Closed vijaystroup closed 5 months ago

vijaystroup commented 6 months ago

Investigative information

Repro steps

import logging
import os
import azure.functions as func

bp = func.Blueprint()

@bp.cosmos_db_trigger(
    arg_name='documents',
    connection='COSMOS_CONNECTION_STRING',
    database_name=os.environ['DB_NAME'],
    container_name=os.environ['DB_CONTAINER']
)
def cosmos_trigger(documents: func.DocumentList) -> str:
    if documents:
        logging.info('Document id: %s', documents[0]['id'])

Expected behavior

Upon insertion of a new document into the container:

Document id: xxxxxxxxxxxxxxxx

Actual behavior

Error on startup:

The 'cosmos_trigger' function is in error: Unable to configure binding 'documents' of type 'cosmosDBTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.

Known workarounds

Not tested but from looking at other issues, perhaps adding the binding connection information in a functions.json file for the specific function would yield the correct results, but other documentation suggests functions.json is for the v1 programming model, not v2.

Contents of the requirements.txt file:

azure-functions
requests
azure-storage-blob
pyjwt[crypto]
azure-servicebus
azure-cosmos
bhagyshricompany commented 6 months ago

Thanks for reporting will check and update.share the func invocationid,timestamp ,region etc.

vijaystroup commented 6 months ago

The error was not caused by an invocation of the function. Upon starting the dev server: func start, all my functions are listed except this cosmos_trigger. Then shortly after the list without any invocations to any of the functions it logs an error as indicated in the Actual behavior section.

bhagyshricompany commented 5 months ago

can you share which doc you use..

vijaystroup commented 5 months ago

can you share which doc you use..

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-trigger?tabs=python-v2%2Cisolated-process%2Cextensionv4%2Cnodejs-v4&pivots=programming-language-python

bhagyshricompany commented 5 months ago

Hi @vijaystroup please use the blueprint for your function app. I checked and its working fine.

image

please see the below code.

image image

.

vijaystroup commented 5 months ago

Still getting the same error:

import logging
import os
import azure.functions as func

bp = func.Blueprint()

@bp.cosmos_db_trigger(
    arg_name='documents',
    connection='COSMOS_CONNECTION_STRING',
    database_name=os.environ['COSMOS_DATABASE'],
    container_name=os.environ['COSMOS_CONTAINER'],
    create_lease_container_if_not_exists=True,
    lease_container_name='lease',
)
def cosmos_trigger(documents: func.DocumentList) -> str:
    if documents:
        logging.info('Document id: %s', documents[0]['id'])
[2024-01-12T20:43:11.211Z] The 'cosmos_trigger' function is in error: Unable to configure binding 'documents' of type 'cosmosDBTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.

The documentation for lase containers located here do not show an example of "the lease container automatically with the Azure Functions trigger". I'm not 100% sure how the lease container works besides keeping the db in sync with multiple instances of functions, so perhaps I am not setting something up correctly. It should be noted, the code above is the only thing that I changed, I did not manually create a lease container as from the docs it said it would automatically create one.

bhagyshricompany commented 5 months ago

please follow the doc as in local setting file also have to be maintain "LeaseContainerName":"leases", "CreateLeaseContainerIfNotExists" :true this two props. Try to add the missing details in your local.settings.json file like cosmos DB connection string and the database name and the collection name which you are referring.then it works.

vijaystroup commented 5 months ago

Still same error on start up: The 'cosmos_trigger' function is in error: Unable to configure binding 'documents' of type 'cosmosDBTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.

I do not have to have manually created a lease container correct? image Here I make sure the db, container, and lease names are actually filled in and I do have COSMOS_CONNECTION_STRING set in my local.settings.json file.

The docs indicate that only the connection argument needs the key name whereas the other arguments need the actual value rather than the key name.

hallvictoria commented 5 months ago

Hi @vijaystroup

No, you don't have to manually create a lease container. create_lease_container_if_not_exists=True will handle that for you. You can verify that the container exists by checking your CosmosDB database. Also, if one of your resources doesn't exist a 404 error will be thrown at start up.

In your host.json file, what version for extensionBundle are you using? I was able to replicate the issue using [3.*, 4.0.0), but the function worked properly with [4.*, 5.0.0). If you aren't using [4.*, 5.0.0), can you try changing to that and see if the issue still occurs?

image
vijaystroup commented 5 months ago

Hi @vijaystroup

No, you don't have to manually create a lease container. create_lease_container_if_not_exists=True will handle that for you. You can verify that the container exists by checking your CosmosDB database. Also, if one of your resources doesn't exist a 404 error will be thrown at start up.

In your host.json file, what version for extensionBundle are you using? I was able to replicate the issue using [3.*, 4.0.0), but the function worked properly with [4.*, 5.0.0). If you aren't using [4.*, 5.0.0), can you try changing to that and see if the issue still occurs? image

That was it! Thank you.