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

Durable Functions V4 JavaScript参数传递与处理问题排查

JavaScript Durable Functions V4 Instance ID传递错误修复方案

核心问题分析

1. Activity函数参数获取方式错误

你在TokenManager里用context.getClient().instanceID拿Orchestrator传的参数完全错了。context.getClient()是用来创建Durable客户端、管理编排实例的,根本不是获取Activity输入的方法。

2. Orchestrator异常处理逻辑顺序错误

reportOrchestrator的catch块里,你先写了return error.message;,这会直接终止函数,后面的日志记录和错误抛出代码根本不会执行,导致问题无法被追踪。


修复后的完整代码

编排函数(reportOrchestrator)

df.app.orchestration('reportOrchestrator', function* (context) {
   try {
       const instanceId = context.df.instanceId;
       context.log(`Orchestration instance ID: ${instanceId}`);

       // 传递instanceID到Activity函数
       const token = yield context.df.callActivity('TokenManager', { instanceID: instanceId }); 

       context.log(`Token retrieved: ${token}`);
       return token;

       // const result = yield context.df.callActivity('PipelineQuery', { token });
   } catch (error) {
       // 先打日志再抛出错误,不要提前return
       context.log(`Error in orchestration: ${error.message}`);
       throw error;
   }
});

活动函数(TokenManager)

df.app.activity('TokenManager', {
   handler: async (context, input) => { // 第二个参数input就是编排传过来的参数
        const log = context.log;

        // 直接从input中获取instanceID
        const lsFile = input;

        if (!lsFile || !lsFile.instanceID) {
            throw new Error("InstanceID缺失或未定义");
        }

        const instanceID = lsFile.instanceID;
        log(`Orchestrator Instance ID is ${instanceID}`);

        let functionID = "POST Reports Orchestrator";

        try {
            // 发送POST请求获取token
            const tokenResponse = await axios.post(logicAppURL, {
                requestType: "GET_TOKEN",
                functionID: functionID,
            });

            if (tokenResponse.data && tokenResponse.data.token) {
                log("Token获取成功");
                return tokenResponse.data.token;
            } else {
                log("Token无法获取,函数将生成自有token");
                await mandrillGeneralError(log, "Azure中POST Reports Orchestrator Token Manager函数应用获取token出错。");
                return "";
            }
        } catch (error) {
            log(`获取token出错: ${error.message}`);
            await mandrillGeneralError(log, `获取token出错: ${error.message}`);
            throw error;
        }
   }
});

额外提醒

  • 确认logicAppURL和mandrillGeneralError已在代码中正确定义,避免引入新错误。
  • Durable Functions V4的API和旧版本有差异,注意区分参数获取、客户端调用的写法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 09:42:46