问题描述
我的 azure 函数中有以下代码,手动超时 10 分钟.
I have the following code in my azure function with 10 minutes manual timeout.
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
try
{
TimeSpan ts = TimeSpan.FromMinutes(1);
for(int i=0;i<10;i++)
{
await Task.Delay(ts);
log.Info(String.Format("After 1 Min Delay {0}",i));
}
log.Info(String.Format("After 10 Min Delay "));
return req.CreateResponse(HttpStatusCode.OK);
}
catch (Exception e)
{
log.Info(String.Format("exception: {0}", e));
return req.CreateResponse(HttpStatusCode.BadRequest);
}
}
当我在 Azure 中运行上述函数时,我看到该函数在 3 分钟后创建了一个新实例.(查看以下日志)
when I run the above function in Azure, I see the function creates a new instance after 3 minutes. (check the below log)
2018-05-15T11:12:42 Welcome, you are now connected to log-streaming service.
2018-05-15T11:12:55.826 [Info] Function started (Id=f25e0bbd-7103-4823-b8f1-ef28888f7007)
2018-05-15T11:12:55.826 [Info] C# HTTP trigger function processed a request.
2018-05-15T11:13:55.844 [Info] After 1 Min Delay 0
2018-05-15T11:14:55.857 [Info] After 1 Min Delay 1
2018-05-15T11:15:55.862 [Info] After 1 Min Delay 2
2018-05-15T11:16:47.385 [Info] Function started (Id=7371ed94-9b62-40cc-bec0-00b8d5e0a250)
2018-05-15T11:16:47.385 [Info] C# HTTP trigger function processed a request.
2018-05-15T11:16:55.879 [Info] After 1 Min Delay 3
2018-05-15T11:17:47.395 [Info] After 1 Min Delay 0
2018-05-15T11:17:55.883 [Info] After 1 Min Delay 4
2018-05-15T11:18:47.400 [Info] After 1 Min Delay 1
2018-05-15T11:18:55.899 [Info] After 1 Min Delay 5
2018-05-15T11:19:47.411 [Info] After 1 Min Delay 2
2018-05-15T11:19:55.914 [Info] After 1 Min Delay 6
2018-05-15T11:20:47.413 [Info] After 1 Min Delay 3
2018-05-15T11:20:55.920 [Info] After 1 Min Delay 7
2018-05-15T11:21:47.416 [Info] After 1 Min Delay 4
2018-05-15T11:21:55.930 [Info] After 1 Min Delay 8
2018-05-15T11:22:47.436 [Info] After 1 Min Delay 5
2018-05-15T11:22:55.936 [Info] After 1 Min Delay 9
2018-05-15T11:22:55.936 [Info] After 10 Min Delay
2018-05-15T11:22:55.936 [Info] Function completed (Success, Id=f25e0bbd-7103-4823-b8f1-ef28888f7007, Duration=600105ms)
2018-05-15T11:23:47.447 [Info] After 1 Min Delay 6
2018-05-15T11:24:47.452 [Info] After 1 Min Delay 7
2018-05-15T11:25:47.467 [Info] After 1 Min Delay 8
2018-05-15T11:26:47.478 [Info] After 1 Min Delay 9
2018-05-15T11:26:47.478 [Info] After 10 Min Delay
2018-05-15T11:26:47.478 [Info] Function completed (Success, Id=7371ed94-9b62-40cc-bec0-00b8d5e0a250, Duration=600086ms)
在上面的日志中,您可以看到 azure 函数调用了两次,并且在完成执行后给出了错误(500:内部服务器错误).
In above log, you can see that azure function is calling twice and also it gives the error (500: internal server error) and after it completes its execution.
推荐答案
关于http请求超时重试.
It is about http request timeout and retry.
当我直接在门户中运行该函数时,它会像@Sumit 和@Joey 看到的那样在将近4m 后再次调用.我把Delay减到10s,函数只触发一次,返回200.
When I run the function directly in portal, it will be called again after nearly 4m like @Sumit and @Joey have seen. I decrease the Delay to 10s, and the function is only triggered once and returns 200.
尝试使用邮递员发布请求,这次该功能只触发一次,响应信息如下.
Try to use postman to post request, this time the function is triggered only once with response message below.
正如我们在代码中看到的,直到 10m 进程完成后才返回响应.它超出了超时设置,并且似乎按照设计,门户发布的请求将在超时后重试.
As we can see in the code, no response sent back until 10m process finished. It is beyond the timeout setting and it seems by design that request posted by portal will be retried after timeout.
更新
Azure 函数是一种 Azure Web App,参见 Azure Web应用程序超时 230 秒设置.
Azure function is one kind of Azure Web App, see Azure Web App time out 230s setting.
对于未发回任何数据的请求,有 230 秒(即略少于 4 分钟)超时.之后,客户端得到你看到的 500,即使实际上请求被允许继续服务器端
这篇关于Azure Function 在应用服务计划中调用自身两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!