【问题标题】:Check status of triggered Azure Function检查触发的 Azure 函数的状态
【发布时间】:2021-06-14 20:54:17
【问题描述】:

我正在尝试为我正在处理的应用程序创建 e2e 测试。它由将一些文件存储在 Blob Storage 中的 Web 应用程序和每天处理这些文件一次的 Azure 函数组成。

我想在 nodejs 中编写测试,在网站上执行某些操作(使用 WDIO),然后通过 HTTP 请求触发函数(如https://docs.microsoft.com/en-us/azure/azure-functions/functions-manually-run-non-http 中所述),然后,如果函数成功完成,检查它是否在合适的位置。

虽然这个问题是,在发送请求触发函数后,它没有返回任何句柄/jobId,我无法检查它的状态。有什么办法可以做到吗?

如果我有 runId,我可能会查询 Application Insights 以查看作业是否完成/失败。

至于现在我这样做:

我在 Application Insights 中查询在测试开始后创建的日志(这不是理想的解决方案,尽管在我的情况下不应有更多的实例同时运行)。如果目前没有日志,我每分钟重试最多 5 次。在此之后,如果工作成功,我会解析它们以查找消息。

我使用的查询:

union traces
| union exceptions
| union requests
| where cloud_RoleName =~ '${APP_NAME}' and operation_Name =~ '${OPERATION_NAME}' | order by timestamp desc
| where message contains 'Processing' or message contains 'Executed' or message contains 'Archived' or innermostMessage contains 'Exception'
| where timestamp > make_datetime("${DATE.toISOString()}")
| project message, innermostMessage

尽管这有一个很大的缺点,因为日志也可能需要长达 5 分钟(通常大约 3 分钟)才能出现在 Application Insights 中:/。

【问题讨论】:

    标签: node.js azure azure-functions


    【解决方案1】:

    使用以下代码获取 azure 函数的实例 id:

    using System;
    using System.IO;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Logging;
    using Newtonsoft.Json;
    
    namespace FunctionApp2
    {
        public static class Function1
        {
            [FunctionName("Function1")]
            public static async Task<IActionResult> Run(
                [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
                ExecutionContext ec,
                ILogger log)
            {
                string iid = ec.InvocationId.ToString();
                return new OkObjectResult(iid);
            }
        }
    }
    

    并使用它来获取 azure 函数实例的状态:

    https://docs.microsoft.com/en-us/rest/api/appservice/web-apps/get-instance-info

    此外,您可以使用应用程序洞察力来获取状态(正如您在问题中提到的那样),这更容易。

    【讨论】:

    • 遗憾的是我有 TimerTrigger 功能所以我不能返回任何东西:|
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2021-08-18
    • 2018-10-01
    • 2020-08-19
    相关资源
    最近更新 更多