private static void Test002(IScheduler Scheduler)
        {
            IJobDetail job = JobBuilder.Create <SimpleJob>()
                             .WithIdentity("job1", "myJobGroup")
                             .Build();

            ITrigger trigger = TriggerBuilder
                               .Create()
                               .WithSchedule(DailyTimeIntervalScheduleBuilder
                                             .Create()
                                             .OnMondayThroughFriday()
                                             .WithIntervalInSeconds(10))
                               .StartNow()
                               .WithIdentity("trigger1", "myJobGroup")
                               .Build();

            Scheduler.ScheduleJob(job, trigger);

            var myJobListener = new MyJobListener();

            myJobListener.Name         = "MyJobListener";
            myJobListener.JobExecuted += OnJobExecuted;

            Scheduler.ListenerManager.AddJobListener(myJobListener, KeyMatcher <JobKey> .KeyEquals(new JobKey("job1", "myJobGroup")));

            GetAllJobs(Scheduler);
        }
示例#2
0
        public void Fire()
        {
            scheduler.Start();

            Parallel.ForEach(jobTypes, jobType =>
            {
                AutoRunAttribute configuration = jobType.GetAttribute <AutoRunAttribute>();

                if (configuration == null)
                {
                    return; // sanity.
                }
                log.Debug(Resources.Debug.SchedulingAutoRunJob.FormatWith(jobType.Name));

                if (configuration.RunOnce)
                {
                    scheduler.StartJob(jobType);
                }
                else
                {
                    DateTimeOffset now    = DateTimeOffset.UtcNow;
                    DateTimeOffset offset = now.AddMinutes(configuration.Delay);

                    int minutes = configuration.Interval ?? AutoRunAttribute.DefaultInterval;

                    IScheduleBuilder schedule = DailyTimeIntervalScheduleBuilder.Create().WithIntervalInMinutes(minutes);
                    ITrigger trigger          = TriggerBuilder.Create().StartAt(offset).WithSchedule(schedule).Build();

                    scheduler.ScheduleJob(jobType, trigger);
                }
            });
        }
示例#3
0
        private async Task ConfigureScheduler()
        {
            var jobFactory = new JobFactory(ServiceProvider);

            var        schedulerFactory = new StdSchedulerFactory();
            IScheduler scheduler        = await schedulerFactory.GetScheduler();

            scheduler.JobFactory = jobFactory; // Use our DI container to create the Job

            await scheduler.Start();

            IJobDetail job = JobBuilder.Create <AfterLunchJob>()
                             .WithIdentity(nameof(AfterLunchJob), "Lunch Bot")
                             .Build();

            // Trigger the job every Friday sometime after Lunch  ....say 3:00
            var onFriday = DailyTimeIntervalScheduleBuilder.Create()
                           .OnDaysOfTheWeek(new DayOfWeek[] { DayOfWeek.Friday });

            ITrigger trigger = TriggerBuilder.Create()
                               .WithIdentity(nameof(onFriday), "Lunch Bot")
                               .WithSchedule(onFriday)
                               .Build();

            // Tell quartz to schedule the job using our trigger
            await scheduler.ScheduleJob(job, trigger);
        }
        public void DayOfWeekPropertyShouldNotAffectOtherTriggers()
        {
            DailyTimeIntervalScheduleBuilder builder = DailyTimeIntervalScheduleBuilder.Create();

            DailyTimeIntervalTriggerImpl trigger1 = (DailyTimeIntervalTriggerImpl)builder
                                                    .WithInterval(1, IntervalUnit.Hour)
                                                    .OnMondayThroughFriday()
                                                    .Build();

            //make an adjustment to this one trigger.
            //I only want mondays now
            trigger1.DaysOfWeek.Clear();
            trigger1.DaysOfWeek.Add(DayOfWeek.Monday);

            //build same way as trigger1
            DailyTimeIntervalTriggerImpl trigger2 = (DailyTimeIntervalTriggerImpl)builder
                                                    .WithInterval(1, IntervalUnit.Hour)
                                                    .OnMondayThroughFriday()
                                                    .Build();

            //check trigger 2 DOW
            //this fails because the reference collection only contains MONDAY b/c it was cleared.
            Assert.IsTrue(trigger2.DaysOfWeek.Contains(DayOfWeek.Monday));
            Assert.IsTrue(trigger2.DaysOfWeek.Contains(DayOfWeek.Tuesday));
            Assert.IsTrue(trigger2.DaysOfWeek.Contains(DayOfWeek.Wednesday));
            Assert.IsTrue(trigger2.DaysOfWeek.Contains(DayOfWeek.Thursday));
            Assert.IsTrue(trigger2.DaysOfWeek.Contains(DayOfWeek.Friday));

            Assert.IsFalse(trigger2.DaysOfWeek.Contains(DayOfWeek.Saturday));
            Assert.IsFalse(trigger2.DaysOfWeek.Contains(DayOfWeek.Sunday));
        }
