public async Task GetJobByPriority_Ilr_submission() { IContainer container = Registrations(); using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); } var manager = container.Resolve <IJobManager>(); await manager.AddJob(new Job { Priority = 1, Status = JobStatusType.Ready }); await manager.AddJob(new Job { Priority = 2, Status = JobStatusType.Ready }); IEnumerable <Job> result = (await manager.GetJobsByPriorityAsync(100)).ToList(); result.Should().NotBeEmpty(); Job job = result.First(); job.JobId.Should().Be(2); job.JobType.Should().Be(JobType.IlrSubmission); } }
public async Task IsCrossLoadingEnabled_Success() { IContainer container = Registrations(); using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); context.JobTypeGroup.Add(new JobTypeGroup { JobTypeGroupId = 1, Description = "Collection Submission", ConcurrentExecutionCount = 25 }); context.JobType.Add(new Data.Entities.JobType { IsCrossLoadingEnabled = true, Title = "Title", Description = "Description", JobTypeId = 1, JobTypeGroupId = 1 }); context.SaveChanges(); } var manager = scope.Resolve <IJobManager>(); (await manager.IsCrossLoadingEnabled(JobType.IlrSubmission)).Should().BeTrue(); } }
public async Task GetTopicTasks_OneTaskTopic_Success() { IContainer container = Registrations(); List <ITopicItem> result; using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); context.JobTopicSubscription.Add(GetJobTopic(1, JobType.IlrSubmission, "TopicA", "Validation", "GenerateReport")); context.SaveChanges(); } var jobTopicTaskService = scope.Resolve <IJobTopicTaskService>(); result = (await jobTopicTaskService.GetTopicItems(JobType.IlrSubmission, false)).ToList(); } result.Should().NotBeNull(); result.Count.Should().Be(1); var topicItem = result.First(); topicItem.SubscriptionSqlFilterValue.Should().Be("TopicA"); topicItem.SubscriptionName.Should().Be("Validation"); topicItem.Tasks.Count.Should().Be(1); topicItem.Tasks.Any(x => x.Tasks.Contains("GenerateReport")).Should().BeTrue(); }
public async Task GetTopicTasks_Task_NotEnabled_Success(JobType jobType) { IContainer container = Registrations(); List <ITopicItem> result; using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); context.JobTopicSubscription.Add(GetJobTopic(1, jobType, "TopicA", "Validation", "GenerateReport")); context.JobTopicSubscription.Add(GetJobTopic(2, jobType, "TopicA", "Funding", "NotEnabledTask", false, true, false)); context.SaveChanges(); var jobTopicTaskService = scope.Resolve <IJobTopicTaskService>(); result = (await jobTopicTaskService.GetTopicItems(jobType, false)).ToList(); } result.Should().NotBeNull(); result.Count.Should().Be(2); result.Single(x => x.SubscriptionName == "Validation").Tasks.Count.Should().Be(1); result.Single(x => x.SubscriptionName == "Funding").Tasks.Any(x => x.Tasks.Contains("NotEnabledTask")).Should().BeFalse(); } }
public async Task TestRemove() { DateTime utcNow = DateTime.UtcNow; Mock <IDateTimeProvider> dateTimeProvider = new Mock <IDateTimeProvider>(); Mock <IScheduleService> scheduleService = new Mock <IScheduleService>(); Mock <ILogger> logger = new Mock <ILogger>(); dateTimeProvider.Setup(x => x.GetNowUtc()).Returns(utcNow); scheduleService .Setup(x => x.CanExecuteSchedule(It.IsAny <Schedule>(), It.IsAny <DateTime>(), It.IsAny <bool>())) .Returns(true); using (var connection = new SqliteConnection("DataSource=:memory:")) { connection.Open(); var options = new DbContextOptionsBuilder <JobQueueDataContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); context.JobTypeGroup.Add(new JobTypeGroup { JobTypeGroupId = 3, Description = "Reference Data", ConcurrentExecutionCount = 25 }); context.JobType.Add(new JobType { JobTypeId = 40, JobTypeGroupId = 3, Description = "ReferenceData EPA", Title = "ReferenceData EPA", IsCrossLoadingEnabled = false }); context.Schedule.Add(new Schedule { Enabled = true, ExecuteOnceOnly = true, JobTypeId = 40 }); await context.SaveChangesAsync(); ExternalDataScheduleService externalDataScheduleService = new ExternalDataScheduleService( options, scheduleService.Object, dateTimeProvider.Object, logger.Object); IEnumerable <Jobs.Model.Enums.JobType> results = await externalDataScheduleService.GetJobs(true, CancellationToken.None); results.Should().HaveCount(1); var schedules = await context.Schedule.ToListAsync(); schedules.Should().BeEmpty(); } } }
public async Task GetJobMetaDataById_Success() { IContainer container = Registrations(); FileUploadJob result; using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); } IFileUploadJobManager manager = scope.Resolve <IFileUploadJobManager>(); var jobId = await manager.AddJob(new FileUploadJob() { JobId = 1, }); result = await manager.GetJobById(1); } result.Should().NotBeNull(); result.JobId.Should().Be(1); }
public async Task GetLatestJobByUkprn_Success() { IContainer container = Registrations(); IEnumerable <FileUploadJob> items; using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); } IFileUploadJobManager manager = scope.Resolve <IFileUploadJobManager>(); await manager.AddJob(new FileUploadJob() { JobId = 1, Ukprn = 10000116, PeriodNumber = 1, FileName = "ilr.xml", CollectionName = "ILR1819" }); await manager.AddJob(new FileUploadJob() { JobId = 2, Ukprn = 10000116, PeriodNumber = 2, FileName = "10000116/SUPPDATA-10000116-ESF-99999-20181109-090919.csv", CollectionName = "ESF" }); await manager.AddJob(new FileUploadJob() { JobId = 3, Ukprn = 10000116, PeriodNumber = 2, FileName = "eas.csv", CollectionName = "EAS" }); await manager.AddJob(new FileUploadJob() { JobId = 4, Ukprn = 10000119, PeriodNumber = 2, FileName = "eas11.csv", CollectionName = "EAS" }); items = (await manager.GetLatestJobByUkprn(new long[] { 10000116, 10000119 })).ToList(); } items.Should().NotBeEmpty(); items.Single(x => x.FileName.Equals("ilr.xml") && x.Ukprn == 10000116 && x.JobId == 1).Should().NotBeNull(); items.Single(x => x.FileName.Equals("eas11.csv") && x.Ukprn == 10000119 && x.JobId == 4).Should().NotBeNull(); }
public async Task GetJobsByUkprnForDateRange_Success() { IContainer container = Registrations(); List <FileUploadJob> result; using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); } IFileUploadJobManager manager = scope.Resolve <IFileUploadJobManager>(); await manager.AddJob(new FileUploadJob() { JobId = 1, Ukprn = 100 }); await manager.AddJob(new FileUploadJob() { JobId = 2, Ukprn = 100 }); result = (await manager.GetJobsByUkprnForDateRange(100, DateTime.UtcNow.AddMinutes(-1), DateTime.UtcNow)).ToList(); } result.Should().NotBeNull(); result.Count.Should().Be(2); }
private void SetupData(DbContextOptions dbContextOptions) { using (var cmContext = new JobQueueDataContext(dbContextOptions)) { var collection = new CollectionEntity() { CollectionId = 1, CollectionTypeId = 1, IsOpen = true, Name = "ILR1718" }; cmContext.Collections.Add(collection); cmContext.ReturnPeriods.Add(new ReturnPeriodEntity() { CalendarMonth = 8, CalendarYear = 2018, ReturnPeriodId = 2, PeriodNumber = 1, StartDateTimeUtc = new System.DateTime(2018, 08, 22), EndDateTimeUtc = new System.DateTime(2018, 09, 04), CollectionEntity = collection, CollectionId = 1, }); cmContext.ReturnPeriods.Add(new ReturnPeriodEntity() { CalendarMonth = 7, CalendarYear = 2018, ReturnPeriodId = 1, PeriodNumber = 12, StartDateTimeUtc = System.DateTime.UtcNow.AddSeconds(-60), EndDateTimeUtc = System.DateTime.UtcNow.AddSeconds(60), CollectionEntity = collection, CollectionId = 1 }); cmContext.ReturnPeriods.Add(new ReturnPeriodEntity() { CalendarMonth = 9, CalendarYear = 2018, ReturnPeriodId = 3, PeriodNumber = 3, StartDateTimeUtc = System.DateTime.UtcNow.AddDays(1), EndDateTimeUtc = System.DateTime.UtcNow.AddDays(10), CollectionEntity = collection, CollectionId = 1 }); cmContext.SaveChanges(); } }
public async Task GetLatestJobByUkprnAndContractReference_Success() { IContainer container = Registrations(); FileUploadJob result; using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); } IFileUploadJobManager manager = scope.Resolve <IFileUploadJobManager>(); await manager.AddJob(new FileUploadJob() { JobId = 1, Ukprn = 10000116, PeriodNumber = 1, FileName = "10000116/SUPPDATA-10000116-ESF-2270-20181109-090919.csv", CollectionName = "ESF" }); await manager.AddJob(new FileUploadJob() { JobId = 2, Ukprn = 10000116, PeriodNumber = 2, FileName = "10000116/SUPPDATA-10000116-ESF-99999-20181109-090919.csv", CollectionName = "ESF" }); await manager.AddJob(new FileUploadJob() { JobId = 3, Ukprn = 10000119, PeriodNumber = 2, FileName = "10000119/SUPPDATA-10000119-ESF-2270-20181109-090919.csv", CollectionName = "ESF" }); result = await manager.GetLatestJobByUkprnAndContractReference(10000116, "ESF-2270", "ESF"); } result.Should().NotBeNull(); result.FileName.Should().Be("10000116/SUPPDATA-10000116-ESF-2270-20181109-090919.csv"); result.Ukprn.Should().Be(10000116); result.JobId.Should().Be(1); }
public async Task UpdateCrossLoadingStatus_Success_EmailSent() { var emailTemplateManager = new Mock <IEmailTemplateManager>(); emailTemplateManager.Setup(x => x.GetTemplate(It.IsAny <long>(), It.IsAny <JobStatusType>(), It.IsAny <JobType>(), It.IsAny <DateTime>())).Returns(Task.FromResult("template")); var emailNotifier = new Mock <IEmailNotifier>(); emailNotifier.Setup(x => x.SendEmail(It.IsAny <string>(), "test", It.IsAny <Dictionary <string, dynamic> >())); IContainer container = Registrations(emailTemplateManager.Object, emailNotifier.Object); using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); context.JobTypeGroup.Add(new JobTypeGroup { JobTypeGroupId = 1, Description = "Collection Submission", ConcurrentExecutionCount = 25 }); context.JobType.Add(new Data.Entities.JobType { IsCrossLoadingEnabled = true, Title = "Title", Description = "Description", JobTypeId = 1, JobTypeGroupId = 1 }); context.SaveChanges(); } var manager = scope.Resolve <IJobManager>(); await manager.AddJob(new Job { Status = JobStatusType.Ready, JobType = JobType.IlrSubmission, CrossLoadingStatus = JobStatusType.MovedForProcessing }); await manager.UpdateCrossLoadingStatus(1, JobStatusType.Completed); var updatedJob = await manager.GetJobById(1); updatedJob.CrossLoadingStatus.Should().Be(JobStatusType.Completed); emailNotifier.Verify(x => x.SendEmail(It.IsAny <string>(), "template", It.IsAny <Dictionary <string, dynamic> >()), Times.Never); } }
public void GetTemplate_Success(bool isClose) { var returnCalendarMock = new Mock <IReturnCalendarService>(); returnCalendarMock.Setup(x => x.GetPeriodAsync("ILR1819", It.IsAny <DateTime>())).ReturnsAsync(() => isClose ? null : new ReturnPeriod()); IContainer container = Registrations(returnCalendarMock.Object); using (var scope = container.BeginLifetimeScope()) { var templateManager = scope.Resolve <IEmailTemplateManager>(); var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); // Create the schema in the database using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); context.JobEmailTemplate.Add(new JobEmailTemplate() { JobType = (short)JobType.IlrSubmission, JobStatus = (short)JobStatusType.Completed, Active = true, TemplateOpenPeriod = "template_open", TemplateClosePeriod = "template_close" }); context.FileUploadJobMetaData.Add(new FileUploadJobMetaData() { Job = new Job() { JobId = 1, JobType = (short)JobType.IlrSubmission }, CollectionName = "ILR1819", PeriodNumber = 1, FileName = "test", FileSize = 100, IsFirstStage = false, StorageReference = "test" }); context.SaveChanges(); } var template = templateManager.GetTemplate(1, JobStatusType.Completed, JobType.IlrSubmission, DateTime.Now).Result; template.Should().NotBeNull(); template.Should().Be(isClose ? "template_close" : "template_open"); } }
public async Task AddJobMetaData_Success_Values() { IContainer container = Registrations(); FileUploadJob savedJob; using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); } FileUploadJob job = new FileUploadJob() { JobId = 1, Ukprn = 1000, FileName = "test.xml", StorageReference = "test-ref", FileSize = 10.5m, IsFirstStage = true, CollectionName = "ILR1718", PeriodNumber = 10, TermsAccepted = true, CollectionYear = 1819 }; IFileUploadJobManager manager = scope.Resolve <IFileUploadJobManager>(); await manager.AddJob(job); savedJob = await manager.GetJobById(1); } savedJob.Should().NotBeNull(); savedJob.JobId.Should().Be(1); savedJob.Ukprn.Should().Be(1000); savedJob.FileName.Should().Be("test.xml"); savedJob.FileSize.Should().Be(10.5m); savedJob.StorageReference.Should().Be("test-ref"); savedJob.IsFirstStage.Should().Be(true); savedJob.CollectionName.Should().Be("ILR1718"); savedJob.PeriodNumber.Should().Be(10); savedJob.TermsAccepted.Should().Be(true); savedJob.CollectionYear.Should().Be(1819); }
public async Task UpdateJob_Fail_InvalidJobId() { IContainer container = Registrations(); using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); } IFileUploadJobManager manager = scope.Resolve <IFileUploadJobManager>(); await Assert.ThrowsAsync <ArgumentException>(() => manager.UpdateJobStage(100, false)); } }
private void SetupData(DbContextOptions dbContextOptions) { using (var cmContext = new JobQueueDataContext(dbContextOptions)) { cmContext.Organisations.Add(new OrganisationEntity() { Ukprn = 1000, OrgId = "test_org1", OrganisationId = 1 }); cmContext.Collections.Add(new CollectionEntity() { CollectionId = 1, CollectionTypeId = 1, IsOpen = true, Name = "test coll" }); cmContext.Collections.Add(new CollectionEntity() { CollectionId = 2, CollectionTypeId = 1, IsOpen = true, Name = "test coll2" }); cmContext.CollectionTypes.Add(new CollectionTypeEntity() { CollectionTypeId = 1, Description = "ILR collection", Type = "ILR" }); cmContext.OrganisationCollections.Add(new OrganisationCollectionEntity() { CollectionId = 1, OrganisationId = 1 }); cmContext.SaveChanges(); } }
public IEnumerable <Job> GetStuckJobs() { DateTime utcNow = _dateTimeProvider.GetNowUtc(); List <Job> jobs = new List <Job>(); using (var context = new JobQueueDataContext(_contextOptions)) { IEnumerable <JobEntity> jobEntities = context.Jobs .Where(x => x.CrossLoadingStatus != null && x.CrossLoadingStatus.Value == _crossLoadingStatus) .AsEnumerable() .Where(x => x.DateTimeUpdatedUtc != null && x.DateTimeUpdatedUtc.Value.AddMinutes(_numberOfMinutesBeforeFail) < utcNow); foreach (JobEntity jobEntity in jobEntities) { jobs.Add(JobConverter.Convert(jobEntity)); } } return(jobs); }
public async Task GetJobByPriority_Ilr_NoJobs() { IContainer container = Registrations(); IEnumerable <Job> result; using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); } var manager = container.Resolve <IJobManager>(); result = await manager.GetJobsByPriorityAsync(100); } result.Should().BeEmpty(); }
public async Task TestRemove(int age, int updateTimeBack, JobStatusType status, int expected) { DateTime utcNow = DateTime.UtcNow; Mock <IDateTimeProvider> dateTimeProvider = new Mock <IDateTimeProvider>(); dateTimeProvider.Setup(x => x.GetNowUtc()).Returns(utcNow); using (var connection = new SqliteConnection("DataSource=:memory:")) { connection.Open(); var options = new DbContextOptionsBuilder <JobQueueDataContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); context.Jobs.Add(new JobEntity { JobId = 1, DateTimeUpdatedUtc = utcNow.AddMinutes(updateTimeBack), CrossLoadingStatus = (short)status, DateTimeSubmittedUtc = utcNow.AddMinutes(updateTimeBack + -1), JobType = (short)JobType.IlrSubmission, NotifyEmail = "*****@*****.**", Priority = 1, Status = (short)JobStatusType.Completed, RowVersion = Encoding.UTF8.GetBytes("RowVersion"), SubmittedBy = "Tester" }); await context.SaveChangesAsync(); ICrossLoadActiveJobService crossLoadActiveJobService = new CrossLoadActiveJobService(options, dateTimeProvider.Object, age); var results = crossLoadActiveJobService.GetStuckJobs(); results.Should().HaveCount(expected); } } }
public async Task <IEnumerable <Jobs.Model.Enums.JobType> > GetJobs(bool removeOldDates, CancellationToken cancellationToken) { DateTime nowUtc = _dateTimeProvider.GetNowUtc().TrimSeconds(); HashSet <int> jobs = new HashSet <int>(); try { using (JobQueueDataContext db = new JobQueueDataContext(_contextOptions)) { IQueryable <Schedule> nonRun = db.Schedule.Where(x => x.Enabled); foreach (Schedule schedule in nonRun) { if (!_scheduleService.CanExecuteSchedule(schedule, nowUtc, removeOldDates)) { continue; } jobs.Add(schedule.JobTypeId); if (schedule.ExecuteOnceOnly) { db.Schedule.Remove(schedule); } else { schedule.LastExecuteDateTime = nowUtc; } } await db.SaveChangesAsync(cancellationToken); } } catch (Exception ex) { _logger.LogError("Failed to retrieve and process external data schedules", ex); } return(jobs.Select(x => (Jobs.Model.Enums.JobType)x)); }
public TasksController(JobQueueDataContext context, ILogger logger, TelemetryClient telemetryClient) : base(logger, telemetryClient) { _context = context; }
public async Task GetLatestJobsPerPeriodByUkprn_Success() { IContainer container = Registrations(); List <FileUploadJob> result; using (var scope = container.BeginLifetimeScope()) { // Create the schema in the database var options = scope.Resolve <DbContextOptions <JobQueueDataContext> >(); using (var context = new JobQueueDataContext(options)) { context.Database.EnsureCreated(); } IFileUploadJobManager manager = scope.Resolve <IFileUploadJobManager>(); await manager.AddJob(new FileUploadJob() { JobId = 1, JobType = JobType.EsfSubmission, Ukprn = 10000116, FileName = "esf.csv", CollectionName = "ESF", CollectionYear = 1819, Status = JobStatusType.Completed, PeriodNumber = 1, DateTimeSubmittedUtc = DateTime.Now.AddMinutes(-10), }); await manager.AddJob(new FileUploadJob() { JobId = 2, JobType = JobType.EasSubmission, Ukprn = 10000116, FileName = "eas.csv", CollectionName = "EAS", CollectionYear = 1819, PeriodNumber = 1, Status = JobStatusType.Completed, DateTimeSubmittedUtc = DateTime.Now.AddMinutes(-10), }); await manager.AddJob(new FileUploadJob() { JobId = 3, Ukprn = 10000116, FileName = "ilr1819.xml", CollectionName = "ILR1819", CollectionYear = 1819, PeriodNumber = 1, JobType = JobType.IlrSubmission, Status = JobStatusType.Completed, }); await manager.AddJob(new FileUploadJob() { JobId = 4, Ukprn = 10000116, FileName = "ilr1718.xml", CollectionName = "ILR1718", CollectionYear = 1718, PeriodNumber = 1, JobType = JobType.IlrSubmission, Status = JobStatusType.Completed, }); await manager.AddJob(new FileUploadJob() { JobId = 5, Ukprn = 10000116, FileName = "ilr_latest_not_completed.xml", CollectionName = "ILR1819", CollectionYear = 1819, PeriodNumber = 1, JobType = JobType.IlrSubmission, Status = JobStatusType.Failed, DateTimeSubmittedUtc = DateTime.Now.AddMinutes(-50), }); result = (await manager.GetLatestJobsPerPeriodByUkprn(10000116, DateTime.Now.AddDays(-1), DateTime.Now)) .ToList(); } result.Should().NotBeNull(); result.Count.Should().Be(4); result.Single(x => x.JobType == JobType.EsfSubmission && x.JobId == 1 && x.FileName == "esf.csv").Should().NotBeNull(); result.Single(x => x.JobType == JobType.EasSubmission && x.JobId == 2 && x.FileName == "eas.csv").Should().NotBeNull(); result.Single(x => x.JobType == JobType.IlrSubmission && x.JobId == 3 && x.FileName == "ilr1819.xml" && x.CollectionYear == 1819).Should().NotBeNull(); result.Single(x => x.JobType == JobType.IlrSubmission && x.JobId == 4 && x.FileName == "ilr1718.xml" && x.CollectionYear == 1718).Should().NotBeNull(); }
public CollectionService(DbContextOptions <JobQueueDataContext> dbContextOptions) { _context = new JobQueueDataContext(dbContextOptions); }