Example #1
0
        [Platform(Exclude = "Linux")]  // TODO seems that we have some trouble on Linux with this
        public async Task ReschedulingTriggerShouldKeepOriginalNextFireTime()
        {
            NameValueCollection properties = new NameValueCollection();

            properties["quartz.serializer.type"] = TestConstants.DefaultSerializerType;
            ISchedulerFactory factory   = new StdSchedulerFactory(properties);
            IScheduler        scheduler = await factory.GetScheduler();

            await scheduler.Start();

            var job = JobBuilder.Create <NoOpJob>().Build();
            IOperableTrigger trigger = (IOperableTrigger)TriggerBuilder.Create()
                                       .WithSimpleSchedule(x => x.WithIntervalInHours(1).RepeatForever())
                                       .ForJob(job)
                                       .StartNow()
                                       .Build();

            await scheduler.ScheduleJob(job, trigger);

            trigger = (IOperableTrigger)await scheduler.GetTrigger(trigger.Key);

            Assert.That(trigger.GetPreviousFireTimeUtc(), Is.EqualTo(null));

            var previousFireTimeUtc = DateTimeOffset.UtcNow.AddDays(1);

            trigger.SetPreviousFireTimeUtc(previousFireTimeUtc);
            trigger.SetNextFireTimeUtc(trigger.GetFireTimeAfter(previousFireTimeUtc));

            await scheduler.RescheduleJob(trigger.Key, trigger);

            trigger = (IOperableTrigger)await scheduler.GetTrigger(trigger.Key);

            Assert.That(trigger.GetNextFireTimeUtc().Value.UtcDateTime, Is.EqualTo(previousFireTimeUtc.AddHours(1).UtcDateTime).Within(TimeSpan.FromSeconds(5)));

            await scheduler.Shutdown(true);
        }
        /// <summary>
        /// populate common properties of a trigger.
        /// </summary>
        /// <param name="triggerKey">triggerKey</param>
        /// <param name="properties">trigger's properties</param>
        /// <param name="trigger">IOperableTrigger</param>
        private void PopulateTrigger(TriggerKey triggerKey, IDictionary<string, string> properties, IOperableTrigger trigger)
        {
            trigger.Key = triggerKey;
            var jobGroupName = RedisJobStoreSchema.Split(properties[RedisJobStoreSchema.JobHash]);
            trigger.JobKey = new JobKey(jobGroupName[2], jobGroupName[1]);
            trigger.Description = properties[RedisJobStoreSchema.Description];
            trigger.FireInstanceId = properties[RedisJobStoreSchema.FireInstanceId];
            trigger.CalendarName = properties[RedisJobStoreSchema.CalendarName];
            trigger.Priority = int.Parse(properties[RedisJobStoreSchema.Priority]);
            trigger.MisfireInstruction = int.Parse(properties[RedisJobStoreSchema.MisfireInstruction]);
            trigger.StartTimeUtc = DateTimeFromUnixTimestampMillis(
                                           double.Parse(properties[RedisJobStoreSchema.StartTime]));

            trigger.EndTimeUtc = string.IsNullOrEmpty(properties[RedisJobStoreSchema.EndTime])
                                      ? default(DateTimeOffset?)
                                      : DateTimeFromUnixTimestampMillis(
                                          double.Parse(properties[RedisJobStoreSchema.EndTime]));

            var baseTrigger = trigger as AbstractTrigger;

            if (baseTrigger != null)
            {
                trigger.SetNextFireTimeUtc(string.IsNullOrEmpty(properties[RedisJobStoreSchema.NextFireTime])
                                      ? default(DateTimeOffset?)
                                      : DateTimeFromUnixTimestampMillis(
                                          double.Parse(properties[RedisJobStoreSchema.NextFireTime])));

                trigger.SetPreviousFireTimeUtc(string.IsNullOrEmpty(properties[RedisJobStoreSchema.PrevFireTime])
                                      ? default(DateTimeOffset?)
                                      : DateTimeFromUnixTimestampMillis(
                                          double.Parse(properties[RedisJobStoreSchema.PrevFireTime])));
            }
        }