Delete existing triggers if the value is updated

30 Views Asked by At

I have the below code which is called from program.cs I originally thought I wont need to get Ischeduler as everything is done for me already. However, if someone goes and updates the trigger value. The job will then be linked up to multiple the new trigger and the existing one which I want to avoid. What's the best way to automatically delete existing triggers if a the value was updated.

   public static async Task AddInfrastructure(this IServiceCollection service, IConfiguration config)
    {
        var quartzConfig = config.GetSection("Quartz");

        // base configuration for DI, read from appSettings.json
        service.Configure<QuartzOptions>(quartzConfig);

        // if you are using persistent job store, you might want to alter some options
        service.Configure<QuartzOptions>(options =>
        {
            options.Scheduling.IgnoreDuplicates = true; // default: false
            options.Scheduling.OverWriteExistingData = true; // default: true
        });

        service.AddQuartz(q =>
        {
            // handy when part of cluster or you want to otherwise identify multiple schedulers
            q.SchedulerId = "AUTO";

            // you can control whether job interruption happens for running jobs when scheduler is shutting down
            q.InterruptJobsOnShutdown = true;

            // when QuartzHostedServiceOptions.WaitForJobsToComplete = true or scheduler.Shutdown(waitForJobsToComplete: true)
            q.InterruptJobsOnShutdownWithWait = true;

            // we can change from the default of 1
            q.MaxBatchSize = 5;


            // these are the defaults
            q.UseSimpleTypeLoader();
            q.UseInMemoryStore();
            q.UseDefaultThreadPool(maxConcurrency: 10);

            var queueManagerJobInterval = int.TryParse(config[AppSettingsKey.QueueManagerJobIntervalInMinutes], out int interval) ? interval : QueueManagerJobIntervalInMinutes;

            var jobKey = new JobKey(nameof(QueueManagerJob));

            q.AddJob<QueueManagerJob>(jobKey);

            q.AddTrigger(t => t
                .ForJob(jobKey)
                .WithIdentity(string.Format("{0}-MMinute-{1}", nameof(QueueManagerJob), queueManagerJobInterval))
                .WithSimpleSchedule(a =>
                    a.WithIntervalInMinutes(queueManagerJobInterval)
                        .RepeatForever()
                        .WithMisfireHandlingInstructionNextWithRemainingCount())
                .StartNow()
            );

            q.UsePersistentStore(s =>
            {
                s.PerformSchemaValidation = true; // default
                s.UseProperties = true; // preferred, but not default
                s.RetryInterval = TimeSpan.FromSeconds(15);
                s.UseSqlServer(sqlServer =>
                {

                    sqlServer.ConnectionStringName = "Quartz";

                    // this is the default
                    sqlServer.TablePrefix = "QRTZ_";
                });
                s.UseNewtonsoftJsonSerializer();
                s.UseClustering(c =>
                {
                    c.CheckinMisfireThreshold = TimeSpan.FromSeconds(20);
                    c.CheckinInterval = TimeSpan.FromSeconds(10);
                });
            });
        });

        service.AddTransient<QueueManagerJob>();


        // Quartz.Extensions.Hosting hosting
        service.AddQuartzHostedService(options =>
        {
            // when shutting down we want jobs to complete gracefully
            options.WaitForJobsToComplete = true;
        });
    }

Would appreciate some help as I am new to Quartz

0

There are 0 best solutions below