示例#5
0
        private static IEnumerable <ITrigger> CreateTrigger(Trigger trigger)
        {
            var triggerBuilder = DailyTimeIntervalScheduleBuilder.Create();
            var builder        = TriggerBuilder.Create()
                                 .StartAt(DateTime.Now.AddSeconds(30))
                                 .WithSchedule(triggerBuilder)
                                 .EndAt(trigger.IsEndDateEnabled ? trigger.EndDate.EndOfTheDay() : (DateTimeOffset?)null);

            if (trigger.OccursOnce)
            {
                triggerBuilder.DailyTriggerOnce(
                    trigger.OccursOnceAtTicks.CastTimeOfDay(),
                    (trigger.SelectedDays()))
                .WithMisfireHandlingInstructionDoNothing();

                return(builder.Build().ToEnumerable());
            }
            else
            {
                triggerBuilder.DailyTriggerReccuring(
                    trigger.Interval,
                    (IntervalUnit)trigger.IntervalUnit,
                    trigger.StartTimeOfDayTicks.CastTimeOfDay(),
                    trigger.EndTimeOfDayTicks.CastTimeOfDay(),
                    (trigger.SelectedDays()))
                .WithMisfireHandlingInstructionDoNothing();

                return(builder.Build().ToEnumerable());
            }
        }
示例#6
0
        private static DailyTimeIntervalScheduleBuilder GetScheduleBuilder(IJobSettings jobSettings)
        {
            var builder = DailyTimeIntervalScheduleBuilder.Create();

            builder = jobSettings.WeekDaysOnly
                ? builder.OnMondayThroughFriday()
                : builder.OnEveryDay();

            return(builder
                   .StartingDailyAt(new TimeOfDay(jobSettings.Time.Hours, jobSettings.Time.Minutes))
                   .EndingDailyAfterCount(1));
        }
        internal static ITrigger CreateTrigger()
        {
            var schedule = DailyTimeIntervalScheduleBuilder.Create()
                           .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 30))
                           .EndingDailyAfterCount(1)
                           .OnEveryDay();

            return(TriggerBuilder.Create()
                   .WithSchedule(schedule)
                   .WithIdentity(nameof(ThisJustInJob))
                   .Build());
        }
示例#8
0
        internal static ITrigger CreateTrigger()
        {
            var schedule = DailyTimeIntervalScheduleBuilder
                           .Create()
                           .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(JobExecutionHour, JobExecutionMinute))
                           .EndingDailyAfterCount(1)
                           .OnEveryDay();

            return(TriggerBuilder.Create()
                   .WithSchedule(schedule)
                   .WithIdentity(nameof(RefreshTVShowsJob))
                   .StartNow()
                   .Build());
        }
示例#9
0
        public void TestDayLightSaving2()
        {
            var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");

            var trigger = DailyTimeIntervalScheduleBuilder.Create()
                          .OnEveryDay()
                          .InTimeZone(timeZoneInfo)
                          .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0))
                          .WithIntervalInHours(4)
                          .Build();

            var first = trigger.GetFireTimeAfter(new DateTimeOffset(2017, 3, 12, 9, 0, 0, TimeSpan.Zero));

            Assert.That(first, !Is.EqualTo(new DateTimeOffset(2017, 3, 12, 9, 0, 0, TimeSpan.Zero)));
        }
