【问题标题】:Azure Durable Functions - diagnose failuresAzure Durable Functions - 诊断故障
【发布时间】:2019-05-21 19:29:37
【问题描述】:

我正在测试持久函数如何扇入/扇出以及如何扩展我的代码。对于这个例子,我模拟了许多短期运行、CPU 密集型操作。

似乎并非所有活动都已完成,我不确定为什么或在哪里查找失败日志。

请看下面的代码:

public static class ParallelLoadDurable
    {
        [FunctionName("ParallelLoadDurable")]
        public static async Task<string> RunOrchestrator(
            [OrchestrationTrigger] DurableOrchestrationContext context, ILogger log)
        {
            DateTime StartTimer = DateTime.Now;

            int counter = 0;
            var parallelTasks = new List<Task<string>>();
            var retryOptions = new RetryOptions(
                 firstRetryInterval: TimeSpan.FromSeconds(5),
                 maxNumberOfAttempts: 5);
            for (int i = 0; i < 1000; i++)
            {
                counter += 1;
                DurablePassModel DPM = new DurablePassModel()
                {
                    LoopNum = counter,
                    StartedOn = StartTimer
                };
                Task<string> task = context.CallActivityWithRetryAsync<string>("ParallelLoadDurable_Hello", retryOptions, DPM);
                parallelTasks.Add(task);
            }
            await Task.WhenAll(parallelTasks);

            DateTime CompleteTime = DateTime.Now;
            TimeSpan TS = CompleteTime.Subtract(StartTimer);

            string ret = $"PROCESS COMPLETED: {counter} times for: {TS.TotalMilliseconds} ms.";
            log.LogInformation(ret);
            return ret;
        }

        [FunctionName("ParallelLoadDurable_Hello")]
        public static string SayHello([ActivityTrigger] DurablePassModel val, ILogger log)
        {
            log.LogInformation($"Starting child function num {val.LoopNum.ToString()}.");
            DateTime StartTimer = DateTime.Now;

            var endTime = DateTime.Now.AddSeconds(10);

            while (true)
            {
                if (DateTime.Now >= endTime)
                    break;
            }

            DateTime CompleteTime = DateTime.Now;
            TimeSpan TS = CompleteTime.Subtract(val.StartedOn);
            TimeSpan TSThis = CompleteTime.Subtract(StartTimer);

            string ret = $"Ran this for: {TSThis.TotalSeconds}s - LoopNum: {val.LoopNum} - total time: {TS.TotalSeconds}s.";
            log.LogInformation(ret);

            return ret;
        }

        [FunctionName("ParallelLoadDurable_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")]HttpRequestMessage req,
            [OrchestrationClient]DurableOrchestrationClient starter,
            ILogger log)
        {
            // Function input comes from the request content.
            string instanceId = await starter.StartNewAsync("ParallelLoadDurable", null);

            log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }

我几乎在每个案例中都完成了大约 96% 的预期活动。这是来自历史表中的结果,其中 EventType = TaskCompleted

同样在“实例”表中,RuntimeStatus 只是保持在“正在运行”

我在哪里可以找到失败列表?

感谢您的帮助

尼克

【问题讨论】:

    标签: azure-functions azure-durable-functions


    【解决方案1】:

    trycatch 块包围您的代码,然后使用Ilogger 记录异常。

    可以这样实现

    try
    {
       //Do something
    }
    catch(Exception ex){
       log.LogError($"Error in function: {ex.Message}");
    }
    

    然后您可以查看日志中的错误消息,如果有的话,还可以查看Application insights

    【讨论】:

      【解决方案2】:

      我建议您为 Azure Functions 配置 Application Insights:https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring 尽管存储数据会涉及一些成本,但它在调查问题时确实有很大帮助。

      此外(或者当您不需要 Application Insights 时)您可以通过 C#HTTP 查询业务流程的状态。当您在本地机器上运行和调试时,这也非常有效!

      使用 HTTP API,您可以执行以下查询来确定失败的实例:

      @functionAppUrl = https://{{appName}}.azurewebsites.net
      @code = YOUR_HOST_KEY
      @taskHub = YOUR_HUB_NAME (see host.json)
      @createdFrom = 2019-02-01T20:00:00Z
      
      GET {{functionAppUrl}}/runtime/webhooks/durabletask/instances
              ?taskHub={{taskHub}}
              &code={{code}}
              &createdTimeFrom={{createdFrom}}
              &runtimeStatus=Failed
      

      我注意到您在编排代码中使用了DateTime.Now。建议使用DurableOrchestrationContext 中的CurrentUtcDateTime 属性,因为编排函数中的行为应该是确定性的。请参阅this section 了解编排器代码约束。

      【讨论】:

        猜你喜欢
        • 2017-06-23
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 2019-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多