Пример #1
0
        public static void InitJobAndTriggerFromJobsettings(this IServiceCollectionQuartzConfigurator quartz, IConfiguration configuration)
        {
            var allJobs = configuration.GetSection("Jobs").Get <List <BaseJobConfig> >();

            Log.Logger.Information($"开始注册 Job");
            Log.Logger.Information($"共获取到 {allJobs.Count} 个 Job");

            foreach (var item in allJobs)
            {
                Log.Logger.Information($"{JsonConvert.SerializeObject(item)}");

                var jobName = $"{item.JobType}_{item.Name}";
                var jobKey  = new JobKey(jobName);
                Log.Logger.Information($"{nameof(jobKey)}_{jobKey}");

                var jobData = new JobDataMap();
                jobData.PutAll(ToIDictionary(item));

                if (item.JobType.ToLower().Contains("testjob"))
                {
                    quartz.AddJob <Jobs.TestJob>(opts => { opts.WithIdentity(jobKey); opts.SetJobData(jobData); });
                }
                if (item.JobType.ToLower().Contains("windowscmdjob"))
                {
                    quartz.AddJob <Jobs.WindowsCMDJob>(opts => { opts.WithIdentity(jobKey); opts.SetJobData(jobData); });
                }

                quartz.AddTrigger(opts => opts
                                  .ForJob(jobKey)
                                  .WithIdentity($"{jobName}_Trigger")
                                  .WithCronSchedule(item.Cron));
            }

            Log.Logger.Information($"结束注册 Job");
        }
 private static void ConfigureQuartz(IServiceCollectionQuartzConfigurator quartz, Action <IServiceCollectionQuartzConfigurator>?configureQuartz)
 {
     quartz.UseMicrosoftDependencyInjectionScopedJobFactory();
     quartz.AddJob <RunQuartzWorkflowDefinitionJob>(job => job.StoreDurably().WithIdentity(nameof(RunQuartzWorkflowDefinitionJob)));
     quartz.AddJob <RunQuartzWorkflowInstanceJob>(job => job.StoreDurably().WithIdentity(nameof(RunQuartzWorkflowInstanceJob)));
     quartz.UseSimpleTypeLoader();
     quartz.UseInMemoryStore();
     configureQuartz?.Invoke(quartz);
 }
Пример #3
0
        private static void AddJobAndTrigger <T>(
            this IServiceCollectionQuartzConfigurator quartz,
            IConfiguration config)
            where T : IJob
        {
            const string QuartzConfig = "Quartz";
            const string GroupName    = "Template_NET_5_Worker";

            var jobName = typeof(T).Name;

            var quartzJobConfiguration = config.GetSection(QuartzConfig)?.GetSection(jobName)?.Get <QuartzJobConfiguration>();

            if (string.IsNullOrEmpty(quartzJobConfiguration?.CronConfig))
            {
                Serilog.Log.Warning("CronConfig for {JobName} not configured, Job will not be configured!", jobName);
                return;
            }

            if (!CronExpression.IsValidExpression(quartzJobConfiguration?.CronConfig))
            {
                Serilog.Log.Warning("CronConfig for {JobName} is invalid, Job will not be configured!", jobName);
                return;
            }

            var jobKey = new JobKey(jobName, GroupName);

            quartz.AddJob <T>(configurator => configurator.WithIdentity(jobKey));
            quartz.AddTrigger(
                configurator => configurator.ForJob(jobKey)
                .WithCronSchedule(quartzJobConfiguration?.CronConfig)
                .WithIdentity($"{jobName}Trigger"));
        }
        public static void AddJobAndTrigger <T>(
            this IServiceCollectionQuartzConfigurator quartz,
            IConfiguration config)
            where T : IJob
        {
            string jobName = typeof(T).Name;

            var configKey    = $"Quartz:{jobName}";
            var cronSchedule = config[configKey];

            if (string.IsNullOrEmpty(cronSchedule))
            {
                throw new Exception($"No Quartz.NET Cron schedule found for job in configuration at {configKey}");
            }


            var jobKey = new JobKey(jobName);

            quartz.AddJob <T>(opts => opts.WithIdentity(jobKey));

            quartz.AddTrigger(opts => opts
                              .ForJob(jobKey)
                              .WithIdentity(jobName + "-trigger")
                              .WithCronSchedule(cronSchedule));
        }