示例#10
0
        protected void Application_Start(object sender, EventArgs e)
        {
            // First, initialize Quartz.NET as usual. In this sample app I'll configure Quartz.NET by code.
            var schedulerFactory = new StdSchedulerFactory();
            var scheduler        = schedulerFactory.GetScheduler();

            scheduler.Start();

            // This tells the QuartzNetWebConsole what scheduler to use
            Setup.Scheduler = () => scheduler;

            // This adds an logger to the QuartzNetWebConsole. It's optional.
            var partialQuartzConsoleUrl = string.Format("http://{0}:{1}/quartz/", Context.Request.Url.Host, Context.Request.Url.Port);

            Setup.Logger = new MemoryLogger(1000, partialQuartzConsoleUrl);

            // I'll add some global listeners
            scheduler.ListenerManager.AddJobListener(new GlobalJobListener());
            scheduler.ListenerManager.AddTriggerListener(new GlobalTriggerListener());

            // A sample trigger and job
            var trigger = TriggerBuilder.Create()
                          .WithIdentity("myTrigger")
                          .WithSchedule(DailyTimeIntervalScheduleBuilder.Create()
                                        .WithIntervalInSeconds(6))
                          .StartNow()
                          .Build();
            var job = new JobDetailImpl("myJob", null, typeof(HelloJob));

            scheduler.ScheduleJob(job, trigger);

            // A cron trigger and job
            var cron = TriggerBuilder.Create()
                       .WithIdentity("myCronTrigger")
                       .ForJob(job.Key)
                       .WithCronSchedule("0/10 * * * * ?") // every 10 seconds
                       .Build();

            scheduler.ScheduleJob(cron);

            // A dummy calendar
            scheduler.AddCalendar("myCalendar", new DummyCalendar {
                Description = "dummy calendar"
            }, false, false);
        }
示例#11
0
        public void TestPassingMidnight()
        {
            IOperableTrigger trigger = (IOperableTrigger)DailyTimeIntervalScheduleBuilder.Create()
                                       .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(16, 00))
                                       .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(00, 00))
                                       .OnEveryDay()
                                       .WithIntervalInMinutes(30)
                                       .Build();

            trigger.StartTimeUtc = new DateTimeOffset(2015, 1, 11, 23, 57, 0, 0, TimeSpan.Zero);

            var fireTimes = TriggerUtils.ComputeFireTimes(trigger, null, 100);

            foreach (var fireTime in fireTimes)
            {
                // Console.WriteLine(fireTime.LocalDateTime);
            }
        }
        public void TestDayLightSaving4()
        {
            var timeZoneInfo = TZConvert.GetTimeZoneInfo("Eastern Standard Time");
            //UTC: 2019/11/1/ 23:00  EST: 2019/11/1 19:00
            var startTime = new DateTimeOffset(2019, 11, 1, 23, 0, 0, TimeSpan.Zero);
            var trigger   = DailyTimeIntervalScheduleBuilder.Create()
                            .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(17, 00))
                            .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(19, 30))
                            .OnDaysOfTheWeek(new[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday })
                            .WithIntervalInHours(2)
                            .InTimeZone(timeZoneInfo)
                            .Build();

            var first = trigger.GetFireTimeAfter(startTime);

            //UTC: 2019/11/4/ 22:00  EST: 2019/11/1 17:00
            Assert.That(first, Is.EqualTo(new DateTimeOffset(2019, 11, 4, 22, 0, 0, TimeSpan.Zero)));
        }
        public void TestDayLightSaving3()
        {
            var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
            //UTC: 2020/3/7/ 00:00  EST: 2020/3/6 19:00
            var startTime = new DateTimeOffset(2020, 3, 7, 0, 0, 0, TimeSpan.Zero);
            var trigger   = DailyTimeIntervalScheduleBuilder.Create()
                            .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(17, 00))
                            .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(19, 30))
                            .OnDaysOfTheWeek(new[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday })
                            .WithIntervalInHours(2)
                            .InTimeZone(timeZoneInfo)
                            .Build();

            var first = trigger.GetFireTimeAfter(startTime);

            //UTC: 2020/3/9/ 21:00  EST: 2020/3/9 17:00
            Assert.That(first, Is.EqualTo(new DateTimeOffset(2020, 3, 9, 21, 0, 0, TimeSpan.Zero)));
        }
