public static async Task AttachTrigger(ITrigger trigger) { if (trigger.JobKey is null) { throw new Exception("The trigger must have reference to appropriate job.\n Configure method <ForJob> in the TriggerBuilder"); } var jobKey = trigger.JobKey; var existJobKey = await scheduler.CheckExists(jobKey); if (existJobKey) { await scheduler.ScheduleJob(trigger); } else { var repeatTypes = Enum.GetNames(typeof(Repeat)).ToList(); var pattern = string.Join("|", repeatTypes); var foundRepeatType = Regex.Match(jobKey.ToString(), pattern).Value; if (foundRepeatType == string.Empty) { throw new Exception($"The job not found and the job key doesn't match to the declared job types: {jobKey} "); } var repeatType = repeatTypes.IndexOf(foundRepeatType) + 1; var jobDetail = QuartzJob.CreateJob((Repeat)repeatType); await ScheduleJob(jobDetail, trigger); } }
public static ITrigger CreateTriggerForExistedJob(DateTime startDate, string description = "") { var triggerKey = TriggerPrefixName + startDate; var groupKey = TriggersPrefixName + Repeat.Once; var jobKey = QuartzJob.GetJobKey(Repeat.Once); return(TriggerBuilder.Create() .WithIdentity(triggerKey, groupKey) .WithDescription(description) .ForJob(jobKey) .Build()); }
public static ITrigger CreateTriggerForExistedJob(DateTime startDate, Repeat repeatType, string description = "") { if (repeatType == Repeat.Once) { return(CreateTriggerForExistedJob(startDate, description)); } var cronExpression = CronJob.GetCronExpression(repeatType, startDate); var triggerKey = TriggerPrefixName + startDate; var triggersGroupKey = TriggersPrefixName + repeatType; var jobKey = QuartzJob.GetJobKey(repeatType); return(TriggerBuilder.Create() .WithIdentity(triggerKey, triggersGroupKey) .WithCronSchedule(cronExpression) .WithDescription(description) .ForJob(jobKey) .Build()); }