【问题标题】:WebJob occasionally throwing System.Threading.Tasks.TaskCanceledExceptionWebJob 偶尔会抛出 System.Threading.Tasks.TaskCanceledException
【发布时间】:2019-05-01 04:34:46
【问题描述】:

我有一个连续的 WebJob,它执行一个队列触发的函数,它偶尔会抛出 System.Threading.Tasks.TaskCanceledException。

WebJobs SDK 版本:2.1.0

堆栈跟踪:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: 
Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: Functions.ProcessCreationQueueMessage ---> 
System.AggregateException : One or more errors occurred. ---> A task was canceled. at 
System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at
 System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task`1.get_Result() at
 Normandy.Portal.Reporting.Creation.WebJobs.Functions.ProcessCreationQueueMessage(String message,String& outputQueueMessage,TextWriter log) at
 D:\a\1\s\Portal\src\Normandy.Portal.Reporting.Creation.WebJobs\Functions.cs : 105 at lambda_method(Closure ,Functions ,Object[] ) at Microsoft.Azure.WebJobs.Host.Executors.VoidMethodInvoker`2.InvokeAsync(TReflected instance,Object[] arguments) at
async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync[TReflected,TReturnValue](Object instance,Object[] arguments) at 
async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker,ParameterHelper parameterHelper,CancellationTokenSource timeoutTokenSource,CancellationTokenSource functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan timerInterval,IFunctionInstance instance) at 
async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,ParameterHelper parameterHelper,TraceWriter traceWriter,CancellationTokenSource functionCancellationTokenSource) 
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??) 
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??) ---> (Inner Exception #0) System.Threading.Tasks.TaskCanceledException : A task was canceled.<--- End of inner exception 
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??) at 
async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance functionInstance,CancellationToken cancellationToken) 

网络作业:

        public void ProcessCreationQueueMessage([QueueTrigger("reportcreation")] string message, [Queue("reportretrieval")] out string outputQueueMessage, TextWriter log)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };

            log.WriteLine(message);

        ...

            try
            {
                var jobId = _reportingService.RequestReport(req.ReportName, searchString).Result;

                var output = new RetrieveReportDataRequest()
                {
                    CreationRequest = new ReportCreationRequest()
                    {
                        UserId = req.UserId,
                        UID = req.UID,
                        CreateDate = req.CreateDate,
                        IsMultiSchoolUser = req.IsMultiSchoolUser,
                        CentreCode = req.CentreCode,
                        ReportName = req.ReportName,
                        InputSearchParameters = req.InputSearchParameters,
                    },
                    SplunkSearchParameters = searchString,
                    SplunkJobId = jobId,
                    Created = DateTime.UtcNow,
                };

                outputQueueMessage = JsonConvert.SerializeObject(output, settings);
            }
            catch (Exception ex)
            {
                log.WriteLine(ex.Message);
                throw;
            }
        }

显然没有时间做任务。对于某些消息,这会失败一次。我错过了什么?

【问题讨论】:

  • RequestReport 在做什么?您可能正在某个地方调用在后台执行异步工作的方法,并且由于超时或类似原因而抛出TaskCanceledException。例如,HttpClient 是意外TaskCanceledExceptions 的常见来源,它会在超时时抛出。
  • RequestReport 正在异步调用外部 API。当任何 API 方法抛出超时时,WebJob 是否会抛出 TaskCancelledException,或者外部 api 是否特别必须抛出 TaskCancelledException?异步调用是否有任何默认超时?
  • 如果 _reportingService-service 托管在 AppService 上并且它的执行需要很长时间(超过 230 秒),那么它可能是失败的原因 - 请参阅 Why does my request time out after 230 seconds?。可以看看Azure WebJob dashboard查看webjob调用失败的执行时间吗?
  • 只是好奇,这个webjob好像是被触发器激活的,为什么Continuous不是Triggered
  • @vladimir 这个 webjob 包含多个触发器,虽然我没有参与这个设计,但我的想法是让它连续并将所有相关触发器保留在一个 WebJob 中。 _reportingService 是一项 Azure Service Fabric 服务,而且网络作业肯定不会花费 230 秒。从我在 WebJobs Dashboard 中可以看到,所有失败的运行的执行时间都在 40 秒到 2 分钟之间。成功运行的时间不到 2 秒。我几乎确信这是由于超时而发生的,但是您知道设置此超时的任何方法吗?

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


【解决方案1】:

可能与主机设置有关 - 查看 Azure Portal -> AppService 配置,确保 Always On 设置为 On

【讨论】:

  • 始终开启。
猜你喜欢
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 2020-01-25
  • 2013-11-17
  • 2016-03-24
  • 2017-06-23
相关资源
最近更新 更多