You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quartz .NET Scheduler在客户UAT环境失效,本地及测试环境正常

问题排查与解决建议

针对你遇到的Quartz.NET调度器在UAT环境完全不触发的问题,可从以下几个方向逐一排查:

1. 确认调度器是否成功启动

当前启动代码未捕获初始化阶段的异常,可能调度器在UAT环境启动失败但未被记录。修改JobScheduler.Start()方法,添加启动日志与异常捕获:

public static void Start()
{
    try
    {
        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
        scheduler.Start();

        IJobDetail job = JobBuilder.Create<EmailTrigger>().Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithDailyTimeIntervalSchedule
            (s =>
              s.WithIntervalInMinutes(10)
              .OnEveryDay()
              .InTimeZone(TimeZoneInfo.Local)
            )
            .Build();

        scheduler.ScheduleJob(job, trigger);
        // 新增调度器启动成功日志
        dtLayer.execute(@"Insert into EmailSendLogs(Message,Description,Date) 
                         values ('Scheduler Initialized','Quartz scheduler started successfully' , GetDate())");
    }
    catch (Exception ex)
    {
        // 记录启动失败异常
        dtLayer.execute(@"Insert into EmailSendLogs(Message,Description,Date) 
                         values ('Scheduler Start Failed','" + dtLayer.checkNull(ex.ToString()) + @"',GetDate())");
        throw;
    }
}

同时在global.asax.cs的Application_Start方法开头添加日志,确认该方法是否被执行:

protected void Application_Start(object sender, EventArgs e)
{
    // 记录应用启动日志
    dtLayer.execute(@"Insert into EmailSendLogs(Message,Description,Date) 
                     values ('App Started','Application_Start executed' , GetDate())");
    JobScheduler.Start();
}

部署后检查EmailSendLogs表,若没有启动相关日志,说明Application_Start未执行或调度器初始化失败。

2. 排查IIS应用池回收配置

ASP.NET应用池默认会定期回收,回收后内存中的Quartz调度器会被销毁,导致后续不再触发任务:

  • 登录UAT环境的IIS管理器,找到对应应用池,查看回收配置:
    • 取消“固定时间间隔(分钟)”的勾选,或延长间隔时间
    • 取消“请求限制”中的回收设置(若开启)
  • 开启应用池的启动模式为“AlwaysRunning”,确保应用持续运行,避免闲置被回收

3. 调整Quartz触发器配置

DailyTimeIntervalSchedule可能因时区或默认时间范围限制导致触发异常,尝试替换为更简单的SimpleSchedule:

ITrigger trigger = TriggerBuilder.Create()
    .WithSimpleSchedule(s =>
        s.WithIntervalInMinutes(10)
         .RepeatForever())
    .Build();

该配置更直接,避免DailyTimeIntervalSchedule可能存在的时区转换或时间范围问题。

4. 启用Quartz.NET内部日志

Quartz的内部日志能帮你定位调度器是否真的触发了任务。添加Quartz日志配置(在app.config或web.config中):

<configSections>
  <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<quartz>
  <add key="quartz.scheduler.instanceName" value="EmailScheduler" />
  <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
  <add key="quartz.threadPool.threadCount" value="3" />
  <add key="quartz.threadPool.threadPriority" value="Normal" />
  <add key="quartz.jobStore.misfireThreshold" value="60000" />
  <add key="quartz.plugin.triggHistory.type" value="Quartz.Plugin.History.LoggingTriggerHistoryPlugin, Quartz" />
  <add key="quartz.plugin.jobHistory.type" value="Quartz.Plugin.History.LoggingJobHistoryPlugin, Quartz" />
</quartz>
<system.diagnostics>
  <trace autoflush="true">
    <listeners>
      <add name="quartzTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Logs\QuartzLog.txt" />
    </listeners>
  </trace>
  <sources>
    <source name="Quartz" switchName="QuartzTraceLevel" switchType="System.Diagnostics.SourceSwitch">
      <listeners>
        <add name="quartzTraceListener" />
      </listeners>
    </source>
  </sources>
  <switches>
    <add name="QuartzTraceLevel" value="Verbose" />
  </switches>
</system.diagnostics>

部署后查看生成的QuartzLog.txt,确认调度器是否初始化、触发器是否被调度、任务是否被执行。

5. 检查UAT环境权限与依赖

  • 数据库权限:确认IIS应用池的运行身份有访问EmailSendLogs和EmailSendData表的权限,避免因权限不足导致日志无法写入,误以为任务未触发
  • .NET框架版本:UAT环境需安装与开发环境一致的.NET框架(Visual Studio 2010默认是.NET 4.0),避免版本不兼容
  • 线程权限:部分UAT环境可能限制应用创建线程,Quartz需要线程池来执行任务,需确认环境允许应用创建线程

6. 考虑Quartz持久化存储

若UAT环境应用池无法避免回收,可将Quartz的作业与触发器持久化到数据库,这样应用池回收后调度器能从数据库恢复任务:

  • 配置Quartz使用ADO.NET作业存储,根据数据库类型设置对应连接字符串与存储实现

内容的提问来源于stack exchange,提问作者Unknown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 12:24:55