public ScheduleRecord CreateSchedule(ScheduleRecord record, IDbTransaction transaction) { const string Sql = @"INSERT INTO [BlueCollarSchedule]([ApplicationName],[QueueName],[Name],[StartOn],[EndOn],[RepeatType],[RepeatValue],[Enabled],[Enqueueing],[EnqueueingUpdatedOn]) VALUES(@ApplicationName,@QueueName,@Name,@StartOn,@EndOn,@RepeatTypeString,@RepeatValue,@Enabled,@Enqueueing,@EnqueueingUpdatedOn); SELECT CAST(SCOPE_IDENTITY() AS bigint);"; record.Id = this.connection.Query<long>( Sql, record, transaction, true, null, null).First(); return record; }
/// <summary> /// Updates the given schedule. /// </summary> /// <param name="record">The schedule record to update.</param> /// <param name="transaction">The transaction to use, if applicable.</param> /// <returns>The updated record.</returns> public ScheduleRecord UpdateSchedule(ScheduleRecord record, IDbTransaction transaction) { const string Sql = @"UPDATE [BlueCollarSchedule] SET [QueueName] = @QueueName, [Name] = @Name, [StartOn] = @StartOn, [EndOn] = @EndOn, [RepeatType] = @RepeatTypeString, [Repeatvalue] = @RepeatValue, [Enabled] = @Enabled, [Enqueueing] = @Enqueueing, [EnqueueingUpdatedOn] = @EnqueueingUpdatedOn WHERE [Id] = @Id;"; this.connection.Execute( Sql, record, transaction, null, null); return record; }
public bool CanScheduleBeEnqueued(ScheduleRecord schedule, DateTime windowBegin, DateTime windowEnd, out DateTime?scheduleDate) { if (schedule == null) { throw new ArgumentNullException("schedule", "schedule cannot be null."); } bool can = false; scheduleDate = null; windowBegin = windowBegin.FloorWithSeconds(); windowEnd = windowEnd.FloorWithSeconds(); DateTime sd = schedule.StartOn.FloorWithSeconds(); DateTime ed = (schedule.EndOn ?? DateTime.MaxValue).FloorWithSeconds(); if (ed > windowEnd) { int repeatSeconds; switch (schedule.RepeatType) { case ScheduleRepeatType.None: repeatSeconds = 0; break; case ScheduleRepeatType.Seconds: repeatSeconds = 1; break; case ScheduleRepeatType.Minutes: repeatSeconds = 60; break; case ScheduleRepeatType.Hours: repeatSeconds = 3600; break; case ScheduleRepeatType.Days: repeatSeconds = 86400; break; case ScheduleRepeatType.Weeks: repeatSeconds = 604800; break; default: throw new NotImplementedException(); } Action log = () => { if (this.logger != null) { this.logger.Debug( "Schedule can be enqueued with start date of {0:yyyy-MM-dd HH:mm:ss}, repeat type of {1} and repeat value of {2}. Window: {3:yyyy-MM-dd HH:mm:ss} - {4:yyyy-MM-dd HH:mm:ss}; calculated schedule date: {5:yyyy-MM-dd HH:mm:ss}", schedule.StartOn, schedule.RepeatType, schedule.RepeatValue ?? 0, windowBegin, windowEnd, sd); } }; if (sd < windowBegin && schedule.RepeatValue != null && schedule.RepeatValue > 0) { long totalRepeatSeconds = repeatSeconds * schedule.RepeatValue.Value; double diffSeconds = windowBegin.Subtract(sd).TotalSeconds; long repeatCount = (long)Math.Ceiling(diffSeconds / totalRepeatSeconds); sd = sd.AddSeconds(repeatCount * totalRepeatSeconds); if (sd > windowBegin && sd <= windowEnd) { scheduleDate = sd; can = true; } } else if (sd > windowBegin && sd <= windowEnd) { scheduleDate = sd; can = true; } if (can) { using (IRepository repository = this.repositoryFactory.Create()) { can = !repository.GetScheduleDateExistsForSchedule(schedule.Id.Value, scheduleDate.Value, null); } if (can) { log(); } else { scheduleDate = null; } } } return(can); }
public void DashboardHandlerScheduleSave() { var record = new ScheduleRecord() { ApplicationName = BlueCollarSection.Section.ApplicationName, Id = 0, Name = "Nightly", QueueName = "schedules", RepeatType = ScheduleRepeatType.Days, RepeatValue = 1, StartOn = new DateTime(2011, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; var transaction = new Mock<IDbTransaction>(); var repository = new Mock<IRepository>(); repository.Setup(r => r.BeginTransaction()).Returns(transaction.Object); repository.Setup(r => r.UpdateSchedule(It.IsAny<ScheduleRecord>(), It.IsAny<IDbTransaction>())).Returns(record); repository .Setup(r => r.CreateSchedule(It.IsAny<ScheduleRecord>(), It.IsAny<IDbTransaction>())) .Returns(record) .Callback(() => { record.Id = new Random().Next(1, 1000); }); var factory = new Mock<IRepositoryFactory>(); factory.Setup(f => f.Create()).Returns(repository.Object); using (SaveScheduleHandler handler = new SaveScheduleHandler(factory.Object)) { handler.ApplicationName = BlueCollarSection.Section.ApplicationName; handler.HandlerRelativeRequestUrl = "~/schedules"; handler.QueryString = new QueryString(); string output; using (MemoryStream inputStream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(record)))) { using (MemoryStream outputStream = new MemoryStream()) { var context = MockHttpContext("POST", "/schedules", inputStream, outputStream); handler.ProcessRequest(context.Object); outputStream.Position = 0; output = Encoding.UTF8.GetString(outputStream.ToArray()); } } Assert.IsNotNull(output); Assert.IsTrue(output.Length > 0); var outputRecord = JsonConvert.DeserializeAnonymousType(output, new { Id = 0 }); Assert.IsNotNull(outputRecord); Assert.AreEqual(record.Id, outputRecord.Id); using (MemoryStream inputStream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(record)))) { using (MemoryStream outputStream = new MemoryStream()) { var context = MockHttpContext("PUT", "/schedules", inputStream, outputStream); handler.ProcessRequest(context.Object); outputStream.Position = 0; output = Encoding.UTF8.GetString(outputStream.ToArray()); } } Assert.IsNotNull(output); Assert.IsTrue(output.Length > 0); outputRecord = JsonConvert.DeserializeAnonymousType(output, new { Id = 0 }); Assert.IsNotNull(outputRecord); Assert.AreEqual(record.Id, outputRecord.Id); } }
public bool CanScheduleBeEnqueued(ScheduleRecord schedule, DateTime windowBegin, DateTime windowEnd, out DateTime? scheduleDate) { if (schedule == null) { throw new ArgumentNullException("schedule", "schedule cannot be null."); } bool can = false; scheduleDate = null; windowBegin = windowBegin.FloorWithSeconds(); windowEnd = windowEnd.FloorWithSeconds(); DateTime sd = schedule.StartOn.FloorWithSeconds(); DateTime ed = (schedule.EndOn ?? DateTime.MaxValue).FloorWithSeconds(); if (ed > windowEnd) { int repeatSeconds; switch (schedule.RepeatType) { case ScheduleRepeatType.None: repeatSeconds = 0; break; case ScheduleRepeatType.Seconds: repeatSeconds = 1; break; case ScheduleRepeatType.Minutes: repeatSeconds = 60; break; case ScheduleRepeatType.Hours: repeatSeconds = 3600; break; case ScheduleRepeatType.Days: repeatSeconds = 86400; break; case ScheduleRepeatType.Weeks: repeatSeconds = 604800; break; default: throw new NotImplementedException(); } Action log = () => { if (this.logger != null) { this.logger.Debug( "Schedule can be enqueued with start date of {0:yyyy-MM-dd HH:mm:ss}, repeat type of {1} and repeat value of {2}. Window: {3:yyyy-MM-dd HH:mm:ss} - {4:yyyy-MM-dd HH:mm:ss}; calculated schedule date: {5:yyyy-MM-dd HH:mm:ss}", schedule.StartOn, schedule.RepeatType, schedule.RepeatValue ?? 0, windowBegin, windowEnd, sd); } }; if (sd < windowBegin && schedule.RepeatValue != null && schedule.RepeatValue > 0) { long totalRepeatSeconds = repeatSeconds * schedule.RepeatValue.Value; double diffSeconds = windowBegin.Subtract(sd).TotalSeconds; long repeatCount = (long)Math.Ceiling(diffSeconds / totalRepeatSeconds); sd = sd.AddSeconds(repeatCount * totalRepeatSeconds); if (sd > windowBegin && sd <= windowEnd) { scheduleDate = sd; can = true; } } else if (sd > windowBegin && sd <= windowEnd) { scheduleDate = sd; can = true; } if (can) { using (IRepository repository = this.repositoryFactory.Create()) { can = !repository.GetScheduleDateExistsForSchedule(schedule.Id.Value, scheduleDate.Value, null); } if (can) { log(); } else { scheduleDate = null; } } } return can; }