示例#14
0
 private static DailyTimeIntervalScheduleBuilder GetScheduleBuilder(DataUpdateJobDetails jobDetails)
 {
     if (jobDetails.WeekDaysOnly)
     {
         return(DailyTimeIntervalScheduleBuilder
                .Create()
                .OnMondayThroughFriday()
                .StartingDailyAt(new TimeOfDay(jobDetails.Time.Hours, jobDetails.Time.Minutes))
                .EndingDailyAfterCount(1));
     }
     else
     {
         return(DailyTimeIntervalScheduleBuilder
                .Create()
                .OnEveryDay()
                .StartingDailyAt(new TimeOfDay(jobDetails.Time.Hours, jobDetails.Time.Minutes))
                .EndingDailyAfterCount(1));
     }
 }
示例#15
0
        void Application_Start()
        {
            // First, initialize Quartz.NET as usual. In this sample app I'll configure Quartz.NET by code.
            var schedulerFactory = new StdSchedulerFactory();
            var scheduler        = schedulerFactory.GetScheduler();

            scheduler.Start();

            // This tells the QuartzNetWebConsole what scheduler to use
            Setup.Scheduler = () => scheduler;


            // I'll add some global listeners
            scheduler.ListenerManager.AddJobListener(new GlobalJobListener());
            scheduler.ListenerManager.AddTriggerListener(new GlobalTriggerListener());

            // A sample trigger and job
            var trigger = TriggerBuilder.Create()
                          .WithIdentity("myTrigger")
                          .WithSchedule(DailyTimeIntervalScheduleBuilder.Create()
                                        .WithIntervalInSeconds(6))
                          .StartNow()
                          .Build();
            var job = new JobDetailImpl("myJob", null, typeof(HelloJob));

            scheduler.ScheduleJob(job, trigger);

            // A cron trigger and job
            var cron = TriggerBuilder.Create()
                       .WithIdentity("myCronTrigger")
                       .ForJob(job.Key)
                       .WithCronSchedule("0/10 * * * * ?") // every 10 seconds
                       .Build();

            scheduler.ScheduleJob(cron);

            // A dummy calendar
            scheduler.AddCalendar("myCalendar", new DummyCalendar {
                Description = "dummy calendar"
            }, false, false);
            RouteTable.Routes.Add(new R());
        }
        /// <summary>
        /// Get a <see cref="IScheduleBuilder" /> that is configured to produce a
        /// schedule identical to this trigger's schedule.
        /// </summary>
        /// <returns></returns>
        /// <see cref="TriggerBuilder"/>
        public override IScheduleBuilder GetScheduleBuilder()
        {
            DailyTimeIntervalScheduleBuilder cb = DailyTimeIntervalScheduleBuilder.Create()
                                                  .WithInterval(RepeatInterval, RepeatIntervalUnit)
                                                  .OnDaysOfTheWeek(DaysOfWeek)
                                                  .StartingDailyAt(StartTimeOfDay)
                                                  .EndingDailyAt(EndTimeOfDay);

            switch (MisfireInstruction)
            {
            case Quartz.MisfireInstruction.DailyTimeIntervalTrigger.DoNothing:
                cb.WithMisfireHandlingInstructionDoNothing();
                break;

            case Quartz.MisfireInstruction.DailyTimeIntervalTrigger.FireOnceNow:
                cb.WithMisfireHandlingInstructionFireAndProceed();
                break;
            }

            return(cb);
        }
        private static DailyTimeIntervalScheduleBuilder zCreateDailyTimeIntervalSchedule(DailyRepeatableSchedule schedule)
        {
            DailyTimeIntervalScheduleBuilder sb = DailyTimeIntervalScheduleBuilder.Create();

            switch (schedule.MissedScheduleMode)
            {
            case MissedScheduleMode.RunImmediately:
                sb = sb.WithMisfireHandlingInstructionFireAndProceed();
                break;

            case MissedScheduleMode.RunAtNextScheduledTime:
                sb = sb.WithMisfireHandlingInstructionDoNothing();
                break;

            default:
                throw new NotImplementedException();
            }
            if (schedule.RepeatsDailyOnInterval)
            {
                sb = sb.StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(
                                            schedule.DailyRepetitionStartTimeUtc.Hour,
                                            schedule.DailyRepetitionStartTimeUtc.Minute,
                                            schedule.DailyRepetitionStartTimeUtc.Second))
                     .WithIntervalInSeconds((int)schedule.DailyRepetitionInterval.TotalSeconds)
                     .EndingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(
                                        schedule.DailyRepetitionEndTimeUtc.Hour,
                                        schedule.DailyRepetitionEndTimeUtc.Minute,
                                        schedule.DailyRepetitionEndTimeUtc.Second));
            }
            else
            {
                sb = sb.StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(
                                            schedule.StartTimeUtc.Hour,
                                            schedule.StartTimeUtc.Minute,
                                            schedule.StartTimeUtc.Second))
                     .WithIntervalInHours(24);
            }
            return(sb);
        }
