Example #1
0
        private TriggerBuilder GetTriggerBuilder(JobDetail jd, TriggerDetail td)
        {
            var trigger = TriggerBuilder.Create();
            trigger.WithIdentity(td.TriggerKey, jd.Group);
            trigger.ForJob(jd.JobKey);
            trigger.WithPriority(td.Priority);
            trigger.StartAt(td.StartTime);
            if (td.EndTime != null && !td.EndTime.Equals(DateTime.MinValue))
                trigger.EndAt(td.EndTime);

            switch (td.ScheduleTypeValue.ToLower())
            {
                case "seconds":
                    if (td.RepeatInterval > 0)
                    {
                        if (td.RepeatCount > 0)
                        {
                            trigger = trigger.WithSimpleSchedule(x => x
                                            .WithIntervalInSeconds(td.RepeatInterval)
                                            .WithRepeatCount(td.RepeatCount)
                                            );
                        }
                        else
                        {
                            trigger = trigger.WithSimpleSchedule(x => x
                                            .WithIntervalInSeconds(td.RepeatInterval)
                                            .RepeatForever()
                                            );
                        }
                    }
                    else
                        Log.Warn(String.Format("Job {0} was configured with {1} schedule but no Repeat Interval mentioned. Please configure the Repeat Interval correctly !!", jd.JobKey, td.ScheduleType), this);

                    Diagnostics.Log.Info(String.Format("ScheduleType {0} and Schedule : {1})", td.ScheduleType, trigger.ToString()), this);

                    break;
                case "minutes":
                    if (td.RepeatInterval > 0)
                    {
                        if (td.RepeatCount > 0)
                        {
                            trigger = trigger.WithSimpleSchedule(x => x
                                            .WithIntervalInMinutes(td.RepeatInterval)
                                            .WithRepeatCount(td.RepeatCount)
                                            );
                        }
                        else
                        {
                            trigger = trigger.WithSimpleSchedule(x => x
                                            .WithIntervalInMinutes(td.RepeatInterval)
                                            .RepeatForever()
                                            );
                        }
                    }
                    else
                        Log.Warn(String.Format("Job {0} was configured with {1} schedule but no Repeat Interval mentioned. Please configure the Repeat Interval correctly !!", jd.JobKey, td.ScheduleType), this);

                    Diagnostics.Log.Info(String.Format("ScheduleType {0} and Schedule : {1})", td.ScheduleType, trigger.ToString()), this);

                    break;
                case "hours":
                    if (td.RepeatInterval > 0)
                    {
                        if (td.RepeatCount > 0)
                        {
                            trigger = trigger.WithSimpleSchedule(x => x
                                            .WithIntervalInHours(td.RepeatInterval)
                                            .WithRepeatCount(td.RepeatCount)
                                            );
                        }
                        else
                        {
                            trigger = trigger.WithSimpleSchedule(x => x
                                            .WithIntervalInHours(td.RepeatInterval)
                                            .RepeatForever()
                                            );
                        }
                    }
                    else
                        Log.Warn(String.Format("Job {0} was configured with {1} schedule but no Repeat Interval mentioned. Please configure the Repeat Interval correctly !!", jd.JobKey, td.ScheduleType), this);

                    Diagnostics.Log.Info(String.Format("ScheduleType {0} and Schedule : {1})", td.ScheduleType, trigger.ToString()), this);

                    break;

                case "daily":
                    trigger.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(td.StartTime.Hour, td.StartTime.Minute));
                    break;

                case "weekly":
                    //Convert Sitecore DaysOfWeeks property to System.DaysOfWeek which is understood by Quartz.net
                    if (td.DaysOfWeeks != null && td.DaysOfWeeks.Count > 0)
                    {
                        System.DayOfWeek[] dayOfWeeks = new System.DayOfWeek[td.DaysOfWeeks.Count];
                        for (int i = 0; i < td.DaysOfWeeks.Count; i++)
                        {
                            dayOfWeeks[i] = (System.DayOfWeek)Enum.Parse(typeof(System.DayOfWeek), td.DaysOfWeeks[i].DayOfWeekValue.ToString());
                        }

                        trigger.WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(td.StartTime.Hour, td.StartTime.Minute, dayOfWeeks));
                    }
                    else
                    {
                        Log.Warn(String.Format("Job {0} was configured with {1} schedule but \"Day of Weeks\" was not set correctly. Please configure the trigger correctly !!", jd.JobKey, td.ScheduleType), this);

                    }
                    break;

                case "monthly":
                    if (td.DayOfMonth > 0)
                        trigger.WithSchedule(CronScheduleBuilder.MonthlyOnDayAndHourAndMinute(td.DayOfMonth, td.StartTime.Hour, td.StartTime.Minute));
                    else
                        Log.Warn(String.Format("Job {0} was configured with {1} schedule but \"Day of Month\" was not set correctly. Please configure the trigger correctly !!", jd.JobKey, td.ScheduleType), this);

                    break;

                case "custom":
                    if (!String.IsNullOrEmpty(td.CronExpression))
                        trigger.WithSchedule(CronScheduleBuilder.CronSchedule(new CronExpression(td.CronExpression)));
                    else
                        Log.Warn(String.Format("Job {0} was configured with {1} schedule but \"Cron Expression\" was not set correctly. Please configure the trigger correctly !!", jd.JobKey, td.ScheduleType), this);
                    break;

            }

            return trigger;
        }
