c#Quartz.net misfirepolicy不起作用(cron表达)

发布于 2025-01-17 15:10:37 字数 5344 浏览 1 评论 0原文

我正在使用 Quartz 开发一个程序,当我恢复工作时遇到问题。 如果我恢复工作,就会被解雇多次。我搜索了谷歌,但失火警察解决方案在我这边不起作用。

这就是我初始化作业的方式。我使用 cron 表达式(每 10 秒触发一次),并且我还有 Misfire DoNothing 句柄。

        public static IJobDetail? job;
        public static ITrigger? trigger = null;

        public static JobKey jobKey = new JobKey("InboxCheckJob", "group1");
        public static TriggerKey triggerKey = new TriggerKey("InboxCheckTrigger", "group1");

        public static async void Init()
        {
            string intervalcron = System.Configuration.ConfigurationManager.AppSettings["inboxCheckCron"];

            Console.WriteLine("InboxCheckInit");
            logger.Info("InboxCheckInit");

            try
            {
                IScheduler scheduler = HelperClass.GetScheduler();
                await scheduler.Start();

                job = JobBuilder.Create<Jobs.InboxCheck>()
                    .WithIdentity(jobKey)
                    .Build();

                trigger = TriggerBuilder.Create()
                    .WithIdentity(triggerKey)
                    .WithCronSchedule(intervalcron, x => x
                        .WithMisfireHandlingInstructionDoNothing()
                    )
                    .Build();

                await scheduler.ScheduleJob(job, trigger);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                logger.Error(ex.ToString()); 
            }
        }

