运行后台任务时防止IIS空闲超时 [英] Prevent IIS from Idle Timeout when Running Background Tasks

查看:78
本文介绍了运行后台任务时防止IIS空闲超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的ASP.Net Core Web API中,我们使用了 Hosted Services 来执行一些后台任务.有时,这些后台任务可能需要很长时间才能完成(大约一个小时).除非有长期运行的任务,否则一切都可以无缝运行.

In our ASP.Net Core Web API, we have used Hosted Services to perform some background tasks. Sometimes it is possible that these background tasks to take a long time to complete (about an hour). Everything works seamlessly except when there are long-running tasks.

IIS应用程序池的空闲超时"默认值为20分钟.因此,如果20分钟内没有请求,IIS将重新启动应用程序池.但是有时,后台任务可能会运行并且IIS重新启动,而不管它是否导致任务突然终止而没有将其标记为完成.IIS启动后,托管服务开始处理将再次拾取任务.但是在完成之前,IIS将重新启动,并且这种操作可能会一直持续下去.当有电子邮件发送任务时,可能多次发送同一封电子邮件,这可能会导致很多问题.

IIS Application Pool's default value for Idle Timeout is 20 mins. So when there is no request for 20 mins, IIS restarts the application pool. But sometimes it is possible that a background task running and IIS restarts irrespective of it causing tasks to terminate abruptly without marking it complete. As soon as IIS starts, tasks are picked up again by the hosted service start processing. But before completing, IIS restarts and this can go on forever. This can cause lots of issues when there are email sending tasks where same email can be potentially sent multiple times.

由于托管服务是ASP.NET Core的头等公民,因此如果IIS在执行空闲超时时也考虑后台执行,那将是理想的选择.

Since Hosted services are a first-class citizen of ASP.NET Core, It would be ideal if IIS considers background executions as well when performing idle timeouts.

现在,我们通过将空闲超时"设置为0来解决该问题,从而使其永不超时.这是一种骇人听闻的解决方案.另外,在节点动态添加和删除的负载平衡环境中,这不是可行的选择.

Right now we have overcome this issue by setting Idle Timeout to 0 so that it never timeout. That's a kind of hacky solution. Also, it's not a viable option in load-balanced environments where nodes are adding and removing dynamically.

是否有更好的方法防止IIS在进行后台执行时重新启动"?

Is there a better way to "Prevent IIS from restarting while a background execution is in progress"?

谢谢

推荐答案

考虑使用专为此类任务设计的 Hangfire .目前,您可以将IIS配置为始终运行该站点.

Consider using Hangfire designed for such tasks. For the time being, You can configure your IIS to always run the site.

  1. 右键单击站点名称>管理网站>高级>确保预加载设置为true.

  1. Right-click site name > Manage WebSite > Advance > make sure preload is set to true.

右键单击应用程序池>高级设置>空闲超时(分钟)为0,启动模式始终运行.

Right-click app pool > advance settings > Idle Time-out (minutes) to 0 and Start Mode to always running.

这篇关于运行后台任务时防止IIS空闲超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