public IEnumerable <ReportJob> RunReports(ConnectionSetting connection) { List <ReportJob> executedJobs = new List <ReportJob>(); _logger.Debug("Retrieving unprocessed reports for connection {0}", connection.Name); // get the number of reports that are currently procesing for this connection List <ReportJob> executingReports = _reportJobRepository.GetProcessingReports(connection.ConnectionString).ToList(); if (executingReports.Count >= _appSettings.MaxConcurrentReports) { _logger.Info("Maximum number of concurrent reports ({0}) are already being run for connection '{1}' exiting", _appSettings.MaxConcurrentReports, connection.Name); return(Enumerable.Empty <ReportJob>()); } // extract the list of SingleExecutionGroups that are running List <string> singleExecutionGroups = executingReports.Where(x => !String.IsNullOrWhiteSpace(x.SingleExecutionGroup)).Select(x => x.SingleExecutionGroup).Distinct().ToList(); _logger.Info("SingleExecutionGroup list to avoid [{0}]", String.Join(",", singleExecutionGroups)); int reportsToRun = _appSettings.MaxConcurrentReports - executingReports.Count; _logger.Info("{0} reports executing for connection '{1}', checking queue for additional reports", executingReports.Count, connection.Name); IEnumerable <ReportJob> jobs = _reportJobRepository.GetPendingReports(connection.ConnectionString, reportsToRun, singleExecutionGroups); foreach (ReportJob job in jobs) { // if the job has a SingleExecutionGroup, we need to add it to the list so we are certain we don't run more // than one of these - we also need to check it hasn't already been added by a previous job retrieved in // the same call if (!String.IsNullOrWhiteSpace(job.SingleExecutionGroup)) { if (singleExecutionGroups.Contains(job.SingleExecutionGroup)) { _logger.Info("Not running job '{0}' as a job with the same SingleExecutionGroup ('{1}') is already running", job.Id, job.SingleExecutionGroup); continue; } else { singleExecutionGroups.Add(job.SingleExecutionGroup); _logger.Info("Added SingleExecutionGroup '{0}' to list of groups to skip", job.SingleExecutionGroup); } } _jobAgent.ExecuteJobAsync(connection, job); executedJobs.Add(job); } return(executedJobs); }
public void RunReports_MaxConcurrentReportsExceeded_ExitsAndReturnsEmptyCollection(int runningReportCount) { // setup string connName = Guid.NewGuid().ToString(); ConnectionSetting conn = new ConnectionSetting(connName, "connection"); _appSettings.MaxConcurrentReports.Returns(5); List <ReportJob> executingJobs = CreateReportJobList(runningReportCount); _reportJobRepository.GetProcessingReports(conn.ConnectionString).Returns(executingJobs); // execute var result = _reportCoordinator.RunReports(conn); // assert Assert.AreEqual(0, result.Count()); _reportJobRepository.Received(1).GetProcessingReports(conn.ConnectionString); // no call should have been made for more reports _reportJobRepository.DidNotReceive().GetPendingReports(Arg.Any <string>(), Arg.Any <int>(), Arg.Any <IEnumerable <string> >()); }