我就这样暂停我的工作。

        public async Task PauseJob(string id) 
        {
            try
            {
                var scheduler = HelperClass.GetScheduler();

                var jobdata = await HelperClass.JobDetails(id);

                JobKey jobKey = new JobKey(jobdata.jobname, jobdata.group);
                TriggerKey triggerKey = new TriggerKey(jobdata.triggername, jobdata.group);

                if (jobKey != null && triggerKey != null)
                {
                    await Console.Out.WriteLineAsync("called pause");

                    await scheduler.PauseJob(jobKey);
                    logger.Info("Paused Job: " + jobKey.ToString());
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
        }

这就是我恢复工作的方式。

        public async Task ResumeJob(string id)
        {
            try
            {
                var scheduler = HelperClass.GetScheduler();

                var jobdata = await HelperClass.JobDetails(id);

                JobKey jobKey = new JobKey(jobdata.jobname, jobdata.group);
                //TriggerKey triggerKey = new TriggerKey(jobdata.triggername, jobdata.group);

                //await Console.Out.WriteLineAsync("jobkey: " + jobKey.ToString());

                //var job = await scheduler.GetJobDetail(new JobKey(jobdata.jobname, jobdata.group));
                //var trigger = await scheduler.GetTrigger(new TriggerKey(jobdata.triggername, jobdata.group));

                //await Console.Out.WriteLineAsync("job: " + job.ToString());

                if (jobKey != null && triggerKey != null)
                {
                    await Console.Out.WriteLineAsync("called resume");

                    //await scheduler.ScheduleJob(job, trigger);
                    await scheduler.ResumeJob(jobKey);
                    logger.Info("Resume Job: " + jobKey.ToString());
                }
            }
            catch(Exception ex)
            {
                logger.Error(ex.Message);
            }
        }

这是我的日志输出。所以30秒后就会着火。然后它被发射了6次。

2022-03-28 10:15:30.3223 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:15:30', nextexecute = '28.03.2022 10:15:40', lastduration = '00:00:302' where id = 2
2022-03-28 10:15:34.2063 INFO Paused Job: group1.InboxCheckJob
2022-03-28 10:16:34.1687 INFO Resume Job: group1.InboxCheckJob
2022-03-28 10:16:34.5750 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:393' where id = 2
2022-03-28 10:16:34.5750 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:394' where id = 2
2022-03-28 10:16:34.5750 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:391' where id = 2
2022-03-28 10:16:34.5750 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:391' where id = 2
2022-03-28 10:16:34.5954 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:405' where id = 2
2022-03-28 10:16:34.6414 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:457' where id = 2
2022-03-28 10:16:40.3023 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:40', nextexecute = '28.03.2022 10:16:50', lastduration = '00:00:289' where id = 2

我的错误策略不起作用是什么问题?

提前致谢

I'm working on a Program with Quartz and I have a problem when I resume my job.
If I resume my job it gets fired multiple times. I searched google but the misfire police solution does not work on my side.

This is how I initialize my Job. I use a cron expression (gets fired every 10 seconds) and I also have the Misfire DoNothing Handle.

        public static IJobDetail? job;
        public static ITrigger? trigger = null;

        public static JobKey jobKey = new JobKey("InboxCheckJob", "group1");
        public static TriggerKey triggerKey = new TriggerKey("InboxCheckTrigger", "group1");

        public static async void Init()
        {
            string intervalcron = System.Configuration.ConfigurationManager.AppSettings["inboxCheckCron"];

            Console.WriteLine("InboxCheckInit");
            logger.Info("InboxCheckInit");

            try
            {
                IScheduler scheduler = HelperClass.GetScheduler();
                await scheduler.Start();

                job = JobBuilder.Create<Jobs.InboxCheck>()
                    .WithIdentity(jobKey)
                    .Build();

                trigger = TriggerBuilder.Create()
                    .WithIdentity(triggerKey)
                    .WithCronSchedule(intervalcron, x => x
                        .WithMisfireHandlingInstructionDoNothing()
                    )
                    .Build();

                await scheduler.ScheduleJob(job, trigger);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                logger.Error(ex.ToString()); 
            }
        }

I Pause my job like this.

        public async Task PauseJob(string id) 
        {
            try
            {
                var scheduler = HelperClass.GetScheduler();

                var jobdata = await HelperClass.JobDetails(id);

                JobKey jobKey = new JobKey(jobdata.jobname, jobdata.group);
                TriggerKey triggerKey = new TriggerKey(jobdata.triggername, jobdata.group);

                if (jobKey != null && triggerKey != null)
                {
                    await Console.Out.WriteLineAsync("called pause");

                    await scheduler.PauseJob(jobKey);
                    logger.Info("Paused Job: " + jobKey.ToString());
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
        }

And this is how I Resume my Job.

        public async Task ResumeJob(string id)
        {
            try
            {
                var scheduler = HelperClass.GetScheduler();

                var jobdata = await HelperClass.JobDetails(id);

                JobKey jobKey = new JobKey(jobdata.jobname, jobdata.group);
                //TriggerKey triggerKey = new TriggerKey(jobdata.triggername, jobdata.group);

                //await Console.Out.WriteLineAsync("jobkey: " + jobKey.ToString());

                //var job = await scheduler.GetJobDetail(new JobKey(jobdata.jobname, jobdata.group));
                //var trigger = await scheduler.GetTrigger(new TriggerKey(jobdata.triggername, jobdata.group));

                //await Console.Out.WriteLineAsync("job: " + job.ToString());

                if (jobKey != null && triggerKey != null)
                {
                    await Console.Out.WriteLineAsync("called resume");

                    //await scheduler.ScheduleJob(job, trigger);
                    await scheduler.ResumeJob(jobKey);
                    logger.Info("Resume Job: " + jobKey.ToString());
                }
            }
            catch(Exception ex)
            {
                logger.Error(ex.Message);
            }
        }

This is my Log output. So it gets fire at 30 sec. Then it gets fired 6 times.

2022-03-28 10:15:30.3223 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:15:30', nextexecute = '28.03.2022 10:15:40', lastduration = '00:00:302' where id = 2
2022-03-28 10:15:34.2063 INFO Paused Job: group1.InboxCheckJob
2022-03-28 10:16:34.1687 INFO Resume Job: group1.InboxCheckJob
2022-03-28 10:16:34.5750 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:393' where id = 2
2022-03-28 10:16:34.5750 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:394' where id = 2
2022-03-28 10:16:34.5750 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:391' where id = 2
2022-03-28 10:16:34.5750 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:391' where id = 2
2022-03-28 10:16:34.5954 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:405' where id = 2
2022-03-28 10:16:34.6414 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:34', nextexecute = '28.03.2022 10:16:44', lastduration = '00:00:457' where id = 2
2022-03-28 10:16:40.3023 DEBUG == AUTO == update app_scheduled_task set lastexecute = '28.03.2022 10:16:40', nextexecute = '28.03.2022 10:16:50', lastduration = '00:00:289' where id = 2

What is my Problem that the misfirepolicy is not working?

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2025-01-24 15:10:37

没关系,有一个不良的乙烯物配置。

我现在将调度程序设置为

        public static async void SetScheduler()
        {
            NameValueCollection config = new NameValueCollection();
            config.Add("quartz.jobStore.misfireThreshold", "1000");

            factory = new StdSchedulerFactory(config);
            scheduler = await factory.GetScheduler();

            Console.WriteLine("Set Scheduler...");
        }

config.add(“ quartz.jobstore.misfirethreshold”,“ 1000”);默认设置为60000。基本上说:如果工作的时间不超过1分钟,则不被视为失火。

我将其设置为1秒钟,现在可以使用。

Nevermind, there is a misfireThreshold config.

I set my Scheduler now like this

        public static async void SetScheduler()
        {
            NameValueCollection config = new NameValueCollection();
            config.Add("quartz.jobStore.misfireThreshold", "1000");

            factory = new StdSchedulerFactory(config);
            scheduler = await factory.GetScheduler();

            Console.WriteLine("Set Scheduler...");
        }

The config.Add("quartz.jobStore.misfireThreshold", "1000"); is set to 60000 in default. Which basically says: If the job is not longer paused than 1 minute it is not considered as a misfire.

I set it to 1 second on my side, now it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文