How to use cron misfire-instruction FireOnceNow with AdoJobStore in Quartz.NET 2.0?

孤者浪人 提交于 2019-12-12 06:23:36

问题


I am using a cron trigger with misfire-instruction set to FireOnceNow in quartz.net-2.0 set up with AdoJobStore and using XMLSchedulingDataProcessorPlugin.

cron-expression is set so the job will trigger every 1 minute: 0 0/1 * * * ?.

The job scheduler is set to start in Global.asax.cs.

Expected behaviour: If the server is stopped when a job should trigger but it is restarted before the next trigger then it should trigger once immediately.

Example:

First job triggers at 00:01:00.

I stop the server before 00:02:00 can trigger but start it a few seconds after it should, let's say 00:02:10.

When I restart the server (at 00:02:10) I would expect for the job that misfired at 00:02:00 would fire once and then normal trigger behavior would continue.

What really happens is that nothing triggers. It just continues triggering from 00:03:00.

Any ideas what I have to do to make it work as expected?

Thanks for your help!

@Global.asax.cs:

private IScheduler scheduler;

protected void Application_Start(object sender, EventArgs e)
{
    var factory = new StdSchedulerFactory();
    scheduler = factory.GetScheduler();
    scheduler.Start();
}

@quartz_jobs.xml:

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     version="2.0">
  <processing-directives>
    <overwrite-existing-data>true</overwrite-existing-data>
  </processing-directives>
  <schedule>
    <job>
      <name>TestJob</name>
      <group>Default</group>
      <job-type>BO.TestJob, BO</job-type>
      <durable>true</durable>
      <recover>true</recover>
    </job>
    <trigger>
      <cron>
        <name>TestCron</name>
        <group>Default</group>
        <job-name>TestJob</job-name>
        <job-group>Default</job-group>
        <misfire-instruction>FireOnceNow</misfire-instruction>
        <cron-expression>0 0/1 * * * ?</cron-expression>
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>

@web.config:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="log4net.Internal.Debug" value="false"/>
  </appSettings>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="INLINE"/>
        <arg key="configFile" value="Trace/application.log.txt"/>
        <arg key="level" value="ALL" />
      </factoryAdapter>
    </logging>
  </common>
  <log4net>
    <appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
      <file value="Trace/application.log.txt"/>
      <appendToFile value="true"/>
      <maximumFileSize value="1024KB"/>
      <rollingStyle value="Size"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d{HH:mm:ss} [%t] %-5p %c - %m%n"/>
      </layout>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="GeneralLog"/>
    </root>
  </log4net>
  <quartz>
    <add key="quartz.scheduler.instanceName" value="TestQuartzServer" />
    <add key="quartz.scheduler.instanceId" value="instance_one" />
    <add key="quartz.threadPool.threadCount" value="10" />
    <add key="quartz.threadPool.threadPriority" value="Normal" />
    <add key="quartz.jobStore.misfireThreshold" value="3000" />
    <add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" />
    <add key="quartz.jobStore.driverDelegateType" value="Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz" />
    <add key="quartz.jobStore.useProperties" value="false" />
    <add key="quartz.jobStore.dataSource" value="default" />
    <add key="quartz.jobStore.tablePrefix" value="qrtz_" />
    <add key="quartz.jobStore.lockHandler.type" value="Quartz.Impl.AdoJobStore.SimpleSemaphore, Quartz" />
    <add key="quartz.dataSource.default.connectionStringName" value="PostgreSqlDb" />
    <add key="quartz.dataSource.default.provider" value="Npgsql-20" />
    <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" />
    <add key="quartz.plugin.xml.fileNames" value="~/quartz_jobs.xml" />
    <add key="quartz.plugin.xml.scanInterval" value="10" />
  </quartz>
  <connectionStrings>
    <add name="PostgreSqlDb" connectionString="Server=localhost;database=quartz_net;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
</configuration>

@TestJob.cs:

using System;
using System.Diagnostics;
using Quartz;

namespace BO
{
    public class TestJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Debug.WriteLine("@{0}: {1}", DateTime.Now, context.JobDetail.Key);
        }
    }
}

回答1:


The problem was that the trigger data required for misfire action to work properly was overridden on each automatic processing of quartz_jobs.xml.

So setting overwrite-existing-data to false was required for the trigger to work after misfire:

<processing-directives>
  <overwrite-existing-data>false</overwrite-existing-data>
  <ignore-duplicates>true</ignore-duplicates>
</processing-directives>


来源:https://stackoverflow.com/questions/13085934/how-to-use-cron-misfire-instruction-fireoncenow-with-adojobstore-in-quartz-net-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!