Azure-Functions icon indicating copy to clipboard operation
Azure-Functions copied to clipboard

VS2017 created Queue triggered functions report completion on run vice completion

Open jchilde opened this issue 9 years ago • 17 comments

This occurs in both the may 31 released cli beta run locally and in the published function.

Upon queue trigger, the first log entry is that the function started. Then it logs completion, then it runs the function. This prevents later uncaught errors from being reported.

jchilde avatar Jun 01 '17 00:06 jchilde

are you running it through the run button or the cli's func run or are you running it by dropping a message into the queue?

ahmelsayed avatar Jun 01 '17 00:06 ahmelsayed

By dropping a message into the queue for both cli and published.

jchilde avatar Jun 01 '17 00:06 jchilde

this shouldn't happen. How are you seeing that exactly? through the logs? Do you mind sharing your code?

ahmelsayed avatar Jun 01 '17 00:06 ahmelsayed

The same code dropped into an azure function doesn't do this. I am observing this from the logs online and the cmd window from the cli. My code is nothing special. I was just trying to port functions I have been using to vs 2017.

jchilde avatar Jun 01 '17 01:06 jchilde

@jchilde Do you have a small repro that shows the issue?

lindydonna avatar Jun 01 '17 01:06 lindydonna

Here is one function that had the behavior; however, I saw it with every function.

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using OnPointFunctions.Models;
using Newtonsoft.Json;
using Microsoft.SharePoint.Client;

namespace OnPointFunctions.Lists
{
    public static class CreateLedger
    {
        [FunctionName("Lists-Create-Ledger")]        
        public static async void Run([QueueTrigger("lists-create-ledger", Connection = "AzureWebJobsStorage")]string myQueueItem, TraceWriter log)
        {
            log.Info($"C# Queue trigger function processed: {myQueueItem}");//

            string inputLedgerXML = Helpers.BlobHelper.ReadBlob("/BuiltInLists/Ledger.xml");
            string inputScriptInjectorWithHudWebPart = Helpers.BlobHelper.ReadBlob("/WebParts/ScriptInjectorWebPartWithHud.txt");

            BuiltInListCreationData data = JsonConvert.DeserializeObject<BuiltInListCreationData>(myQueueItem);
            Guid MatterWebGUID = data.MatterWebGUID;
            string NewListTitle = data.NewListTitle;
            string OPHostUrl = data.OPHostUrl;

            using (ClientContext ctx = await Helpers.SharePointHelper.OPClientContext(data.TenantId.ToString(), data.OPHostUrl))
            {
                Web web = ctx.Site.OpenWebById(MatterWebGUID);
                ListCollection lists = web.Lists;
                List NewList = null;
                ListCreationInformation lci = new ListCreationInformation
                {
                    TemplateType = (int)ListTemplateType.GenericList,
                    TemplateFeatureId = Guid.Parse("00bfea71-de22-43b2-a848-c05709900100"),
                    Title = NewListTitle,
                    Url = "Lists/" + NewListTitle,
                    QuickLaunchOption = QuickLaunchOptions.On,
                    CustomSchemaXml = inputLedgerXML,
                    Description = "Records costs, hourly fees, and money held in escrow for a matter"
                };

                NewList = lists.Add(lci);
                ctx.ExecuteQuery();
                NewList.ListExperienceOptions = ListExperience.ClassicExperience;
                NewList.EnableAttachments = false;
                NewList.Update();
                ctx.Load(NewList);
                ctx.ExecuteQuery();
                Helpers.ContentTypes.UpdateContentTypeName(web, NewList.Id, "Ledger");
                Helpers.WebParts.AddToView(web, NewList.Id, inputScriptInjectorWithHudWebPart);
                Helpers.QueueHelper.AddToQueue("update-quicklaunch", data);
            }
        }
    }
}

jchilde avatar Jun 01 '17 01:06 jchilde

@jchilde You're using "async void". This causes a lot of problems, and we're going to add an analyzer that disallows it. See https://github.com/Azure/azure-webjobs-sdk-script/issues/1556

lindydonna avatar Jun 01 '17 01:06 lindydonna

I am no longer on my surface so I will be a bit more verbose.

I am porting production functions over to VS 2017. I am literally copying the scripts and pasting them. So I know the scripts work fine.

The only changes I am making are to the triggers/binding parameters as seen in my code above.

Now, When I place a message into the "lists-create-ledger" queue, I expect that I will get a log message saying that the function was triggered, then I would expect to see everywhere that I have logged information using "log.Info". Finally I expect to see the message that the queue completed or resulted in an error.

What I actually see is the start message, followed by the function complete message, followed by my log messages.

What clued me in to the problem was that in the script above, it wasn't creating a sharepoint list properly; however, it was showing successful completion. Long story short, it failed in the ctx.ExecuteQuery() line. However, it was a silent fail. Then I realized that all function error messages are provided when the function completes. However, my functions are saying they are complete when they actually aren't.

This morning, I was running the most recent cli. Then I got a message that a new version was available today. I installed the new version. The error occurred before and after upgrading to the latest cli.

jchilde avatar Jun 01 '17 01:06 jchilde

VS 2017 CLI states that and an asynchronous function must return either a task or void. See image... async

jchilde avatar Jun 01 '17 01:06 jchilde

@jchilde Yes, that is a general C# concept. On Azure Functions, you can't use async void for your main Function method.

lindydonna avatar Jun 01 '17 01:06 lindydonna

@jchilde As a matter of fact, your code doesn't use await, so with the current implementation, feel free to just remove the async. You can keep the void return, but I'd recommend looking at employing async patterns as some of the code you have above would benefit from it.

fabiocav avatar Jun 01 '17 01:06 fabiocav

It has an await when creating the SharePoint client context.

jchilde avatar Jun 01 '17 01:06 jchilde

But I do appreciate you looking at it. I am not a programmer and can use all help.

jchilde avatar Jun 01 '17 01:06 jchilde

@jchilde In that case, you should change it to "async Task" as the return type.

lindydonna avatar Jun 01 '17 01:06 lindydonna

then you can just do

public static async Task Run([..............................

you'll also need to add a using System.Threading.Tasks; at the top to use Tasks.

The reason Task is needed to come back from your function is to tell the functions runtime what the state of your function is. returning void tells the function runtime that your function is done. then it starts executing which is not in order and can be killed at any point. Returning a Task contains the information the runtime needs to know whether your function has finished or still in progress.

async Task is like void for sync functions.

ahmelsayed avatar Jun 01 '17 01:06 ahmelsayed

You guys rock. Thanks. I really like creating functions this way. The only other issue I have seen is the known problem with input bindings. Otherwise works well for me.

jchilde avatar Jun 01 '17 01:06 jchilde

Completely missed your await! Glad this is working for you now.

fabiocav avatar Jun 01 '17 06:06 fabiocav