Skip to content

Commit

Permalink
Merge pull request #604 from DFE-Digital/feature/random-delay-sending…
Browse files Browse the repository at this point in the history
…-complete-projects

hosted service delay and lo information changed to log error
  • Loading branch information
paullocknimble authored Nov 6, 2024
2 parents c05820d + 239db55 commit 4a7d874
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
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;

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

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// delay start to prevent runtime colisions
var startDelayInSeconds = GetSecureRandomDelay(1, 60);

await Task.Delay(startDelayInSeconds * 1000, stoppingToken);

while (!stoppingToken.IsCancellationRequested)
{
using (var scope = _factory.CreateScope())
Expand All @@ -45,24 +50,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 4a7d874

Please sign in to comment.