Пример #5
0
        private static void AddJobAndTrigger <T>(
            this IServiceCollectionQuartzConfigurator quartz,
            string configKey, string cronSchedule)
            where T : IJob
        {
            // Use the name of the IJob as the appsettings.json key
            string jobName = typeof(T).Name;

            // Try and load the schedule from configuration

            // Some minor validation
            if (string.IsNullOrEmpty(cronSchedule))
            {
                throw new Exception($"No Quartz.NET Cron schedule found for job in configuration at {configKey}");
            }

            // register the job as before
            var jobKey = new JobKey(jobName);

            quartz.AddJob <T>(opts => opts.WithIdentity(jobKey));

            quartz.AddTrigger(opts => opts
                              .ForJob(jobKey)
                              .WithIdentity(jobName + "-trigger")
                              .WithCronSchedule(cronSchedule)); // use the schedule from configuration
        }
        public static void AddJobAndTrigger <T>(
            this IServiceCollectionQuartzConfigurator quartz,
            IConfiguration config)
            where T : IJob
        {
            // Use the name of the IJob as the appsettings.json key
            var jobName = typeof(T).Name;

            // Try and load the schedule from configuration
            var configKey    = $"Quartz:{jobName}";
            var cronSchedule = config[configKey];

            // Some minor validation
            if (string.IsNullOrEmpty(cronSchedule))
            {
                return;
            }

            // register the job as before
            var jobKey = new JobKey(jobName);

            quartz.AddJob <T>(opts => opts.WithIdentity(jobKey));

            quartz.AddTrigger(opts => opts
                              .ForJob(jobKey)
                              .WithIdentity(jobName + "-trigger")
                              .WithCronSchedule(cronSchedule)); // use the schedule from configuration
        }
Пример #7
0
        public static void AddJobAndTrigger <T>(this IServiceCollectionQuartzConfigurator quartz, IConfiguration config) where T : IJob
        {
            string jobName = typeof(T).Name;
            var    jobKey  = new JobKey(jobName);

            quartz.AddJob <T>(opts => opts.WithIdentity(jobKey));

            quartz.AddTrigger(opts => opts
                              .ForJob(jobKey)
                              .WithIdentity(jobName + "-trigger")
                              .WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever()));
        }
        static void RegisterTaskKickJob(IServiceCollectionQuartzConfigurator configurator, JobOptions jobOptions)
        {
            var jobKey = new JobKey(jobOptions.Id);

            configurator
            .AddJob <KickTaskJob>(c => c
                                  .WithIdentity(jobKey)
                                  .UsingJobData(jobOptions.ToJobDataMap())
                                  )
            .AddTrigger(c => c
                        .ForJob(jobKey)
                        .WithIdentity(jobKey + "-trigger")
                        .WithCronSchedule(jobOptions.Cron)
                        );
        }
Пример #9
0
        public static IServiceCollectionQuartzConfigurator ConfigureJob(this IServiceCollectionQuartzConfigurator configurator, IConfiguration appConfig)
        {
            var interval = appConfig.GetValue <int>("Quartz:repeatInterval");

            var jobKey = new JobKey("Main Job");

            configurator.AddJob <MainJob>(opts => opts.WithIdentity(jobKey));

            configurator.AddTrigger(opts => opts
                                    .ForJob(jobKey)
                                    .WithIdentity("Main Trigger")
                                    .WithSimpleSchedule(s => s.WithIntervalInMinutes(interval).RepeatForever().Build())
                                    );

            return(configurator);
        }
Пример #10
0
        public static IServiceCollectionQuartzConfigurator AddJobAndTrigger <T>(
            this IServiceCollectionQuartzConfigurator quartz, string cronExpression)
            where T : IJob
        {
            var jobName = typeof(T).Name;
            var jobKey  = new JobKey(jobName);

            quartz.AddJob <T>(configurator => configurator.WithIdentity(jobKey));

            quartz.AddTrigger(configurator => configurator
                              .ForJob(jobKey)
                              .WithIdentity($"{jobName}Trigger")
                              .WithCronSchedule(cronExpression)
                              );

            return(quartz);
        }