Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Managing Durable Function Orchestrations - Azure and C#

Compare Page Revisions



« Older Revision - Back to Page History - Current Revision


Page Revision: Tue, Jun 14, 2022, 9:44 AM


Overview

When working with Azure Durable Functions, it can be useful to terminate past orchestrations that are still running but no longer needed. This article provides a Azure function with an HTTP trigger that provides this functionality.

IT IS IMPORTANT NOT TO INCLUDE THIS CODE IN A PRODUCTION SITUATION, as security on it is insufficient. However, it does provide details around how this is accomplished.

Code

public static class OrchestrationManagerFunctions
{
    [FunctionName(nameof(OrchestrationManager))]
    public static async Task<IActionResult> OrchestrationManager(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        /*--- Inits ---*/
        log.LogInformation("C# HTTP trigger function processed a request.");

        var token = new CancellationToken();

        /*--- Get/Display Orchestrations Currently Running ---*/
        var instances = await starter.ListInstancesAsync(new OrchestrationStatusQueryCondition { }, token);

        Debug.Print("=========================================================");
        Debug.Print("  Orchestrations Currently Running");
        Debug.Print("---------------------------------------------------------");

        foreach (var item in instances.DurableOrchestrationState)
        {
            if (item.RuntimeStatus != OrchestrationRuntimeStatus.Running)
            {
                continue;
            }

            var mgr = starter.CreateHttpManagementPayload(item.InstanceId);

            var url = mgr.TerminatePostUri;

            Debug.Print($"Instance('{item.InstanceId}') - {item.Name} - Since {item.CreatedTime}");
        }
        Debug.Print("=========================================================");

        /*--- Terminate All Running Instances ---*/
        foreach (var item in instances.DurableOrchestrationState)
        {
            if (item.RuntimeStatus != OrchestrationRuntimeStatus.Running)
            {
                continue;
            }

            var mgr = starter.CreateHttpManagementPayload(item.InstanceId);

            var uri = new Uri(mgr.TerminatePostUri);

            var httpRequest = (HttpWebRequest)WebRequest.Create(uri);

            httpRequest.Method = "POST";

            var httpResponse = (HttpWebResponse)(await httpRequest.GetResponseAsync());

            var httpResult = await new StreamReader(httpResponse.GetResponseStream()).ReadToEndAsync();

            Debug.Print($"Instance('{item.InstanceId}') - {item.Name} - TERMINATED");
        }

        Debug.Print("=========================================================");

        return new OkObjectResult(string.Empty);
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2023, Patrick Jasinski.