示例#1
0
        public async Task ScheduleJobAsync(InternalBuilder jobBuilder)
        {
            jobBuilder.Build();

            var jobKey     = new JobKey(jobBuilder.JobData.JobName, jobBuilder.JobData.JobGroup);
            var triggerKey = new TriggerKey(jobBuilder.JobData.TriggerName, jobBuilder.JobData.JobGroup);

            var data = new JobDataMap();

            data.Add("builder", jobBuilder);

            var job = JobBuilder.Create <GenericQuartzJob>()
                      .WithIdentity(jobKey)
                      .UsingJobData(data)
                      .Build();

            var triggerBuilder = TriggerBuilder.Create()
                                 .WithIdentity(triggerKey);

            if (jobBuilder.JobStartDate.HasValue)
            {
                triggerBuilder = triggerBuilder.StartAt((DateTimeOffset)jobBuilder.JobStartDate.Value);
            }
            else
            {
                triggerBuilder = triggerBuilder.StartNow();
            }

            ITrigger trigger;

            if (typeof(IDateJob).IsAssignableFrom(jobBuilder.JobType) && jobBuilder.JobStartDate.HasValue)
            {
                trigger = triggerBuilder
                          .Build();
            }
            else
            {
                trigger = triggerBuilder
                          .WithSimpleSchedule((schedule) =>
                {
                    if (jobBuilder.JobDelay.HasValue)
                    {
                        schedule.WithInterval(jobBuilder.JobDelay.Value);
                    }

                    if (jobBuilder.Infinite)
                    {
                        schedule.RepeatForever();
                    }
                })
                          .Build();
            }

            if (await Scheduler.CheckExists(jobKey))
            {
                await Scheduler.RescheduleJob(triggerKey, trigger);
            }
            else
            {
                await Scheduler.ScheduleJob(job, trigger);
            }
        }