public async Task GetNonCompletedJobsWithinTimeFrame_Called_ReturnsJobSummaries() { string jobId = "5678"; DateTimeOffset from = DateTimeOffset.UtcNow.AddDays(-2); DateTimeOffset to = DateTimeOffset.UtcNow.AddDays(-1); IJobsApiClient jobsApiClient = Substitute.For <IJobsApiClient>(); JobManagementResiliencePolicies policies = new JobManagementResiliencePolicies { JobsApiClient = Policy.NoOpAsync() }; IMessengerService messengerService = Substitute.For <IMessengerService>(); ILogger logger = Substitute.For <ILogger>(); JobSummary jobSummary = new JobSummary { JobId = jobId }; IEnumerable <JobSummary> jobSummaries = new List <JobSummary> { jobSummary }; ApiResponse <IEnumerable <JobSummary> > jobSummariesApiResponse = new ApiResponse <IEnumerable <JobSummary> >(HttpStatusCode.OK, jobSummaries); jobsApiClient .GetNonCompletedJobsWithinTimeFrame(from, to) .Returns(jobSummariesApiResponse); JobManagement jobManagement = new JobManagement(jobsApiClient, logger, policies, messengerService); //Act IEnumerable <JobSummary> result = await jobManagement.GetNonCompletedJobsWithinTimeFrame(from, to); Assert.AreEqual(result, jobSummaries); await jobsApiClient .Received(1) .GetNonCompletedJobsWithinTimeFrame(from, to); }
public async Task <IEnumerable <JobSummary> > GetNonCompletedJobsWithinTimeFrame(DateTimeOffset dateTimeFrom, DateTimeOffset dateTimeTo) { ApiResponse <IEnumerable <JobSummary> > jobSummaryResponse = await _jobsApiClientPolicy.ExecuteAsync(() => _jobsApiClient.GetNonCompletedJobsWithinTimeFrame(dateTimeFrom, dateTimeTo)); IEnumerable <JobSummary> jobSummaries = jobSummaryResponse?.Content; return(jobSummaries); }
public async Task ScaleDownForJobConfiguration() { DateTimeOffset now = DateTimeOffset.UtcNow; DateTimeOffset hourAgo = now.AddHours(-1); ApiResponse <IEnumerable <JobSummary> > jobSummariesResponse = await _jobsApiClientPolicy.ExecuteAsync( () => _jobsApiClient.GetNonCompletedJobsWithinTimeFrame(hourAgo, now)); if (!jobSummariesResponse.StatusCode.IsSuccess()) { string errorMessage = "Failed to fetch job summaries that are still running within the last hour"; _logger.Error(errorMessage); throw new RetriableException(errorMessage); } IEnumerable <string> jobDefinitionIdsStillActive = jobSummariesResponse.Content?.Select(m => m.JobType).Distinct(); IEnumerable <CosmosDbScalingConfig> cosmosDbScalingConfigs = await GetAllConfigs(); IList <CosmosDbScalingCollectionSettings> settingsToUpdate = new List <CosmosDbScalingCollectionSettings>(); foreach (CosmosDbScalingConfig cosmosDbScalingConfig in cosmosDbScalingConfigs) { CosmosDbScalingCollectionSettings settings = await _scalingConfigRepositoryPolicy.ExecuteAsync(() => _cosmosDbScalingConfigRepository.GetCollectionSettingsByRepositoryType(cosmosDbScalingConfig.RepositoryType)); bool proceed = !settingsToUpdate.Any(m => m.Id == cosmosDbScalingConfig.Id); if (proceed) { if (!settings.IsAtBaseLine) { settingsToUpdate.Add(settings); } } } if (!settingsToUpdate.IsNullOrEmpty()) { foreach (CosmosDbScalingCollectionSettings settings in settingsToUpdate) { try { await ScaleCollection(settings.CosmosCollectionType, settings.MinRequestUnits); int decrementValue = settings.CurrentRequestUnits - settings.MinRequestUnits; settings.CurrentRequestUnits = settings.MinRequestUnits; await UpdateCollectionSettings(settings, CosmosDbScalingDirection.Down, decrementValue); } catch (Exception ex) { throw new RetriableException($"Failed to scale down collection for repository type '{settings.CosmosCollectionType}'", ex); } } await _cacheProviderPolicy.ExecuteAsync(() => _cacheProvider.RemoveAsync <List <CosmosDbScalingConfig> >(CacheKeys.AllCosmosScalingConfigs)); } }