【问题标题】:Azure WebJob logging is crashing host, what can we do?Azure WebJob 日志记录正在崩溃主机,我们该怎么办?
【发布时间】:2016-04-05 15:25:38
【问题描述】:

我们正在使用 Azure 运行多个 WebJobs。我们的 WebJob 函数之一具有以下签名:

public static Task ProcessFileUploadedMessageAsync(
    [QueueTrigger("uploads")] FileUploadedMessage message,
    TextWriter logger,
    CancellationToken cancellationToken)

此函数正在监视队列中是否存在指示文件已上传的消息,然后触发文件数据的导入。请注意使用 TextWriter 作为第二个参数:这是由 WebJobs API 基础结构提供的。

我们的导入过程有点慢(在某些情况下,单个文件导入可能需要几个小时),因此我们会定期将消息写入日志(通过TextWriter)以跟踪我们的进度。不幸的是,我们较大的文件导致 WebJob 托管进程由于日志记录异常而终止。这是来自托管进程日志的示例堆栈跟踪:

[04/02/2016 03:44:59 > 660083: ERR ] 
[04/02/2016 03:45:00 > 660083: ERR ] Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
[04/02/2016 03:45:00 > 660083: ERR ] Parameter name: chunkLength
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Text.StringBuilder.ToString()
[04/02/2016 03:45:00 > 660083: ERR ]    at System.IO.StringWriter.ToString()
[04/02/2016 03:45:00 > 660083: ERR ]    at Microsoft.Azure.WebJobs.Host.Loggers.UpdateOutputLogCommand.<UpdateOutputBlob>d__10.MoveNext()
[04/02/2016 03:45:00 > 660083: ERR ] --- End of stack trace from previous location where exception was thrown ---
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
[04/02/2016 03:45:00 > 660083: ERR ]    at Microsoft.Azure.WebJobs.Host.Loggers.UpdateOutputLogCommand.<TryExecuteAsync>d__3.MoveNext()
[04/02/2016 03:45:00 > 660083: ERR ] --- End of stack trace from previous location where exception was thrown ---
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
[04/02/2016 03:45:00 > 660083: ERR ]    at Microsoft.Azure.WebJobs.Host.Timers.RecurrentTaskSeriesCommand.<ExecuteAsync>d__0.MoveNext()
[04/02/2016 03:45:00 > 660083: ERR ] --- End of stack trace from previous location where exception was thrown ---
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
[04/02/2016 03:45:00 > 660083: ERR ]    at Microsoft.Azure.WebJobs.Host.Timers.TaskSeriesTimer.<RunAsync>d__d.MoveNext()
[04/02/2016 03:45:00 > 660083: ERR ] --- End of stack trace from previous location where exception was thrown ---
[04/02/2016 03:45:00 > 660083: ERR ]    at Microsoft.Azure.WebJobs.Host.Timers.BackgroundExceptionDispatcher.<>c__DisplayClass1.<Throw>b__0()
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
[04/02/2016 03:45:00 > 660083: ERR ]    at System.Threading.ThreadHelper.ThreadStart()
[04/02/2016 03:45:00 > 660083: SYS ERR ] Job failed due to exit code -532462766
[04/02/2016 03:45:01 > 660083: SYS INFO] Process went down, waiting for 0 seconds
[04/02/2016 03:45:01 > 660083: SYS INFO] Status changed to PendingRestart

主要问题是这个异常不是由我们的代码引发的,而是由 WebJobs API 中的某些东西引发的:

at Microsoft.Azure.WebJobs.Host.Loggers.UpdateOutputLogCommand.<UpdateOutputBlob>d__10.MoveNext()

我们尝试在对TextWriter 的调用周围放置try...catch 块,但这些都没有效果。似乎日志消息在某处缓冲,并通过单独的线程定期刷新到 Azure blob 存储。如果是这种情况,那么我们就无法捕获异常。

有没有其他人遇到过同样的问题,或者任何人都可以想到可能的解决方案或解决方法?


为了完整起见,以下是我们使用TextWriter 进行日志记录的方式:

await logger.WriteLineAsync("some text to log");

没有比这更复杂的了。


更新:好像这已被报告为issue #675 on the WebJobs SDK GitHub。

【问题讨论】:

  • 史蒂文,它是否在没有异步方法的情况下工作:logger.WriteLine("some text to log"); ?
  • @Thomas 我没试过

标签: azure azure-webjobs


【解决方案1】:

你应该在方法中添加async,就像这样:

public async static Task ProcessFileUploadedMessageAsync(...)

了解详情可以看this article

【讨论】:

  • 谢谢,但是这个特定的方法没有 await 调用,只是返回一个由不同的异步方法生成的任务。
  • 但你说你使用了'await logger.WriteLineAsync("some text to log");'用于记录。这段代码不是在 ProcessFileUploadedMessageAsync 中吗?
  • 不,抱歉,这是对问题的简化。 logger 实际上并没有直接在 WebJob 方法中使用,它只是传递给返回 Task 的 async 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多