Azure Function HTTP一次执行多次执行

时间:2020-10-15 06:33:57

标签: c# azure azure-function-app

我有一个相对简单的Azure Function在消耗计划下运行,超时设置为4 4分钟。

当我通过门户网站执行时,即使我只执行过一次,它也会多次执行且运行彼此重叠。

例如:

  • 执行1

  • 00:36:52.917

  • 00:41:25.750

  • 执行2

  • 00:40:41.793

  • 00:41:25.700

  • 执行3

  • 00:44:27.430

  • 00:48:30.857

关于执行时间的重复是没有逻辑的。

知道发生了什么吗?

using System;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;
using Microsoft.Data.SqlClient;


namespace FMPApiCall

{
    public static class FMPTestAPI
    {
        #region Private Data Members
        private static readonly HttpClient Client = new HttpClient();
        #endregion

        [FunctionName("FMPTestAPI")]

        public static async Task<IActionResult> Run(
           [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
            HttpRequest req, ILogger log)

        {
            log.LogInformation("FMPTestAPI triggered - BEGIN");

            List<string> symbolsList = FMPCallAPI.CommonFunctions.GetSymbolsFromDB();   //gets as list of IDs from the database

            int counter = 0;

            using SqlConnection dbconnection = new SqlConnection(Environment.GetEnvironmentVariable("SqlServerConnectionString"));
            {
                dbconnection.Open();

                foreach (string symbol in symbolsList)
                {
                    counter++;

                    var apiRequest =
                            $"https://myapi.com/api/v3/dataset1/{symbol}?period=quarter&limit=4&apikey=123";
                    var response = await Client.GetAsync(apiRequest);
                    var symbolsData = await response.Content.ReadAsStringAsync();

                    var sqlStr = $"INSERT INTO [dbo].[_azure] (symbol,runid, response,lastupdated) VALUES ( '{symbol}' , {counter} ,'{response}',  getutcdate() )";
                    using (SqlCommand cmd = new SqlCommand(sqlStr, dbconnection))
                    {
                        var rows = cmd.ExecuteNonQuery();
                    }


                }
                dbconnection.Close();
            }

            log.LogInformation("FMPTestAPI triggered - END");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult(new { Hello = name })
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
    }
}

eee

0 个答案:

没有答案