Exemplo n.º 1
0
        public override void ScheduleJob(BaseJob scheduleJob)
        {
            var metadata = Metadata.ExtractData(scheduleJob);

            var jobKey = new JobKey(metadata.Name, metadata.Group);

            if (_quartzScheduler.CheckExists(jobKey).Result)
            {
                // if the job already scheduled, we don't want to re-schedule it
                return;
            }

            var job = JobBuilder.Create <QuartzJob>()
                      .WithIdentity(metadata.Name, metadata.Group)
                      .WithDescription(metadata.Description)
                      .UsingJobData("TypeFullName", metadata.Type.FullName)
                      .Build();

            var trigger = TriggerBuilder.Create()
                          .WithIdentity($"{metadata.Name}Trigger", metadata.Group)
                          .StartNow()
                          .WithCronSchedule(metadata.Schedule, trg => trg
                                            .WithMisfireHandlingInstructionDoNothing())
                          .ForJob(metadata.Name, metadata.Group)
                          .Build();

            _quartzScheduler.ScheduleJob(job, trigger);

            OnJobScheduled(JobInfo.Create(metadata.Group, metadata.Name,
                                          logger: scheduleJob.GetLogger(),
                                          scheduleExp: metadata.Schedule,
                                          schedule: CronExpressionDescriptor.ExpressionDescriptor.GetDescription(metadata.Schedule, new Options()
            {
                Locale = "en"
            }),
                                          nextFire: trigger.GetNextFireTimeUtc()
                                          ));

            // TODO: Find better way to schedule paused job
            if (metadata.State == (byte)JobState.Paused)
            {
                PauseJob(metadata.Name, metadata.Group);
            }
        }