示例#18
0
        public void TestDayLightSaving()
        {
            var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");

            var trigger = DailyTimeIntervalScheduleBuilder.Create()
                          .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(22, 15))
                          .OnEveryDay()
                          .WithIntervalInHours(24)
                          .WithRepeatCount(9999)
                          .InTimeZone(timeZoneInfo)
                          .Build();

            var first = trigger.GetFireTimeAfter(new DateTimeOffset(2014, 10, 25, 0, 0, 0, TimeSpan.Zero));

            Assert.That(first, Is.EqualTo(new DateTimeOffset(2014, 10, 25, 22, 15, 0, TimeSpan.FromHours(1))));

            var second = trigger.GetFireTimeAfter(first);

            Assert.That(second, Is.EqualTo(new DateTimeOffset(2014, 10, 26, 22, 15, 0, TimeSpan.FromHours(0))));

            var third = trigger.GetFireTimeAfter(second);

            Assert.That(third, Is.EqualTo(new DateTimeOffset(2014, 10, 27, 22, 15, 0, TimeSpan.FromHours(0))));
        }
        protected override TriggerPropertyBundle GetTriggerPropertyBundle(SimplePropertiesTriggerProperties props)
        {
            int    repeatCount     = (int)props.Long1;
            int    interval        = props.Int1;
            string intervalUnitStr = props.String1;
            string daysOfWeekStr   = props.String2;
            string timeOfDayStr    = props.String3;

            IntervalUnit intervalUnit = (IntervalUnit)Enum.Parse(typeof(IntervalUnit), intervalUnitStr, true);
            DailyTimeIntervalScheduleBuilder scheduleBuilder = DailyTimeIntervalScheduleBuilder.Create()
                                                               .WithInterval(interval, intervalUnit)
                                                               .WithRepeatCount(repeatCount);

            if (daysOfWeekStr != null)
            {
                ISet <DayOfWeek> daysOfWeek = new HashSet <DayOfWeek>();
                string[]         nums       = daysOfWeekStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (nums.Length > 0)
                {
                    foreach (String num in nums)
                    {
                        daysOfWeek.Add((DayOfWeek)Int32.Parse(num));
                    }
                    scheduleBuilder.OnDaysOfTheWeek(daysOfWeek);
                }
            }
            else
            {
                scheduleBuilder.OnDaysOfTheWeek(DailyTimeIntervalScheduleBuilder.AllDaysOfTheWeek);
            }

            if (timeOfDayStr != null)
            {
                string[]  nums = timeOfDayStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                TimeOfDay startTimeOfDay;
                if (nums.Length >= 3)
                {
                    int hour = Int32.Parse(nums[0]);
                    int min  = Int32.Parse(nums[1]);
                    int sec  = Int32.Parse(nums[2]);
                    startTimeOfDay = new TimeOfDay(hour, min, sec);
                }
                else
                {
                    startTimeOfDay = TimeOfDay.HourMinuteAndSecondOfDay(0, 0, 0);
                }
                scheduleBuilder.StartingDailyAt(startTimeOfDay);

                TimeOfDay endTimeOfDay;
                if (nums.Length >= 6)
                {
                    int hour = Int32.Parse(nums[3]);
                    int min  = Int32.Parse(nums[4]);
                    int sec  = Int32.Parse(nums[5]);
                    endTimeOfDay = new TimeOfDay(hour, min, sec);
                }
                else
                {
                    endTimeOfDay = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59);
                }
                scheduleBuilder.EndingDailyAt(endTimeOfDay);
            }
            else
            {
                scheduleBuilder.StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(0, 0, 0));
                scheduleBuilder.EndingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59));
            }


            int timesTriggered = props.Int2;

            string[] statePropertyNames  = { "timesTriggered" };
            object[] statePropertyValues = { timesTriggered };

            return(new TriggerPropertyBundle(scheduleBuilder, statePropertyNames, statePropertyValues));
        }
