-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #921 from SkillsFundingAgency/CON-954-house-keepin…
…g-communications CON-954 : v2 housekeeping tasks: communications
- Loading branch information
Showing
11 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...cies.Jobs.UnitTests/Triggers/QueueTriggers/CommunicationsHouseKeepingQueueTriggerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Communication.Types; | ||
using Esfa.Recruit.Vacancies.Client.Application.Providers; | ||
using Esfa.Recruit.Vacancies.Client.Application.Queues.Messages; | ||
using Esfa.Recruit.Vacancies.Jobs.Configuration; | ||
using Esfa.Recruit.Vacancies.Jobs.Triggers.QueueTriggers; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
|
||
namespace Recruit.Vacancies.Jobs.UnitTests.Triggers.QueueTriggers | ||
{ | ||
public class CommunicationsHouseKeepingQueueTriggerTests | ||
{ | ||
private const int Days = 180; | ||
private readonly Mock<ILogger<CommunicationsHouseKeepingQueueTrigger>> _loggerMock = new Mock<ILogger<CommunicationsHouseKeepingQueueTrigger>>(); | ||
private readonly RecruitWebJobsSystemConfiguration _jobsConfig = new RecruitWebJobsSystemConfiguration() { HardDeleteCommunicationMessagesStaleByDays = Days }; | ||
private readonly Mock<ITimeProvider> _timeProviderMock = new Mock<ITimeProvider>(); | ||
private readonly Mock<ICommunicationRepository> _communicationRepositoryMock = new Mock<ICommunicationRepository>(); | ||
|
||
[Fact] | ||
public async Task Delete_Communication_Messages_older_than_180_days() | ||
{ | ||
//Arrange | ||
var datetime = new DateTime(2020, 08, 30).AddDays(-180); | ||
_communicationRepositoryMock.Setup(x => x.HardDelete(datetime)).Returns(Task.CompletedTask); | ||
var sut = new CommunicationsHouseKeepingQueueTrigger(_loggerMock.Object, _jobsConfig, _timeProviderMock.Object, _communicationRepositoryMock.Object); | ||
var message = new CommunicationsHouseKeepingQueueMessage() { CreatedByScheduleDate = new DateTime(2020, 08, 30) }; | ||
|
||
//Act | ||
await sut.CommunicationsHouseKeepingAsync(JsonConvert.SerializeObject(message), null); | ||
|
||
//Assert | ||
_communicationRepositoryMock.Verify(c => c.HardDelete(datetime), Times.AtLeastOnce); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...s/Recruit.Vacancies.Jobs/Triggers/QueueTriggers/CommunicationsHouseKeepingQueueTrigger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Communication.Types; | ||
using Esfa.Recruit.Vacancies.Client.Application.Providers; | ||
using Esfa.Recruit.Vacancies.Client.Application.Queues.Messages; | ||
using Esfa.Recruit.Vacancies.Client.Infrastructure.StorageQueue; | ||
using Esfa.Recruit.Vacancies.Jobs.Configuration; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
|
||
namespace Esfa.Recruit.Vacancies.Jobs.Triggers.QueueTriggers | ||
{ | ||
public class CommunicationsHouseKeepingQueueTrigger | ||
{ | ||
private readonly ILogger<CommunicationsHouseKeepingQueueTrigger> _logger; | ||
private readonly RecruitWebJobsSystemConfiguration _jobsConfig; | ||
private readonly ITimeProvider _timeProvider; | ||
private readonly ICommunicationRepository _communicationRepository; | ||
private const int DefaultStaleByDays = 180; | ||
private string JobName => GetType().Name; | ||
|
||
public CommunicationsHouseKeepingQueueTrigger(ILogger<CommunicationsHouseKeepingQueueTrigger> logger, | ||
RecruitWebJobsSystemConfiguration jobsConfig, | ||
ITimeProvider timeProvider, | ||
ICommunicationRepository communicationRepository) | ||
{ | ||
_logger = logger; | ||
_jobsConfig = jobsConfig; | ||
_timeProvider = timeProvider; | ||
_communicationRepository = communicationRepository; | ||
} | ||
|
||
public async Task CommunicationsHouseKeepingAsync([QueueTrigger(QueueNames.CommunicationsHouseKeepingQueueName, Connection = "QueueStorage")] string message, TextWriter log) | ||
{ | ||
try | ||
{ | ||
if (_jobsConfig.DisabledJobs.Contains(JobName)) | ||
{ | ||
_logger.LogDebug($"{JobName} is disabled, skipping ..."); | ||
return; | ||
} | ||
|
||
var payload = JsonConvert.DeserializeObject<CommunicationsHouseKeepingQueueMessage>(message); | ||
|
||
var targetDate = payload?.CreatedByScheduleDate ?? _timeProvider.Today; | ||
|
||
var deleteCommunicationsMessagesBefore180Days = targetDate.AddDays((_jobsConfig.HardDeleteCommunicationMessagesStaleByDays ?? DefaultStaleByDays) * -1); | ||
|
||
await _communicationRepository.HardDelete(deleteCommunicationsMessagesBefore180Days); | ||
|
||
_logger.LogInformation($"Deleted Communication Mesages created before 180 days {deleteCommunicationsMessagesBefore180Days}"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, $"Failed to run {JobName}"); | ||
throw; | ||
} | ||
} | ||
|
||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...s/Recruit.Vacancies.Jobs/Triggers/TimerTriggers/CommunicationsHouseKeepingTimerTrigger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Esfa.Recruit.Vacancies.Client.Application.Providers; | ||
using Esfa.Recruit.Vacancies.Client.Application.Queues; | ||
using Esfa.Recruit.Vacancies.Client.Application.Queues.Messages; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Esfa.Recruit.Vacancies.Jobs.Triggers.TimerTriggers | ||
{ | ||
public class CommunicationsHouseKeepingTimerTrigger | ||
{ | ||
private readonly ILogger<CommunicationsHouseKeepingTimerTrigger> _logger; | ||
private readonly IRecruitQueueService _queue; | ||
private readonly ITimeProvider _timeProvider; | ||
|
||
public CommunicationsHouseKeepingTimerTrigger(ILogger<CommunicationsHouseKeepingTimerTrigger> logger, IRecruitQueueService queue, ITimeProvider timeProvider) | ||
{ | ||
_logger = logger; | ||
_queue = queue; | ||
_timeProvider = timeProvider; | ||
} | ||
|
||
public Task CommunicationsHouseKeepingAsync([TimerTrigger(Schedules.WeeklyTenAmSunday)] TimerInfo timerInfo, TextWriter log) | ||
{ | ||
_logger.LogInformation($"Timer trigger {this.GetType().Name} fired"); | ||
|
||
var message = new CommunicationsHouseKeepingQueueMessage | ||
{ | ||
CreatedByScheduleDate = _timeProvider.Now | ||
}; | ||
|
||
return _queue.AddMessageAsync(message); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...it.Vacancies.Client/Application/Queues/Messages/CommunicationsHouseKeepingQueueMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Esfa.Recruit.Vacancies.Client.Application.Queues.Messages | ||
{ | ||
public class CommunicationsHouseKeepingQueueMessage | ||
{ | ||
public DateTime? CreatedByScheduleDate { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters