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

如何检查隔离工作进程模式下的Durable Function是否已运行?

解决方案

下面是添加了运行状态检查逻辑的完整代码,当BuildingsOrchestration编排已在运行时直接返回提示,未运行则启动编排:

[Function(FunctionNames.BuildingsStart)]
public async Task<IActionResult> HttpRun(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    [DurableClient] DurableTaskClient starter)
{
    // 查询当前运行中的 BuildingsOrchestration 实例
    var instanceQuery = new OrchestrationInstanceQuery
    {
        OrchestrationNames = new[] { FunctionNames.BuildingsOrchestration },
        RuntimeStatus = new[] { OrchestrationRuntimeStatus.Running }
    };

    var runningInstances = await starter.ListInstancesAsync(instanceQuery, CancellationToken.None);

    // 如果存在运行中的实例,直接返回
    if (runningInstances.DurableInstances.Any())
    {
        return new OkObjectResult("BuildingsOrchestration 编排已在运行中");
    }

    // 未运行则启动编排
    var instanceId = await starter.StartNewAsync<string>(FunctionNames.BuildingsOrchestration, null);
    return new OkObjectResult($"BuildingsOrchestration 编排已启动,实例ID: {instanceId}");
}

关键逻辑说明

  • 运行状态检查:通过DurableTaskClient.ListInstancesAsync方法,传入包含目标编排名称和Running运行状态的查询条件,筛选出当前正在运行的实例。
  • 直接返回逻辑:如果查询到存在运行中的实例,返回HTTP 200响应并提示已在运行。
  • 启动编排:确认无运行实例时,调用StartNewAsync启动BuildingsOrchestration,并返回启动后的实例ID供后续追踪使用。

注意事项

  • 需要确保FunctionNames.BuildingsOrchestration常量正确指向你的编排函数名称。
  • 返回类型从Task改为Task<IActionResult>,以便返回标准HTTP响应。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 15:32:36