0
votes

I am writing some code that implements event sourcing using Cosmos as my storage. My initial documents are written successfully to the collection. I have then set up an Azure function that triggers on changes to that collection's feed and copies the item to another collection.

My problem is that while this all works fine if I am debugging the functions app locally (the changes come through and are processed without issue) the function is not triggered once published as a functions app. The function exists but the total execution count is always 0. It's like the function isn't running on the timer and checking the feed. Other functions in my functions app work as expected.

My function code is

 [FunctionName("EventSourceWrite")]
        public static void Run([CosmosDBTrigger(
            databaseName: "Puffin",
            collectionName: "EventSource",
            ConnectionStringSetting = "EventSourceConnection",
            CreateLeaseCollectionIfNotExists = true,
            LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
        {
            if (input != null && input.Count > 0)
            {
                log.LogInformation("Documents modified " + input.Count);
                log.LogInformation("First document Id " + input[0].Id);

                var container = Cosmos.GetItemsContainer();

                foreach (var doc in input)
                {
                    var item = JsonConvert.DeserializeObject<Item>(doc.ToString());

                    // reset the Id
                    item.Id = item.ItemId;

                    if(!string.IsNullOrEmpty(item.CollectionId))
                    {
                        container.UpsertItemAsync(item, new Microsoft.Azure.Cosmos.PartitionKey(item.CollectionId));
                    }   
                }
            }
        }
1
Did you add 'EventSourceConnection' in app setting of deployed function app? Please share a screenshot of your deployed Function app settings page.krishg
Have you gone through the troubleshooting guide? docs.microsoft.com/en-us/azure/cosmos-db/…. Do you have any logs from when you deploy to see if the configuration is correct?Matias Quaranta
@krishg, that was exactly the issue. I've not tried this before so it seems easy to miss as none of the examples I've seen cover this step, but now that you pointed this out it seems obvious! Seeing as you got in first if you want the accepted answer then add as an answer and I'll accept it.Andy Davies
It's fine, you can accept the one which is already below :). Glad your problem got solved.krishg

1 Answers

1
votes

Please check that do you have EventSourceConnection in your application settings of your function app. If no, please new one and try again.

enter image description here