My First Encounter with Azure Durable Function

pexels image from Arina Krasnikova
pexels image from Arina Krasnikova

Recently, I need to have an orchestrator in Azure Function App to monitor the status of a long-running job. Azure Duration Function comes in handy. In this blog, I am going to simplify the use case to focus solely on how Duration Function works. 

User Case: A long-running job is executed and we need to monitor its status and log this status in a database.

Durable Function

Referencing the diagram above.

This is where the Duration Function is executed.
(A) fx is an Azure Function where the job is started. Following that, it makes a IDurableOrchestrationClient.StartNewAsync call to start a new Duration Function. We have the Job-Id and the expiration time in the context of this function. Essentially, Duration Function is an internal HTTP endpoint in Azure Function App. It looks like this
[FunctionName("JobMonitoring")]
public async Task RunMonitor(
    [OrchestrationTrigger] IDurableOrchestrationContext context)
{
...
}

(B)Here the JobMonitoring Durable Function is called. The following is the pseudocode.
[FunctionName("JobMonitoring")]
public async Task RunMonitor(
    [OrchestrationTrigger] IDurableOrchestrationContext context)
{
    var data = context.GetInput<ContextData>();
    var expireTime = data.ExpireTime;

    if (context.CurrentUtcDateTime < expireTime)
    {
        var status = await context.CallActivityAsync<string>(
            "GetJobStatus", data.JobId);

        if (status == "Processing")
        {
            var nextCheckpoint = context.CurrentUtcDateTime.AddMinutes(1);
            await context.CreateTimer(nextCheckpoint, CancellationToken.None);
            context.ContinueAsNew(data);
        }
        else
        {
            // store status in database
        }
    }
}

It checks if the expiration time (found in the content) is reached, otherwise, it gets the job status with the JobId (found in the context). It creates a new timer for the next checkpoint if the job is still in a processing state.

All the information (context, function name, etc) around calling this function again in 1 minute is stored in Azure Blob Storage. The red circles show information stored and retrieved from Blob Storage.


(B')After one minute, the Durable Function is executed again.

(BC)Here the JobMonitoring Durable Function is called and the job status is completed. Job Status is stored in the database and the Durable Function ended.


 Essentially, the context of the Durable Function call is stored in Azure Blob Storage. During the time to invoke the function, a new runtime context object is created from the persisted blob entry.


Comments

Popular posts from this blog

OpenAI: Functions Feature in 2023-07-01-preview API version

Storing embedding in Azure Database for PostgreSQL

Happy New Year, 2024 from DALL-E