示例#20
0
        public ITrigger GetQuartzTrigger(string JobName, string JobGroup)
        {
            var trigger = TriggerBuilder
                          .Create()
                          .WithIdentity(this.Name, this.Group)
                          .ForJob(JobName, JobGroup);

            switch (this.Period)
            {
            case PeriodType.Giornaliero:
                var dayScheduler = DailyTimeIntervalScheduleBuilder
                                   .Create()
                                   .OnEveryDay()
                                   .InTimeZone(TimeZoneInfo.Local)
                                   .StartingDailyAt(new TimeOfDay(this.StartDate.Hour, this.StartDate.Minute, this.StartDate.Second));
                if (this.Interval > 0)
                {
                    dayScheduler.WithInterval(this.Interval, this.IntervalUnit);

                    if (this.LifeUnit == IntervalUnit.Hour)
                    {
                        dayScheduler.EndingDailyAt(new TimeOfDay(this.StartDate.AddHours(this.Life).Hour, this.StartDate.Minute, this.StartDate.Minute));
                    }
                    else
                    {
                        dayScheduler.EndingDailyAt(new TimeOfDay(this.StartDate.AddMinutes(this.Life).Hour, this.StartDate.AddMinutes(this.Life).Minute, this.StartDate.Minute));
                    }
                }
                else
                {
                    dayScheduler.WithInterval(24, IntervalUnit.Hour);
                }
                trigger.WithSchedule(dayScheduler);
                break;

            case PeriodType.Settimanale:
                var weekScheduler = DailyTimeIntervalScheduleBuilder
                                    .Create()
                                    .OnDaysOfTheWeek(this.WeekDays.ToArray())
                                    .InTimeZone(TimeZoneInfo.Local)
                                    .StartingDailyAt(new TimeOfDay(this.StartDate.Hour, this.StartDate.Minute, this.StartDate.Second));
                if (this.Interval > 0)
                {
                    if (this.IntervalUnit != IntervalUnit.Minute && this.IntervalUnit != IntervalUnit.Hour)
                    {
                        throw new InvalidIntervalUnitException("IntervalUnit (" + this.IntervalUnit.ToString() + ") non accettata per il periodo selezionato (" + this.Period + ")");
                    }

                    weekScheduler.WithInterval(this.Interval, this.IntervalUnit);


                    if (this.LifeUnit == IntervalUnit.Hour)
                    {
                        weekScheduler.EndingDailyAt(new TimeOfDay(this.StartDate.AddHours(this.Life).Hour, this.StartDate.Minute, this.StartDate.Minute));
                    }
                    else if (this.LifeUnit == IntervalUnit.Minute)
                    {
                        weekScheduler.EndingDailyAt(new TimeOfDay(this.StartDate.AddMinutes(this.Life).Hour, this.StartDate.AddMinutes(this.Life).Minute, this.StartDate.Minute));
                    }
                    else
                    {
                        throw new InvalidLifeUnitException("LifeUnit (" + this.LifeUnit.ToString() + ") non accettata per il periodo selezinato (" + this.Period + ")");
                    }
                }
                else
                {
                    weekScheduler.WithInterval(24, IntervalUnit.Hour);
                }
                trigger.WithSchedule(weekScheduler);
                break;

            case PeriodType.Mensile:
                var monthsScheduler = CalendarIntervalScheduleBuilder
                                      .Create()
                                      .InTimeZone(TimeZoneInfo.Local);
                if (this.Interval > 0)
                {
                    if (this.IntervalUnit != IntervalUnit.Day && this.IntervalUnit != IntervalUnit.Week)
                    {
                        throw new InvalidIntervalUnitException("IntervalUnit (" + this.IntervalUnit.ToString() + ") non accettata per il periodo selezionato (" + this.Period + ")");
                    }

                    trigger.WithCalendarIntervalSchedule(x =>
                    {
                        x.WithInterval(this.Interval, this.IntervalUnit);
                    });

                    if (this.LifeUnit == IntervalUnit.Day)
                    {
                        trigger.EndAt(new DateTimeOffset(this.StartDate.AddDays(this.Life)));
                    }
                    else if (this.LifeUnit == IntervalUnit.Week)
                    {
                        trigger.EndAt(new DateTimeOffset(this.StartDate.AddDays((7 * this.Life))));
                    }
                    else
                    {
                        throw new InvalidLifeUnitException("LifeUnit (" + this.LifeUnit.ToString() + ") non accettata per il periodo selezinato (" + this.Period + ")");
                    }
                }
                else
                {
                    monthsScheduler.WithInterval(1, IntervalUnit.Month);
                }
                trigger.StartAt(new DateTimeOffset(this.StartDate));
                break;

            case PeriodType.Annuale:
                var yearScheduler = CalendarIntervalScheduleBuilder
                                    .Create()
                                    .InTimeZone(TimeZoneInfo.Local);
                if (this.Interval > 0)
                {
                    if (this.IntervalUnit != IntervalUnit.Year && this.IntervalUnit != IntervalUnit.Month)
                    {
                        throw new InvalidIntervalUnitException("IntervalUnit (" + this.IntervalUnit.ToString() + ") non accettata per il periodo selezionato (" + this.Period + ")");
                    }

                    trigger.WithCalendarIntervalSchedule(x =>
                    {
                        x.WithInterval(this.Interval, this.IntervalUnit);
                    });

                    if (this.LifeUnit == IntervalUnit.Year)
                    {
                        trigger.EndAt(new DateTimeOffset(this.StartDate.AddDays(this.Life)));
                    }
                    else if (this.LifeUnit == IntervalUnit.Month)
                    {
                        trigger.EndAt(new DateTimeOffset(this.StartDate.AddDays((30 * this.Life))));
                    }
                    else
                    {
                        throw new InvalidLifeUnitException("LifeUnit (" + this.LifeUnit.ToString() + ") non accettata per il periodo selezinato (" + this.Period + ")");
                    }
                }
                else
                {
                    yearScheduler.WithInterval(1, IntervalUnit.Year);
                }
                trigger.StartAt(new DateTimeOffset(this.StartDate));
                break;

            default:
                return(null);
            }
            return(trigger.Build());
        }
