2
votes

I'm working with Azure Functions for the first time. I'm trying to write a simple function which responds to documents changed or added to a CosmosDb collection. The function I've written looks like this:

[FunctionName("ChangeLog")]
public static void Run([CosmosDBTrigger(
    databaseName: "Recaptcha",
    collectionName: "Rules",
    ConnectionStringSetting = "CosmosDBConnection",
    LeaseCollectionName = null)]IReadOnlyList<RuleConfigCollection> documents)
{
    if (documents != null && documents.Count > 0)
    {
        ApplicationEventLogger.Write(
            Diagnostics.ApplicationEvents.RecaptchaRulesChanged,
            new Dictionary<string, object>()
            {
                { "SomeEnrichment", documents[0].Rules.ToList().Count.ToString() }
            });
    }
}

By my understanding a lease collection is necessary when multiple functions are pointed at the same CosmosDb, but in my case this isn't relevant. That's why I've set the lease collection to null.

I've published this to Azure from Visual Studio and can see the function is created with the following function.json:

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.12",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "connectionStringSetting": "CosmosDBConnection",
      "collectionName": "Rules",
      "databaseName": "Recaptcha",
      "leaseDatabaseName": "Recaptcha",
      "createLeaseCollectionIfNotExists": false,
      "name": "documents"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/My.Namespace.Functions.App.dll",
  "entryPoint": "My.Namespace.Functions.App.ChangeLogFunction.Run"
}

I've also added an application setting named CosmosDBConnection with the value AccountEndpoint=https://my-cosmosdb.documents.azure.com:443;AccountKey=myAccountKey;.

I run the function then add a document to the collection, but the logs just keep saying No new trace in the past n min(s) and the application events I expect to see are not being written.

Have I missed something in this setup?

1
OK, I'm actually getting an exception thrown from one of my own dependencies now, so my function is being called. Not sure if that was since I added the lease collection as per @mikhail's comment below or not. But looks like this is now an app issue my side.Tom Troughton
The other thing I changed was replacing RuleConfigCollection (my strongly-typed document model) with the generic Microsoft.Azure.Documents.Document. I'll have to look at strong typing later.Tom Troughton

1 Answers

1
votes

I'm not sure that's the root cause of you issue, but your understanding of leaseCollection is wrong.

leaseCollection is used to coordinate multiple instances (workers) of your Function App to distribute partitions between workers.

It is required even for a single Function listening to Cosmos DB change feed.