You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算Orchestration函数总运行时间及解决重放秒表重置问题

Hey there! Let's tackle your two questions about Durable Orchestration functions one by one:

1. 如何计算Orchestration函数的总运行时长?

You don't need to build custom timing logic from scratch—Durable Functions provides built-in, reliable ways to get the total runtime of your orchestration:

  • For completed orchestrations: Access the start and end times directly from the orchestration's instance status. For example:
    // C# example
    var startTime = this.InstanceRuntimeStatus.StartTime;
    var endTime = this.InstanceRuntimeStatus.EndTime;
    var totalDuration = endTime - startTime;
    
    # Python example
    start_time = context.df.created_time
    end_time = context.df.completed_time
    total_duration = end_time - start_time
    
  • For in-progress orchestrations: Calculate elapsed time using the orchestration's creation time and the current deterministic UTC time:
    // C# example
    var elapsed = this.CurrentUtcDateTime - this.InstanceRuntimeStatus.StartTime;
    
    # Python example
    elapsed = context.df.current_utc_datetime - context.df.created_time
    

All these values use UTC time, which is mandatory for orchestrations to avoid timezone-related inconsistencies.

2. 解决秒表因Orchestration重放重置的问题

The core issue here is that regular stopwatches live in memory, and orchestrations replay their code from scratch every time there's an event (like an activity completing). This resets any in-memory state—including your stopwatch.

The fix is to use deterministic time APIs provided by Durable Functions to track activity start/end times, since these values are persisted in the orchestration's history and won't reset during replays. Here's how to implement it:

Step-by-step approach:

  • Before calling each activity, record the current orchestration time (use the built-in API, not non-deterministic methods like DateTime.UtcNow)
  • After the activity completes, record the orchestration time again
  • Calculate the duration as the difference between these two times, and store this value in the orchestration's state (either in a persisted variable, or via SetCustomStatus for external visibility)

Example in C#:

[FunctionName("MyOrchestration")]
public async Task RunOrchestrator(
    [OrchestrationTrigger] IDurableOrchestrationContext context)
{
    var totalElapsed = TimeSpan.Zero;

    // Track first activity
    var activity1Start = context.CurrentUtcDateTime;
    await context.CallActivityAsync("Activity1", null);
    var activity1End = context.CurrentUtcDateTime;
    totalElapsed += activity1End - activity1Start;

    // Track second activity
    var activity2Start = context.CurrentUtcDateTime;
    await context.CallActivityAsync("Activity2", null);
    var activity2End = context.CurrentUtcDateTime;
    totalElapsed += activity2End - activity2Start;

    // Save total duration to custom status (for external monitoring)
    await context.SetCustomStatusAsync(new { TotalActivityDuration = totalElapsed.TotalSeconds });
}

Example in Python:

import azure.durable_functions as df
from datetime import timedelta

def orchestrator_function(context: df.DurableOrchestrationContext):
    total_elapsed = timedelta(0)

    # Track first activity
    activity1_start = context.current_utc_datetime
    yield context.call_activity("Activity1", None)
    activity1_end = context.current_utc_datetime
    total_elapsed += activity1_end - activity1_start

    # Track second activity
    activity2_start = context.current_utc_datetime
    yield context.call_activity("Activity2", None)
    activity2_end = context.current_utc_datetime
    total_elapsed += activity2_end - activity2_start

    # Set custom status for external access
    context.set_custom_status({"total_activity_duration": total_elapsed.total_seconds()})
    return total_elapsed.total_seconds()

main = df.Orchestrator.create(orchestrator_function)

This works because context.CurrentUtcDateTime (C#) and context.current_utc_datetime (Python) return the same value during replays for the same point in the orchestration's history—so your timing calculations stay accurate, even after multiple replays.

内容的提问来源于stack exchange,提问作者suresh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:31:08