예제 #1
0
        private void ScheduleJobs()
        {
            // Initialize Quartz
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

            _scheduler            = schedulerFactory.GetScheduler();
            _scheduler.JobFactory = new NinjectJobFactory(_kernel);

            TimeZoneInfo easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
            bool         isDaylight      = easternTimeZone.IsDaylightSavingTime(DateTime.Now);

            DateTimeOffset morning = DateBuilder.NewDateInTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))
                                     .AtHourMinuteAndSecond(7, 30, 0)
                                     .Build();

            //DateTimeOffset morning = DateBuilder.FutureDate(10, IntervalUnit.Second);

            DateTimeOffset marketAlertTimeInMorning = DateBuilder.NewDateInTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))
                                                      .AtHourMinuteAndSecond(isDaylight ? 7 : 8, 45, 0)
                                                      .Build();

            //DateTimeOffset marketAlertTimeInMorning = DateBuilder.FutureDate(10, IntervalUnit.Second);

            DateTimeOffset suspendedPaymentTimeInMorning = DateBuilder.NewDateInTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))
                                                           .AtHourMinuteAndSecond(11, 0, 0)
                                                           .Build();

            // Schedule task for trial expiries
            IJobDetail trialExpiryJobDetail       = JobBuilder.Create <TrialExpiredJob>().WithIdentity("Trial Expiry Emails").Build();
            ITrigger   dailyTriggerForTrialExpiry = TriggerBuilder.Create()
                                                    .WithIdentity("Daily Trigger (Trial Expiries)")
                                                    .StartAt(morning)
                                                    .WithSimpleSchedule(schedule => schedule.WithInterval(TimeSpan.FromDays(1)).RepeatForever())
                                                    .ForJob(trialExpiryJobDetail)
                                                    .Build();

            _scheduler.ScheduleJob(trialExpiryJobDetail, dailyTriggerForTrialExpiry);

            // Schedule task for email being sent to inactive members
            IJobDetail inactiveMembersJobDetail       = JobBuilder.Create <InactiveMembersJob>().WithIdentity("Inactive Member Emails").Build();
            ITrigger   dailyTriggerForInactiveMembers = TriggerBuilder.Create()
                                                        .WithIdentity("Daily Trigger (Inactive Members)")
                                                        .StartAt(morning)
                                                        .WithSimpleSchedule(schedule => schedule.WithInterval(TimeSpan.FromDays(1)).RepeatForever())
                                                        .ForJob(inactiveMembersJobDetail)
                                                        .Build();

            _scheduler.ScheduleJob(inactiveMembersJobDetail, dailyTriggerForInactiveMembers);

            // Schedule task for email being sent to people who have not signed up after their trial expiry
            IJobDetail userFeedbackJobDetail       = JobBuilder.Create <LostUserFeedbackRequestJob>().WithIdentity("User Feedback Emails").Build();
            ITrigger   dailyTriggerForUserFeedback = TriggerBuilder.Create()
                                                     .WithIdentity("Daily Trigger (User Feedback)")
                                                     .StartAt(morning)
                                                     .WithSimpleSchedule(schedule => schedule.WithInterval(TimeSpan.FromDays(1)).RepeatForever())
                                                     .ForJob(userFeedbackJobDetail)
                                                     .Build();

            _scheduler.ScheduleJob(userFeedbackJobDetail, dailyTriggerForUserFeedback);

            // Schedule task for morning market alert
            IJobDetail morningMarketAlertJobDetail    = JobBuilder.Create <MorningMarketAlertJob>().WithIdentity("Morning Market Alert").Build();
            ITrigger   dailyMorningMarketAlertTrigger = TriggerBuilder.Create()
                                                        .WithIdentity("Daily Trigger (Morning Market Alert)")
                                                        .StartAt(marketAlertTimeInMorning)
                                                        .WithSimpleSchedule(schedule => schedule.WithInterval(TimeSpan.FromDays(1)).RepeatForever())
                                                        .ForJob(morningMarketAlertJobDetail)
                                                        .Build();

            _scheduler.ScheduleJob(morningMarketAlertJobDetail, dailyMorningMarketAlertTrigger);

            // Schedule task for failed payments
            IJobDetail paymentSuspendedJob            = JobBuilder.Create <SuspendedPaymentJob>().WithIdentity("Payment Suspended Job").Build();
            ITrigger   weeklyPaymentSuspensionTrigger = TriggerBuilder.Create()
                                                        .WithIdentity("Weekly Trigger (Payment Suspensions)")
                                                        .StartAt(suspendedPaymentTimeInMorning)
                                                        .WithSimpleSchedule(schedule => schedule.WithInterval(TimeSpan.FromDays(7)).RepeatForever())
                                                        .ForJob(paymentSuspendedJob)
                                                        .Build();

            _scheduler.ScheduleJob(paymentSuspendedJob, weeklyPaymentSuspensionTrigger);

            // Finally, start the scheduler
            _scheduler.Start();
        }