Esempio n. 1
0
        public void IntervalScheduleTriggerTest()
        {
            Debug.WriteLine("------- Scheduling Jobs -------------------");

            var semHandle = new EventWaitHandle(false, EventResetMode.AutoReset);

            // Set the job run time.
            DateTime rangeStart  = DateTime.UtcNow;
            DateTime runTime     = rangeStart.AddSeconds(1);
            var      runSchedule = new IntervalSchedule(TimeSpan.FromMilliseconds(500), TimeSpan.Zero, rangeStart);

            // Define the job and tie it to our HelloJob class.
            JobDataMap jobDataMap = new JobDataMap((IDictionary <string, object>) new Dictionary <string, object> {
                { "SemHandle", semHandle }
            });

            IJobDetail job = JobBuilder.Create <HelloJob>()
                             .WithDescription("job1")
                             .WithIdentity("job1", "group1")
                             .UsingJobData(jobDataMap)
                             .Build();

            // Trigger the job to run on the set time.
            ITrigger trigger = TriggerBuilder.Create()
                               .WithIdentity("trigger1", "group1")
                               .WithItinerarySchedule(runSchedule, rangeStart)
                               .Build();

            // Tell Quartz to schedule the job using our trigger.
            sched.ScheduleJob(job, trigger);
            var firstEvent = runSchedule.GetRange(rangeStart, DateTime.MaxValue).First();

            Debug.WriteLine(string.Format("{0} will start at: {1}", job.Description, firstEvent.StartTime.ToString("r")));

            // Start up the scheduler.
            sched.Start();
            Debug.WriteLine("------- Started Scheduler -----------------");

            // Wait long enough so that the scheduler as an opportunity to
            // run the job.
            Debug.WriteLine("------- Waiting a few seconds... -------------");

            // Wait for job to signal 5 times.
            for (int i = 0; i < 5; i++)
            {
                Assert.IsTrue(semHandle.WaitOne(5000));
            }
        }
        public void IntervalScheduleTriggerTest()
        {
            Debug.WriteLine("------- Scheduling Jobs -------------------");

             var semHandle = new EventWaitHandle(false, EventResetMode.AutoReset);

             // Set the job run time.
             DateTime rangeStart = DateTime.UtcNow;
             DateTime runTime = rangeStart.AddSeconds(1);
             var runSchedule = new IntervalSchedule(TimeSpan.FromMilliseconds(500), TimeSpan.Zero, rangeStart);

             // Define the job and tie it to our HelloJob class.
             JobDataMap jobDataMap = new JobDataMap((IDictionary<string, object>)new Dictionary<string, object> {
            { "SemHandle", semHandle }
             });

             IJobDetail job = JobBuilder.Create<HelloJob>()
            .WithDescription("job1")
            .WithIdentity("job1", "group1")
            .UsingJobData(jobDataMap)
            .Build();

             // Trigger the job to run on the set time.
             ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .WithItinerarySchedule(runSchedule, rangeStart)
            .Build();

             // Tell Quartz to schedule the job using our trigger.
             sched.ScheduleJob(job, trigger);
             var firstEvent = runSchedule.GetRange(rangeStart, DateTime.MaxValue).First();
             Debug.WriteLine(string.Format("{0} will start at: {1}", job.Description, firstEvent.StartTime.ToString("r")));

             // Start up the scheduler.
             sched.Start();
             Debug.WriteLine("------- Started Scheduler -----------------");

             // Wait long enough so that the scheduler as an opportunity to
             // run the job.
             Debug.WriteLine("------- Waiting a few seconds... -------------");

             // Wait for job to signal 5 times.
             for (int i = 0; i < 5; i++) {
            Assert.IsTrue(semHandle.WaitOne(5000));
             }
        }
        public void IntervalScheduleTriggerTest()
        {
            Debug.WriteLine("------- Scheduling Jobs -------------------");

             var semHandle = new EventWaitHandle(false, EventResetMode.AutoReset);

             // Set the job run time.
             DateTime rangeStart = DateTime.UtcNow;
             DateTime runTime = rangeStart.AddSeconds(1);
             var runSchedule = new IntervalSchedule(TimeSpan.FromMilliseconds(500), TimeSpan.Zero, rangeStart);

             // Define the job and tie it to our HelloJob class.
             JobDetail job = new JobDetail("job1", "group1", typeof(HelloJob));
             job.JobDataMap["SemHandle"] = semHandle;

             // Trigger the job to run on the set time.
             var trigger = new ItineraryTrigger("trigger1", "group1", runSchedule, rangeStart);

             // Tell Quartz to schedule the job using our trigger.
             sched.ScheduleJob(job, trigger);
             var firstEvent = runSchedule.GetRange(rangeStart, DateTime.MaxValue).First();
             Debug.WriteLine(string.Format("{0} will start at: {1}", job.FullName, firstEvent.StartTime.ToString("r")));

             // Start up the scheduler.
             sched.Start();
             Debug.WriteLine("------- Started Scheduler -----------------");

             // Wait long enough so that the scheduler as an opportunity to
             // run the job.
             Debug.WriteLine("------- Waiting a few seconds... -------------");

             // Wait for job to signal 5 times.
             for (int i = 0; i < 5; i++) {
            Assert.IsTrue(semHandle.WaitOne(5000));
             }
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool Exist(Period periodToCompare)
        {
            if (Schedule != null && Schedule.Count > 0)
            {
                foreach (Period period in Schedule)
                {
                    switch (periodToCompare.RepeatType)
                    {
                    case ScheduleRepeatType.ScheduleRepeatDaily:

                        ISchedule schedCurPer    = new IntervalSchedule(new TimeSpan(1, 0, 0, 0), period.End - period.Begin, period.Begin);
                        ISchedule schedPerToComp = new IntervalSchedule(new TimeSpan(1, 0, 0, 0), periodToCompare.End - periodToCompare.Begin, periodToCompare.Begin);

                        ISchedule overlap = new BoolNonIntersectionSchedule(schedCurPer, schedPerToComp);


                        IEnumerable <TimedEvent> eventsCurPer    = schedCurPer.GetRange(period.Begin, period.End);
                        IEnumerable <TimedEvent> eventsPerToComp = overlap.GetRange(periodToCompare.Begin, periodToCompare.End);

                        if (eventsCurPer.Any())
                        {
                            return(true);
                        }

                        break;


                    case ScheduleRepeatType.ScheduleRepeatWeekly:

                        ISchedule sched1 = new IntervalSchedule(new TimeSpan(7, 0, 0, 0), period.End - period.Begin, period.Begin);


                        IEnumerable <TimedEvent> events1 = sched1.GetRange(DateTime.Now.Add(new TimeSpan(-7, 0, 0, 0)), DateTime.Now.Add(new TimeSpan(7, 0, 0, 0)));

                        foreach (TimedEvent e in events1)
                        {
                            if (DateTime.Now >= e.StartTime && DateTime.Now <= e.EndTime)
                            {
                                return(true);
                            }
                        }

                        break;


                    case ScheduleRepeatType.ScheduleRepeatEverySecondWeek:

                        ISchedule sched2 = new IntervalSchedule(new TimeSpan(14, 0, 0, 0), period.End - period.Begin, period.Begin);

                        IEnumerable <TimedEvent> events2 = sched2.GetRange(DateTime.Now.Add(new TimeSpan(-14, 0, 0, 0)), DateTime.Now.Add(new TimeSpan(14, 0, 0, 0)));

                        foreach (TimedEvent e in events2)
                        {
                            if (DateTime.Now >= e.StartTime && DateTime.Now <= e.EndTime)
                            {
                                return(true);
                            }
                        }

                        break;

                    case ScheduleRepeatType.ScheduleRepeatEveryThirdWeek:

                        ISchedule sched3 = new IntervalSchedule(new TimeSpan(21, 0, 0, 0), period.End - period.Begin, period.Begin);

                        IEnumerable <TimedEvent> events3 = sched3.GetRange(DateTime.Now.Add(new TimeSpan(-21, 0, 0, 0)), DateTime.Now.Add(new TimeSpan(21, 0, 0, 0)));

                        foreach (TimedEvent e in events3)
                        {
                            if (DateTime.Now >= e.StartTime && DateTime.Now <= e.EndTime)
                            {
                                return(true);
                            }
                        }

                        break;

                    case ScheduleRepeatType.ScheduleRepeatMonthly:

                        ISchedule sched4 = new IntervalSchedule(new TimeSpan(DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month), 0, 0, 0), period.End - period.Begin, period.Begin);

                        IEnumerable <TimedEvent> events4 = sched4.GetRange(DateTime.Now.Add(new TimeSpan(-31, 0, 0, 0)), DateTime.Now.Add(new TimeSpan(31, 0, 0, 0)));

                        foreach (TimedEvent e in events4)
                        {
                            if (DateTime.Now >= e.StartTime && DateTime.Now <= e.EndTime)
                            {
                                return(true);
                            }
                        }

                        break;

                    default:
                        if (DateTime.Now >= period.Begin && DateTime.Now <= period.End)
                        {
                            return(true);
                        }
                        break;
                    }
                }
            }

            return(false);
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool IsOnTime(Period periodToCompare = null, bool compareWithCurrentDateTime = true)
        {
            if (Schedule != null && Schedule.Count > 0)
            {
                foreach (Period period in Schedule)
                {
                    switch (period.RepeatType)
                    {
                    case ScheduleRepeatType.ScheduleRepeatDaily:

                        ISchedule sched0 = new IntervalSchedule(new TimeSpan(1, 0, 0, 0), period.End - period.Begin, period.Begin);
                        IEnumerable <TimedEvent> events0 = sched0.GetRange(DateTime.Now.Add(new TimeSpan(-1, 0, 0, 0)), DateTime.Now.Add(new TimeSpan(1, 0, 0, 0)));

                        foreach (TimedEvent e in events0)
                        {
                            if (DateTime.Now >= e.StartTime && DateTime.Now <= e.EndTime)
                            {
                                return(true);
                            }
                        }

                        break;

                    case ScheduleRepeatType.ScheduleRepeatWeekly:

                        ISchedule sched1 = new IntervalSchedule(new TimeSpan(7, 0, 0, 0), period.End - period.Begin, period.Begin);


                        IEnumerable <TimedEvent> events1 = sched1.GetRange(DateTime.Now.Add(new TimeSpan(-7, 0, 0, 0)), DateTime.Now.Add(new TimeSpan(7, 0, 0, 0)));

                        foreach (TimedEvent e in events1)
                        {
                            if (DateTime.Now >= e.StartTime && DateTime.Now <= e.EndTime)
                            {
                                return(true);
                            }
                        }

                        break;


                    case ScheduleRepeatType.ScheduleRepeatEverySecondWeek:

                        ISchedule sched2 = new IntervalSchedule(new TimeSpan(14, 0, 0, 0), period.End - period.Begin, period.Begin);

                        IEnumerable <TimedEvent> events2 = sched2.GetRange(DateTime.Now.Add(new TimeSpan(-14, 0, 0, 0)), DateTime.Now.Add(new TimeSpan(14, 0, 0, 0)));

                        foreach (TimedEvent e in events2)
                        {
                            if (DateTime.Now >= e.StartTime && DateTime.Now <= e.EndTime)
                            {
                                return(true);
                            }
                        }

                        break;

                    case ScheduleRepeatType.ScheduleRepeatEveryThirdWeek:

                        ISchedule sched3 = new IntervalSchedule(new TimeSpan(21, 0, 0, 0), period.End - period.Begin, period.Begin);

                        IEnumerable <TimedEvent> events3 = sched3.GetRange(DateTime.Now.Add(new TimeSpan(-21, 0, 0, 0)), DateTime.Now.Add(new TimeSpan(21, 0, 0, 0)));

                        foreach (TimedEvent e in events3)
                        {
                            if (DateTime.Now >= e.StartTime && DateTime.Now <= e.EndTime)
                            {
                                return(true);
                            }
                        }

                        break;

                    case ScheduleRepeatType.ScheduleRepeatMonthly:

                        ISchedule sched4 = new IntervalSchedule(new TimeSpan(DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month), 0, 0, 0), period.End - period.Begin, period.Begin);

                        IEnumerable <TimedEvent> events4 = sched4.GetRange(DateTime.Now.Add(new TimeSpan(-31, 0, 0, 0)), DateTime.Now.Add(new TimeSpan(31, 0, 0, 0)));

                        foreach (TimedEvent e in events4)
                        {
                            if (DateTime.Now >= e.StartTime && DateTime.Now <= e.EndTime)
                            {
                                return(true);
                            }
                        }

                        break;

                    default:
                        if (DateTime.Now >= period.Begin && DateTime.Now <= period.End)
                        {
                            return(true);
                        }
                        break;
                    }
                }
            }

            return(false);
        }