连续运行的webjob在20分钟后中止

时间:2017-05-27 01:22:20

标签: c# azure-webjobs azure-webjobssdk webjob

我在Premium层中的Web应用程序(App Service)下运行Web作业。它调用REST API端点,这需要很长时间才能返回响应。 Web作业在20分钟后中止,但根据文档,Web作业可以在后台运行长时间运行的任务。这似乎没有按预期工作。

是否为Web作业功能指定了最大超时?

代码:

public async Task<HttpResponseMessage> GetAsync(Uri requestUrl)
{
    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetAadAuthentication().AccessToken);
    var httpResponse = await _httpClient.GetAsync(requestUrl);
    if (!httpResponse.IsSuccessStatusCode)
    {
        ProcessFailedRequest(httpResponse);
    }

    return httpResponse;
}

异常日志:

[05/26/2017 21:28:01 > 6e349c: ERR ] Unhandled Exception: System.Threading.Tasks.TaskCanceledException: A task was canceled.
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
[05/26/2017 21:28:01 > 6e349c: ERR ]    at Util.testWebClient.<GetAsync>d__12.MoveNext() in d:\test\Util\testWebClient.cs:line 99
[05/26/2017 21:28:01 > 6e349c: ERR ] --- End of stack trace from previous location where exception was thrown ---
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
[05/26/2017 21:28:01 > 6e349c: ERR ]    at Util.testClient.<GetForecastResultAsync>d__7.MoveNext() in d:\test\Util\testClient.cs:line 135
[05/26/2017 21:28:01 > 6e349c: ERR ] --- End of stack trace from previous location where exception was thrown ---
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
[05/26/2017 21:28:01 > 6e349c: ERR ]    at AutoAggregation.Functions.<>c__DisplayClass18_0.<ProcessResultDownloadMessage>b__0(Object workItem) in d:\test\AutoAggregation\Functions.cs:line 198
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Threading.ThreadPoolWorkQueue.Dispatch()
[05/26/2017 21:28:01 > 6e349c: ERR ]    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

2 个答案:

答案 0 :(得分:1)

您可能正在使用免费的托管计划层吗?此(https://tomssl.com/2016/12/20/how-to-get-azure-webjobs-to-run-indefinitely-for-free/)博客准确地描述了相同的问题和解决方法。对于专业用途,我建议增加托管计划

TLDR:免费套餐下的应用池最多可以运行20分钟。

答案 1 :(得分:1)

我将HttpClient的超时时间增加为:

_httpClient = new HttpClient();
_httpClient.Timeout=Timeout.InfiniteTimeSpan;

以前它被设置为

_httpClient.Timeout=new TimeSpan(0, 20, 0);