Skip to content

Commit

Permalink
Showing 9 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -202,7 +202,7 @@ public async Task Handle_ConversionProjectsExist_ErrorResponse_ReturnsCommandSuc
Assert.IsType<CommandSuccessResult>(result);

// Verifying that the logger was called for the "Success sending conversion" case
_mockLogger.Verify(x => x.Log(LogLevel.Information,
_mockLogger.Verify(x => x.Log(LogLevel.Error,
// We're checking for an Information log
It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("Error sending conversion project to complete with project urn")),
// Check if the message contains the expected string
Original file line number Diff line number Diff line change
@@ -204,7 +204,7 @@ public async Task Handle_ConversionProjectsExist_ErrorResponse_ReturnsCommandSuc
Assert.IsType<CommandSuccessResult>(result);

// Verifying that the logger was called for the "Success sending conversion" case
_mockLogger.Verify(x => x.Log(LogLevel.Information,
_mockLogger.Verify(x => x.Log(LogLevel.Error,
// We're checking for an Information log
It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("Error sending conversion project to complete with project urn")),
// Check if the message contains the expected string
Original file line number Diff line number Diff line change
@@ -265,7 +265,7 @@ public async Task Handle_ConversionProjectsExist_ErrorResponse_ReturnsCommandSuc
Assert.IsType<CommandSuccessResult>(result);

// Verifying that the logger was called for the "Success sending conversion" case
_mockLogger.Verify(x => x.Log(LogLevel.Information,
_mockLogger.Verify(x => x.Log(LogLevel.Error,
// We're checking for an Information log
It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("Error sending transfer project to complete with project urn")),
// Check if the message contains the expected string
Original file line number Diff line number Diff line change
@@ -261,7 +261,7 @@ public async Task Handle_ConversionProjectsExist_ErrorResponse_ReturnsCommandSuc
Assert.IsType<CommandSuccessResult>(result);

// Verifying that the logger was called for the "Success sending conversion" case
_mockLogger.Verify(x => x.Log(LogLevel.Information,
_mockLogger.Verify(x => x.Log(LogLevel.Error,
// We're checking for an Information log
It.IsAny<EventId>(), It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("Error sending transfer project to complete with project urn")),
// Check if the message contains the expected string
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ public async Task<CommandResult> Handle(CreateCompleteConversionProjectsCommand
{
var errorResponse = await response.Content.ReadFromJsonAsync<CreateCompleteProjectErrorResponse>();
responseMessage = errorResponse.GetAllErrors();
_logger.LogInformation("Error sending conversion project to complete with project urn: {project} due to Status code {code} and Complete Validation Errors:" + responseMessage, completeObject.urn, response.StatusCode);
_logger.LogError("Error sending conversion project to complete with project urn: {project} due to Status code {code} and Complete Validation Errors:" + responseMessage, completeObject.urn, response.StatusCode);
}

conversionProject.SetProjectSentToComplete();
Original file line number Diff line number Diff line change
@@ -95,7 +95,7 @@ public async Task<CommandResult> Handle(CreateCompleteFormAMatConversionProjects
{
var errorResponse = await response.Content.ReadFromJsonAsync<CreateCompleteProjectErrorResponse>();
responseMessage = errorResponse.GetAllErrors();
_logger.LogInformation("Error sending conversion project to complete with project urn: {project} due to Status code {code} and Complete Validation Errors:" + responseMessage, completeObject.urn, response.StatusCode);
_logger.LogError("Error sending conversion project to complete with project urn: {project} due to Status code {code} and Complete Validation Errors:" + responseMessage, completeObject.urn, response.StatusCode);
}

conversionProject.SetProjectSentToComplete();
Original file line number Diff line number Diff line change
@@ -108,7 +108,7 @@ public async Task<CommandResult> Handle(CreateCompleteFormAMatTransferProjectsCo
var errorResponse =
await response.Content.ReadFromJsonAsync<CreateCompleteProjectErrorResponse>();
responseMessage = errorResponse.GetAllErrors();
_logger.LogInformation("Error sending transfer project to complete with project urn: {project} for transfering academy: {urn} due to Status code {code} and Complete Validation Errors:" + responseMessage, transferProject.Urn, establishment.Urn, response.StatusCode);
_logger.LogError("Error sending transfer project to complete with project urn: {project} for transfering academy: {urn} due to Status code {code} and Complete Validation Errors:" + responseMessage, transferProject.Urn, establishment.Urn, response.StatusCode);
}

transferProject.SetProjectSentToComplete(transferringAcademy.Ukprn);
Original file line number Diff line number Diff line change
@@ -116,7 +116,7 @@ public async Task<CommandResult> Handle(CreateCompleteTransferProjectsCommand re
var errorResponse =
await response.Content.ReadFromJsonAsync<CreateCompleteProjectErrorResponse>();
responseMessage = errorResponse.GetAllErrors();
_logger.LogInformation("Error sending transfer project to complete with project urn: {project} for transfering academy: {urn} due to Status code {code} and Complete Validation Errors:" + responseMessage, transferProject.Urn, establishment.Urn, response.StatusCode);
_logger.LogError("Error sending transfer project to complete with project urn: {project} for transfering academy: {urn} due to Status code {code} and Complete Validation Errors:" + responseMessage, transferProject.Urn, establishment.Urn, response.StatusCode);
}

transferProject.SetProjectSentToComplete(transferringAcademy.Ukprn);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Dfe.Academies.Academisation.Service.Commands.CompleteProject;
using Dfe.Academies.Academisation.Service.Commands.FormAMat;
using System.Security.Cryptography;
using Dfe.Academies.Academisation.Service.Commands.CompleteProject;
using Dfe.Academisation.CorrelationIdMiddleware;
using MediatR;

@@ -16,7 +16,7 @@ public CreateCompleteProjectsService(ILogger<CreateCompleteProjectsService> logg
{
_logger = logger;
_factory = factory;
_delayInMilliseconds = config.GetValue<int?>("DatabasePollingDelay") ?? 10_000;
_delayInMilliseconds = GetSecureRandomDelay(1000, 30000) + config.GetValue<int?>("SendProjectsToCompletePollingDelay") ?? 60_000;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@@ -45,24 +45,27 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
catch (Exception ex)
{
_logger.LogError("Error enriching conversions complete project", ex);
_logger.LogError("Error creating complete projects", ex);
}

await Task.Delay(_delayInMilliseconds, stoppingToken);

try
{
await mediator.Send(new CreateCompleteTransferProjectsCommand(), stoppingToken);
}
catch (Exception ex)
{
_logger.LogError("Error enriching transfers complete project", ex);
}

await Task.Delay(_delayInMilliseconds, stoppingToken);
await Task.Delay(_delayInMilliseconds, stoppingToken);
}
}
}
}

private int GetSecureRandomDelay(int minValue, int maxValue)
{
// Use RandomNumberGenerator to securely generate a random delay in the specified range
byte[] randomNumber = new byte[4];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(randomNumber);
}
// Convert bytes to a positive integer
int randomValue = Math.Abs(BitConverter.ToInt32(randomNumber, 0));
// Scale the random value to the specified range
return (randomValue % (maxValue - minValue + 1)) + minValue;
}
}
}

0 comments on commit 4a237e2

Please sign in to comment.