我的 ASP.NET MVC 应用程序中的 Quartz Scheduler 仅在刷新 IIS 服务器上的 webpage/application 后才执行作业

Quartz Scheduler in my ASP.NET MVC application executes job only after refreshing the webpage/application at IIS server

所以我正在为我的 ASP.NET MVC 应用程序使用 Quartz Scheduler,以便安排写入文件的作业。当用户从网页配置作业时,可以根据用户要求每小时、每天、每周等触发该作业。

此 link 中的代码:

但是我放弃了我的 stop() 方法来使配置的作业永远 运行。我能够在本地成功获取作业 运行ning,但只有在 IIS 服务器上刷新 Web 应用程序 运行ning 后,我才能看到文件作业正在执行。

起初,我在 Global.asax.cs 文件的 Application_start() 处调用作业调度程序的 start(),以便在 Web 应用程序启动时恢复作业。

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        using (var db = new M3DBContext())
        {
            //db.Configurations is a table in my db
            var file_loc = (from c in db.Configurations where c.ConfigurationKey == "file_location" select c.ConfigurationValue).Single();
            var freq = (from c in db.Configurations where c.ConfigurationKey == "frequency" select c.ConfigurationValue).Single();
            var delim = (from c in db.Configurations where c.ConfigurationKey == "delimiter" select c.ConfigurationValue).Single();
            var datetime = (from c in db.Configurations where c.ConfigurationKey == "datetime" select c.ConfigurationValue).Single();

            //System.Diagnostics.Debug.WriteLine(file_loc + " " + freq + " " + delim + " " + datetime);
            JobScheduler js = new JobScheduler(datetime,freq);
            if (!(file_loc.Equals("default") || freq.Equals("default") || delim.Equals("default") || datetime.Equals("1970-1-1")))
            {
                js.start();
            }
        }
    }

但主要要求是尽管 Web 应用程序是 open/close/refreshed/unrefreshed,但要按照配置的时间间隔使计划的作业 运行 永远有效。当 Web 应用程序关闭且 IIS 仍处于打开状态时,我的工作没有 运行。

尽管 Web 应用程序是 opened/closed/refreshed/unrefreshed,但如何使我的应用程序的石英调度程序在 IIS 中永远 运行?

Application_Start 仅在第一个 HTTP 请求之后 运行 并且实际上,在 IIS 回收之后,JobScheduler 将不会 运行 直到新请求触发 Web 服务器。您无法更改此行为。

但是,您可以通过在您网站的 IIS 设置中启用 preloadEnabled,使 IIS 运行 成为回收后的默认请求。它将触发 Application_Start.

这就是为什么在虚拟主机中安排事情充其量是冒险的,老实说不推荐的确切原因。如果您有需要定期 运行 的作业,无论网络流量如何,最好的选择始终是处理后台作业的独立服务。它可以存在于同一台服务器上,但服务的设计具有很高的正常运行时间,并且是为保持活动状态和在后台执行任务而量身定制的。

话虽如此,如果您出于某种原因需要将其作为您的网络应用程序的一部分,那么您将需要打开该功能以始终让该应用程序运行运行宁。如上所述,您需要打开预加载并打开 IIS 的始终打开模式 https://blogs.msdn.microsoft.com/vijaysk/2012/10/09/iis-8-whats-new-application-pool-settings/

唯一的替代方法是让外部服务定期或在服务器启动时访问应用程序的页面(任何页面)。这可以作为计划的 task/cron 作业完成,使用简单的 curl 命令加载页面。