|  | 
| 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.
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.StartNewAsynccall to start a new Duration Function. We have theJob-Idand theexpiration timein 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 JobMonitoringDurable 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  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 JobMonitoringDurable 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
Post a Comment