Esempio n. 1
0
        public async Task <WeeklyReleaseSchedule> ExecuteAsync(QueueId queueId)
        {
            queueId.AssertNotNull("queueId");

            var parameters = new
            {
                QueueId = queueId.Value
            };

            using (var connection = this.connectionFactory.CreateConnection())
            {
                var releaseTimes = await connection.QueryAsync <WeeklyReleaseTime>(Sql, parameters);

                var hoursOfWeek = releaseTimes.Select(_ => HourOfWeek.Parse(_.HourOfWeek)).ToArray();

                if (hoursOfWeek.Length == 0)
                {
                    throw new Exception(
                              string.Format(
                                  "Queue does not have any weekly release times defined. At least one should exist per collection at all times. {0}",
                                  queueId));
                }

                return(WeeklyReleaseSchedule.Parse(hoursOfWeek));
            }
        }
        public void ItShouldReturnDatesThatAlignWithReleaseTimes()
        {
            foreach (var schedule in Schedules)
            {
                foreach (var date in Dates)
                {
                    var results = this.target.GetNextLiveDates(date, WeeklyReleaseSchedule.Parse(schedule), 10);

                    var expectedFirstReleaseTime = 0;
                    var currentHourOfWeek        = HourOfWeek.Parse(date);
                    for (var i = 0; i < schedule.Count; i++)
                    {
                        // If this never evaluates, it means the current date is beyond any scheduled date for this week,
                        // so we wrap around to the start of next week, which is 0 (initialized above).
                        if (currentHourOfWeek.Value < schedule[i].Value)
                        {
                            expectedFirstReleaseTime = i;
                            break;
                        }
                    }

                    for (var i = 0; i < results.Count; i++)
                    {
                        var expectedWeeklyReleaseTime = schedule[(i + expectedFirstReleaseTime) % schedule.Count];
                        var actualWeeklyReleaseTime   = HourOfWeek.Parse(results[i]);

                        Assert.AreEqual(expectedWeeklyReleaseTime, actualWeeklyReleaseTime);
                    }
                }
            }
        }
        public async Task WhenPostingCollection_ItShouldIssueCreateCollectionCommand()
        {
            const byte HourOfWeekValue          = 42;
            var        initialWeeklyReleaseTime = HourOfWeek.Parse(HourOfWeekValue);

            this.random.Setup(_ => _.Next(Shared.HourOfWeek.MinValue, Shared.HourOfWeek.MaxValue + 1)).Returns(HourOfWeekValue);
            var data    = new NewQueueData(BlogId, QueueName.Value);
            var command = new CreateQueueCommand(Requester, QueueId, BlogId, QueueName, initialWeeklyReleaseTime);

            this.requesterContext.Setup(_ => _.GetRequesterAsync()).ReturnsAsync(Requester);
            this.guidCreator.Setup(_ => _.CreateSqlSequential()).Returns(QueueId.Value);
            this.createCollection.Setup(_ => _.HandleAsync(command)).Returns(Task.FromResult(0)).Verifiable();

            var result = await this.target.PostQueueAsync(data);

            Assert.AreEqual(result, new QueueCreation(QueueId, initialWeeklyReleaseTime));
            this.createCollection.Verify();
        }
Esempio n. 4
0
        public IReadOnlyList <DateTime> GetNextLiveDates(
            DateTime exclusiveLowerBound,
            WeeklyReleaseSchedule weeklyReleaseSchedule,
            int numberOfLiveDatesToReturn)
        {
            exclusiveLowerBound.AssertUtc("exclusiveLowerBound");
            weeklyReleaseSchedule.AssertNotNull("weeklyReleaseSchedule");
            numberOfLiveDatesToReturn.AssertNonNegative("numberOfLiveDatesToReturn");

            var startTimeWithWeekReset = new DateTime(
                exclusiveLowerBound.Year,
                exclusiveLowerBound.Month,
                exclusiveLowerBound.Day,
                0,
                0,
                0,
                DateTimeKind.Utc)
                                         .AddDays(-1 * (int)exclusiveLowerBound.DayOfWeek);

            var ascendingWeeklyReleaseTimes     = weeklyReleaseSchedule.Value;
            var releasesPerWeek                 = ascendingWeeklyReleaseTimes.Count;
            var currentHourOfWeek               = HourOfWeek.Parse(exclusiveLowerBound).Value;
            var nextReleaseTimeAfterCurrentTime = ascendingWeeklyReleaseTimes.FirstOrDefault(_ => _.Value > currentHourOfWeek);
            var startFromNextWeek               = nextReleaseTimeAfterCurrentTime == null;
            var releaseTimeIndexOffset          = startFromNextWeek ? releasesPerWeek : ascendingWeeklyReleaseTimes.IndexOf(nextReleaseTimeAfterCurrentTime);

            var result = new List <DateTime>();

            for (var i = releaseTimeIndexOffset; i < numberOfLiveDatesToReturn + releaseTimeIndexOffset; i++)
            {
                var weeksFilled       = i / releasesPerWeek;
                var releaseTimeIndex  = i % releasesPerWeek;
                var releaseHourOfWeek = ascendingWeeklyReleaseTimes[releaseTimeIndex].Value;

                var releaseWeek = startTimeWithWeekReset.AddDays(weeksFilled * 7);
                var releaseTime = releaseWeek.AddHours(releaseHourOfWeek);

                result.Add(releaseTime);
            }

            return(result);
        }
        public async Task <QueueCreation> PostQueueAsync(NewQueueData newQueueData)
        {
            newQueueData.AssertBodyProvided("newQueueData");
            var newQueue = newQueueData.Parse();

            var requester = await this.requesterContext.GetRequesterAsync();

            var newQueueId = new QueueId(this.guidCreator.CreateSqlSequential());

            // Spread default release dates so posts are not delivered on same date as standard.
            var hourOfWeek = (byte)this.random.Next(HourOfWeek.MinValue, HourOfWeek.MaxValue + 1);
            var initialWeeklyReleaseTime = HourOfWeek.Parse(hourOfWeek);

            await this.createQueue.HandleAsync(
                new CreateQueueCommand(
                    requester,
                    newQueueId,
                    newQueue.BlogId,
                    newQueue.Name,
                    initialWeeklyReleaseTime));

            return(new QueueCreation(newQueueId, initialWeeklyReleaseTime));
        }