我正在开发一个 asp.net mvc 5 Web 应用程序,部署在 IIS-8 中,我的应用程序中有一个方法可以执行一项长时间运行的任务,主要扫描我们的网络以查找服务器和虚拟机,并使用扫描结果更新我们的数据库. 方法执行可能会持续 30-40 分钟才能在生产环境中完成。我正在使用一个名为 Hangfire 的计划工具,它每天会调用此方法 2 次。
这是 startup.cs 文件中的作业定义,它将在8:01 am
&处调用方法8:01 pm
:-
public void Configuration(IAppBuilder app)
{
var options = new SqlServerStorageOptions
{
PrepareSchemaIfNecessary = false
};
GlobalConfiguration.Configuration.UseSqlServerStorage("scanservice",options);
RecurringJob.AddOrUpdate(() => ss.Scan(), "01 8,20 ***");
}
这是计划工具每天调用两次的方法:-
public void Scan()
{
Service ss = new Service();
ss.NetworkScan().Wait();
}
最后,进行真正扫描的方法是(我只提供该方法将做什么的高级描述):-
public async Task<ScanResult> NetworkScan()
{
// retrieve the server info from the DB
// loop over all servers & then execute some power shell commands to scan the network & retrieve the info for each server one by one...
// after the shell command completed for each server, i will update the related server info inside the DB
目前我在我们的测试环境上做了一些测试,一切都运行良好,扫描大约需要 25 秒来扫描 2 个测试服务器。但是现在我们计划将应用程序转移到生产环境中,我们有大约 120++ 个服务器要扫描. 所以我估计方法执行需要大约 30 -40 分钟才能在生产环境中完成。所以我的问题是如何确保此执行永不过期,并且 ScanNetwork() 方法将完成到最后?