Durable Functions: Return Result from Orchestrator

Viewed 2073

I have a use case which fits well with the durable functions sequence example: push a json payload through three functions, each of which modifies the json graph and forwards it to the next function.

In the sequence example the result of the sequence is retrieved by issuing a query to the orchestrator.

In my use case I want to directly return the result of the three functions, essentially as the response of the third function.

Is there a way to do this? Is it even wise?

1 Answers

This is certainly doable. You can start with an HTTP trigger to start the orchestration and use the GetStatusAsync API inside your function to poll and wait for it to complete. Once completed, you can return the result from your HTTP trigger.

Something like this, perhaps:

public static async Task<JObject> Run(JObject input, DurableOrchestrationClient client)
{
    string instanceId = await client.StartAsync("MyOrchestration", input);
    for (int i = 0; i < 60; i++)
    {
        var status = await client.GetStatusAsync(instanceId);
        if (status?.RuntimeStatus == "Completed")
        {
            return (JObject)status.Output;
        }

        // handle other status conditions, like failure

        await Task.Delay(TimeSpan.FromSeconds(1));
    }

    // handle timeouts
}

As you can see from the code, the issue you'll have is dealing with error conditions. For example, what does your function do if the orchestration fails? Also, what if it takes a long time to finish? Those are things you can certainly figure out, but you'll want to code defensively to handle these cases.

Related