Click here to Skip to main content
15,881,588 members
Articles / Hosted Services / Azure
Tip/Trick

Durable Functions - Set Custom Status Message

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Jan 2019CPOL 7.5K  
Set a custom status message with your durable functions

Introduction

One common pattern with Azure durable functions is for an orchestrator to call one or more activity functions in order. If these activity functions don't themselves return a value or have any external effect, it can be difficult to follow where the orchestrator function has got to.

To get around this, I have a simple class which is returned for any activity function that does not have its own return type:

C#
/// <summary>
/// A class to give feedback from a durable function
/// </summary>
public class ActivityResponse
{
    /// <summary>
    /// The name of the function that was called
    /// </summary>
    /// <remarks>
    /// This is just for logging - it would not be advisable to use this for any
    /// branch logic as it is up to the developer to set it correctly
    /// </remarks>
    public string FunctionName { get; set; }

    /// <summary>
    /// A message returned from the activity that can be logged
    /// </summary>
    public string Message { get; set; }

    /// <summary>
    /// The activity had a fatal error and the calling orchestrator should be terminated
    /// </summary>
    public bool FatalError { get; set; }
}

Then when the durable function is called, it populates this class with the function name and any error or message that should be logged.

C#
[FunctionName("GetLeagueSummaryLogParametersActivity")]
public static async Task<ActivityResponse> GetLeagueSummaryLogParametersActivity(
        [ActivityTrigger] DurableActivityContext context,
        ILogger log = null)
    {
        ActivityResponse ret = new ActivityResponse()
        { FunctionName = "GetLeagueSummaryLogParametersActivity" };

        try
        {
             // - - - 8 < - - - - - - - - - - - - - - - - - - - - - -
             // Work of the durable function activity goes here
             // - - - 8 < - - - - - - - - - - - - - - - - - - - - - -
        }
        catch (Exception ex)
        {
            if (null != log)
            {
                // Unable to get the request details from the orchestration
                log.LogError ($"GetLeagueSummaryLogParametersActivity : error {ex.Message} ");
            }
            ret.Message = ex.Message;
            ret.FatalError = true;
        }
        return ret;
    }

Then the calling function can make use of this returned class and use it with the SetContext method to add it to the durable function's status message:

C#
// Save the parameters to the event stream
ActivityResponse resp = await context.CallActivityAsync<ActivityResponse>
("GetLeagueSummaryLogParametersActivity", queryRequest);

    #region Logging
    if (null != log)
    {
        if (null != resp)
        {
            log.LogInformation($"{resp.FunctionName} complete: {resp.Message } ");
        }
    }
    #endregion

    if (null != resp)
        {
            context.SetCustomStatus(resp);
        }

This context message will then be available through the GetStatusAsync method (and will also be logged in Application Insights if you have that hooked up to your functions app.)

History

  • 2019-01-02 Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
-- There are no messages in this forum --