4
votes

Looking at the Kudu Azure WebJobs API docs https://github.com/projectkudu/kudu/wiki/WebJobs-API I see there are several calls I can do to manage WebJobs programmatically.

What is missing is a call to get, for a continuous webjob, the details of a single invocation. With invocation I mean the single execution of the function for a given message.

What I am trying to do is, for a message getting in the poison queue, to get the exception message of the parent invocation. In the poison message I get the id of the parent invocation with the json prop $AzureWebJobsParentId.

I would like to manage the poison queue with a function that emails the details of the error and moves the message in a dead-letter queue.

Any idea if this is possible?

3
Not that the API you link to above is the general WebJobs API, and it knows nothing about the SDK. But your question is SDK specific. You may want to tweak the title to make the clearer. - David Ebbo
Ok @DavidEbbo you mean that the Api I refer to is the Kudu Api and it's not the place to look at? I thought it was, since there's a call to get details of a triggered job. Any suggestion on where to look instead? Would be strange if there was no way. It looks a useful scenario to me. Anyway, thanks for the comment. - Giorgio Bozio
The Kudu API only knows about continuous and triggered WebJobs. If the continuous WebJob happens to use the SDK, Kudu has no knowledge of that. So your question is more: when using the SDK, is there an API to get the details. - David Ebbo
@GiorgioBozio how about accepting the answer mentioning Azure WebJobs SDK Extensions? This is still the way to go. - Sascha Gottfried

3 Answers

5
votes

The Azure WebJobs SDK Core extensions contain a binding for ExecutionContext which allows you to access invocation specific system information in your function. An example showing how to access the function Invocation ID:

public static void ProcessOrder(
    [QueueTrigger("orders")] Order order,
    TextWriter log,
    ExecutionContext context)
{
    log.WriteLine("InvocationId: {0}", context.InvocationId);
}

The invocation ID is used in the Dashboard logs, so having access to this programatically allows you to correlate an invocation to those logs.

Create a function specific error handler that will only handle errors for one function. This is done by naming convention based on an "ErrorHandler" suffix.

public static void ProcessOrderErrorHandler(
    [ErrorTrigger()] TraceFilter filter,
    TextWriter log)
{

    var lastMessage = filter.Message;
    var lastMessages = filter.GetDetailedMessage(5);
}

For email notifications

public static void ErrorMonitor(
    [ErrorTrigger("0:30:00", 10, Throttle = "1:00:00")] TraceFilter filter,
    [SendGrid] SendGridMessage message)
{
    message.Subject = "WebJobs Error Alert";
    message.Text = filter.GetDetailedMessage(5);
}
1
votes

There's not an official way to do this yet, but there's an issue to track exposing stable APIs (C# and REST) for reading the individual function instances: See https://github.com/Azure/azure-webjobs-sdk/issues/880 for status

0
votes

Given a WebJobs SDK invocation ID, you can access the details of that execution via the WebJobs Dashboard. You can access the Dashboard via the WebJobs blade in the portal (the link will be in the "LOGS" column).

Or in the browser you can form the URL yourself, for example (substituting your app name and invocation ID):

https://<yourapp>.scm.azurewebsites.net/azurejobs/#/functions/invocations/<invocation-id>

That is how you would access these details manually.