示例#21
0
        public static async void Start(Task task)
        {
            IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            await scheduler.Start();

            var nameJob = $"{task.Name}-{task.Oid}-{Guid.NewGuid()}";

            IJobDetail job = JobBuilder.Create <ZipArchiveController>()
                             .WithIdentity(nameJob, "CreateZip")
                             .UsingJobData("pathIn", task.CopyDirectory)
                             .UsingJobData("pathOut", task.SaveDirectory)
                             .UsingJobData("taskOid", task.Oid)
                             .Build();

            var daysOfWeek = new List <DayOfWeek>();

            if (task.IsMonday)
            {
                daysOfWeek.Add(DayOfWeek.Monday);
            }

            if (task.IsTuesday)
            {
                daysOfWeek.Add(DayOfWeek.Tuesday);
            }

            if (task.IsWednesday)
            {
                daysOfWeek.Add(DayOfWeek.Wednesday);
            }

            if (task.IsThursday)
            {
                daysOfWeek.Add(DayOfWeek.Thursday);
            }

            if (task.IsFriday)
            {
                daysOfWeek.Add(DayOfWeek.Friday);
            }

            if (task.IsSaturday)
            {
                daysOfWeek.Add(DayOfWeek.Saturday);
            }

            if (task.IsSunday)
            {
                daysOfWeek.Add(DayOfWeek.Sunday);
            }

            var date = Convert.ToDateTime(task.Date);

            var trigger = default(ITrigger);

            if (daysOfWeek.Count == 0)
            {
                trigger = TriggerBuilder.Create()
                          .WithIdentity(nameJob, "CreateZip")
                          .StartAt(DateBuilder.DateOf(date.Hour, date.Minute, date.Second, date.Day, date.Month, date.Year))
                          .Build();
            }
            else
            {
                var dailyTimeInterval = DailyTimeIntervalScheduleBuilder.Create().OnDaysOfTheWeek(daysOfWeek);

                trigger = TriggerBuilder.Create()
                          .WithIdentity(nameJob, "CreateZip")
                          .StartAt(DateBuilder.DateOf(date.Hour, date.Minute, date.Second, date.Day, date.Month, date.Year))
                          .WithSchedule(dailyTimeInterval)
                          .WithCalendarIntervalSchedule(x => x
                                                        .WithIntervalInWeeks(1))
                          .Build();
            }

            await scheduler.ScheduleJob(job, trigger);
        }