如何处理待重启的 azure web 作业
How to handle azure web job pending restart
我目前正在使用队列触发器使用 azure web 作业,所以每当队列中有消息时,我都会调用一个函数,该函数将执行 15 分钟的耗时任务(将数据推送到文档数据库),并且能够执行好吧,现在我的 azure 应用程序计划出现套接字异常,并且 web 启动正在自行重新启动,并且在进入毒队列之前再次重试此功能多次。
我想知道的几件事
- 如何处理异常并进行一些日志记录?
- 如何在 web 作业再次开始之前删除队列中的现有消息,以便我们不进行并发操作?
- 在网络作业关闭之前是否会触发任何事件,以便我可以做任何事情
感谢任何帮助或指点
How to handle the exception and do some logging ?
要处理 WebJob 中的异常,您可以在 WebJob 中添加 try-catch 并将所有作业代码放在 try 块中。
另一种方法是使用ErrorTrigger,我们需要安装
使用前的 Azure WebJobs SDK 扩展。
How to delete the existing message in queue before the web job start again so that we are not doing concurrent operations ?
您可以将 MaxDequeueCount 属性 更改为 1。如果发生异常,邮件将立即移至毒物队列。
JobHostConfiguration config = new JobHostConfiguration();
config.Queues.MaxDequeueCount = 1;
JobHost host = new JobHost(config);
Is there any event that will be fired before web job shuts down so that I can do anything
我还没有找到任何会在网络作业关闭之前触发的事件。有两件事会导致网络作业关闭。
发生异常,我们可以使用 try-catch 块或 ErrorTrigger 记录异常。
Web 作业因 Web App Plan 重新启动而中止。我们可以向该函数添加一个 CancellationToken 类型的参数,IsCancellationRequested 属性 将在 WebJob 由于 Web App Plan 重新启动而中止之前设置为 true。
public static void ProcessQueueMessage2([QueueTrigger("myqueue")] string message, CancellationToken cancellationToken)
{
Task.Run(new Action(LongRunningTask), cancellationToken);
while (true)
{
Thread.Sleep(5000);
if (cancellationToken.IsCancellationRequested)
{
Console.WriteLine("The function has been Cancelled");
}
}
}
public static void LongRunningTask()
{
}
在 WebJob 中止之前,输出将是这样的。
[06/12/2017 01:55:38 > 9d3635: SYS INFO] Status changed to Stopping
[06/12/2017 01:55:42 > 9d3635: INFO] The function has been Cancelled
[06/12/2017 01:55:43 > 9d3635: SYS INFO] WebJob process was aborted
[06/12/2017 01:55:43 > 9d3635: SYS INFO] Status changed to Stopped
我目前正在使用队列触发器使用 azure web 作业,所以每当队列中有消息时,我都会调用一个函数,该函数将执行 15 分钟的耗时任务(将数据推送到文档数据库),并且能够执行好吧,现在我的 azure 应用程序计划出现套接字异常,并且 web 启动正在自行重新启动,并且在进入毒队列之前再次重试此功能多次。
我想知道的几件事
- 如何处理异常并进行一些日志记录?
- 如何在 web 作业再次开始之前删除队列中的现有消息,以便我们不进行并发操作?
- 在网络作业关闭之前是否会触发任何事件,以便我可以做任何事情
感谢任何帮助或指点
How to handle the exception and do some logging ?
要处理 WebJob 中的异常,您可以在 WebJob 中添加 try-catch 并将所有作业代码放在 try 块中。
另一种方法是使用ErrorTrigger,我们需要安装 使用前的 Azure WebJobs SDK 扩展。
How to delete the existing message in queue before the web job start again so that we are not doing concurrent operations ?
您可以将 MaxDequeueCount 属性 更改为 1。如果发生异常,邮件将立即移至毒物队列。
JobHostConfiguration config = new JobHostConfiguration();
config.Queues.MaxDequeueCount = 1;
JobHost host = new JobHost(config);
Is there any event that will be fired before web job shuts down so that I can do anything
我还没有找到任何会在网络作业关闭之前触发的事件。有两件事会导致网络作业关闭。
发生异常,我们可以使用 try-catch 块或 ErrorTrigger 记录异常。
Web 作业因 Web App Plan 重新启动而中止。我们可以向该函数添加一个 CancellationToken 类型的参数,IsCancellationRequested 属性 将在 WebJob 由于 Web App Plan 重新启动而中止之前设置为 true。
public static void ProcessQueueMessage2([QueueTrigger("myqueue")] string message, CancellationToken cancellationToken)
{
Task.Run(new Action(LongRunningTask), cancellationToken);
while (true)
{
Thread.Sleep(5000);
if (cancellationToken.IsCancellationRequested)
{
Console.WriteLine("The function has been Cancelled");
}
}
}
public static void LongRunningTask()
{
}
在 WebJob 中止之前,输出将是这样的。
[06/12/2017 01:55:38 > 9d3635: SYS INFO] Status changed to Stopping
[06/12/2017 01:55:42 > 9d3635: INFO] The function has been Cancelled
[06/12/2017 01:55:43 > 9d3635: SYS INFO] WebJob process was aborted
[06/12/2017 01:55:43 > 9d3635: SYS INFO] Status changed to Stopped