Example #2
0
        public List<TriggerDetail> GetTriggersForJob(JobDetail jobDetail)
        {
            Database masterDb = Factory.GetDatabase("master");

            //Get the trigger definitions for this job
            string quartzJobTriggersQuery =
                "fast://" + GetJobDefinitionLocation() + "//*[@@parentid='" + jobDetail.Id + "']//*[@@templateid='" + Common.Constants.TriggerDetailTempalteID + "']";

            Item[] quartzJobTriggers = masterDb.SelectItems(quartzJobTriggersQuery);
            List<TriggerDetail> lstTriggers = new List<TriggerDetail>();

            if (quartzJobTriggers != null && quartzJobTriggers.Length > 0)
            {

                foreach (Item triggerItem in quartzJobTriggers)
                {
                    TriggerDetail triggerDetail = new TriggerDetail();
                    triggerDetail.Id = triggerItem.ID.ToString();
                    triggerDetail.ParentItemId = jobDetail.Id;
                    triggerDetail.TriggerKey = triggerItem["Trigger Key"];
                    if (!String.IsNullOrEmpty(triggerItem.Fields["Start Time"].Value))
                    {
                        triggerDetail.StartTime = ((DateField)triggerItem.Fields["Start Time"]).DateTime;
                    }
                    if (!String.IsNullOrEmpty(triggerItem.Fields["End Time"].Value))
                    {
                        triggerDetail.EndTime = ((DateField)triggerItem.Fields["End Time"]).DateTime;
                    }
                    if (!String.IsNullOrEmpty(triggerItem.Fields["Priority"].Value))
                    {
                        triggerDetail.Priority = int.Parse(triggerItem.Fields["Priority"].Value);
                    }

                    if (!String.IsNullOrEmpty(triggerItem.Fields["Day of Month"].Value))
                        triggerDetail.DayOfMonth = int.Parse(triggerItem["Day of Month"]);

                    //if (!String.IsNullOrEmpty(triggerItem.Fields["Repeat Every"].Value))
                    //    triggerDetail.IntervalUnit = triggerItem["Repeat Every"];

                    if (!String.IsNullOrEmpty(triggerItem.Fields["Repeat Interval"].Value))
                        triggerDetail.RepeatInterval = int.Parse(triggerItem["Repeat Interval"]);

                    if (!String.IsNullOrEmpty(triggerItem.Fields["Repeat Count"].Value))
                        triggerDetail.RepeatCount = int.Parse(triggerItem["Repeat Count"]);

                    if (!String.IsNullOrEmpty(triggerItem.Fields["Schedule Type"].Value))
                    {
                        triggerDetail.ScheduleType = triggerItem.Fields["Schedule Type"].Value;
                    }

                    if (!String.IsNullOrEmpty(triggerItem.Fields["Cron Expression"].Value))
                        triggerDetail.CronExpression = triggerItem["Cron Expression"];

                    lstTriggers.Add(triggerDetail);
                } //end for loop for triggers
            } //end if

            return lstTriggers;
        }