最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

quartz scheduler - Why does ISchedulerLIstener.JobDeleted fire twice for each job? - Stack Overflow

matteradmin6PV0评论

I am creating my jobs from a database table and adding a simple Schedule Listener.

        // Create, configure and start the scheduler.
        this.scheduler = await this.schedulerFactory.GetScheduler(cancellationToken);
        this.scheduler.JobFactory = this.jobFactory;
        this.scheduler.ListenerManager.AddSchedulerListener(this.schedulerListener);
        await this.scheduler.Start(cancellationToken).ConfigureAwait(false);

        // Schedule each the persistent jobs.
        foreach (var jobSchedule in this.dataModel.JobSchedules)
        {
            try
            {
                // Create the job.
                var job = jobSchedule.Job;
                var type = Type.GetType(job.Type);
                ArgumentNullException.ThrowIfNull(type);
                var jobDetail = JobBuilder.Create(type)
                    .WithIdentity(new JobKey(job.Name, job.Group))
                    .Build();

                // Create the trigger.
                var trigger = jobSchedule.Trigger;
                var quartzTriggerBuilder = TriggerBuilder
                    .Create()
                    .WithIdentity(new TriggerKey(trigger.Name, trigger.Group));

                // Add the CRON scheduler.
                if (trigger.CronSchedule != null)
                {
                    if (trigger.TimeZone == null)
                    {
                        quartzTriggerBuilder.WithCronSchedule(trigger.CronSchedule);
                    }
                    else
                    {
                        quartzTriggerBuilder
                            .WithCronSchedule(
                            trigger.CronSchedule,
                            x => x.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById(trigger.TimeZone)));
                    }
                }

                var quartzTrigger = quartzTriggerBuilder.Build();

                // Schedule the job.
                await this.scheduler.ScheduleJob(jobDetail, quartzTrigger, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                this.logger.LogError(exception, "{message}", exception.Message);
            }
        }

The listener implementation is simply:

    public async Task JobDeleted(JobKey jobKey, CancellationToken cancellationToken = default)
    {
            this.logger.LogError("{message}", $"Deleting job ({jobKey.Group},{jobKey.Name})");
    }

This fires twice for every job I have running. I've verified that there is only one job running by putting logging statements on the jobs.

Why does it fire twice?

Post a comment

comment list (0)

  1. No comments so far