Skip to content

Commit

Permalink
EHD-461: Load Tests: Code changes: Make most background jobs singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgriff committed Feb 11, 2025
1 parent e6b9f08 commit bd35974
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 14 deletions.
29 changes: 28 additions & 1 deletion GenderPayGap.WebUI/BackgroundJobs/JobHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using GenderPayGap.Core.Classes.Logger;
using GenderPayGap.Extensions;
using GenderPayGap.Extensions.AspNetCore;
Expand All @@ -12,9 +13,35 @@ internal enum JobErrorsLogged

internal static class JobHelpers {

private static readonly ConcurrentDictionary<string, string> ActionsCurrentlyRunning = [];

public static void RunAndLogSingletonJob(Action action, string actionName, JobErrorsLogged logErrors = JobErrorsLogged.Automatically)
{
RunAndLogSingletonJob(unusedRunId => action(), actionName, logErrors);
}

public static void RunAndLogSingletonJob(Action<string> action, string actionName, JobErrorsLogged logErrors = JobErrorsLogged.Automatically)
{
if (ActionsCurrentlyRunning.TryAdd(actionName, ""))
{
try
{
RunAndLogJob(action, actionName, logErrors);
}
finally
{
ActionsCurrentlyRunning.Remove(actionName, out _);
}
}
else
{
CustomLogger.Information($"Function already running: {actionName}", new {environment = Config.EnvironmentName});
}
}

public static void RunAndLogJob(Action action, string actionName, JobErrorsLogged logErrors = JobErrorsLogged.Automatically)
{
RunAndLogJob(unusedRunId => action(), actionName, logErrors);
RunAndLogJob(_ => action(), actionName, logErrors);
}

public static void RunAndLogJob(Action<string> action, string actionName, JobErrorsLogged logErrors = JobErrorsLogged.Automatically)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public AnonymiseRetiredDuplicateUsersJob(IDataRepository dataRepository)
//Remove any duplicate retired user details
public void AnonymiseRetiredDuplicateUsers()
{
JobHelpers.RunAndLogJob(AnonymiseRetiredDuplicateUsersAction, nameof(AnonymiseRetiredDuplicateUsers));
JobHelpers.RunAndLogSingletonJob(AnonymiseRetiredDuplicateUsersAction, nameof(AnonymiseRetiredDuplicateUsers));
}

private void AnonymiseRetiredDuplicateUsersAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public AnonymiseThreeYearOldFeedbackJob(IDataRepository dataRepository)

public void AnonymiseFeedback()
{
JobHelpers.RunAndLogJob(GetAndAnonymiseFeedback, nameof(AnonymiseFeedback));
JobHelpers.RunAndLogSingletonJob(GetAndAnonymiseFeedback, nameof(AnonymiseFeedback));
}

public void GetAndAnonymiseFeedback()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public BackupDatabaseToJsonFileJob(

public void RunBackup()
{
JobHelpers.RunAndLogJob(RunBackupAction, nameof(RunBackup));
JobHelpers.RunAndLogSingletonJob(RunBackupAction, nameof(RunBackup));
}

private void RunBackupAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public FetchCompaniesHouseDataJob(

public void FetchCompaniesHouseData()
{
JobHelpers.RunAndLogJob(UpdateFromCompaniesHouse, nameof(FetchCompaniesHouseData));
JobHelpers.RunAndLogSingletonJob(UpdateFromCompaniesHouse, nameof(FetchCompaniesHouseData));
}

private void UpdateFromCompaniesHouse(string runId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public NotifyUsersAndRetireInactiveAccountsJob(IDataRepository dataRepository, E

public void NotifyUsersAndRetireInactiveAccounts()
{
JobHelpers.RunAndLogJob(NotifyUsersAndRetireInactiveAccountsAction, nameof(NotifyUsersAndRetireInactiveAccounts));
JobHelpers.RunAndLogSingletonJob(NotifyUsersAndRetireInactiveAccountsAction, nameof(NotifyUsersAndRetireInactiveAccounts));
}

private void NotifyUsersAndRetireInactiveAccountsAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PurgeOrganisationsJob(IDataRepository dataRepository, AuditLogger auditLo
//Remove any unverified users their addresses, UserOrgs, Org and addresses and archive to zip
public void PurgeOrganisations()
{
JobHelpers.RunAndLogJob(PurgeOrganisationsAction, nameof(PurgeOrganisations));
JobHelpers.RunAndLogSingletonJob(PurgeOrganisationsAction, nameof(PurgeOrganisations));
}

private void PurgeOrganisationsAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public PurgeRegistrationsJob(IDataRepository dataRepository, AuditLogger auditLo
//Remove any incomplete registrations
public void PurgeRegistrations()
{
JobHelpers.RunAndLogJob(PurgeRegistrationsAction, nameof(PurgeRegistrations));
JobHelpers.RunAndLogSingletonJob(PurgeRegistrationsAction, nameof(PurgeRegistrations));
}

private void PurgeRegistrationsAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public PurgeUsersJob(IDataRepository dataRepository, AuditLogger auditLogger)
//Remove any unverified users their addresses, UserOrgs, Org and addresses and archive to zip
public void PurgeUsers()
{
JobHelpers.RunAndLogJob(PurgeUsersAction, nameof(PurgeUsers));
JobHelpers.RunAndLogSingletonJob(PurgeUsersAction, nameof(PurgeUsers));
}

private void PurgeUsersAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SendReminderEmailsJob(

public void SendReminderEmails()
{
JobHelpers.RunAndLogJob(SendReminderEmailsAction, nameof(SendReminderEmails));
JobHelpers.RunAndLogSingletonJob(SendReminderEmailsAction, nameof(SendReminderEmails));
}

private void SendReminderEmailsAction(string runId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public SetPresumedScopesJob(IDataRepository dataRepository)
//Set presumed scope of previous years and current years
public void SetPresumedScopes()
{
JobHelpers.RunAndLogJob(SetPresumedScopesAction, nameof(SetPresumedScopes));
JobHelpers.RunAndLogSingletonJob(SetPresumedScopesAction, nameof(SetPresumedScopes));
}

private void SetPresumedScopesAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public UpdatePublicFacingDownloadFilesJob(

public void UpdateDownloadFiles()
{
JobHelpers.RunAndLogJob(UpdateDownloadFilesAction, nameof(UpdateDownloadFiles));
JobHelpers.RunAndLogSingletonJob(UpdateDownloadFilesAction, nameof(UpdateDownloadFiles));
}

private void UpdateDownloadFilesAction()
Expand Down
17 changes: 15 additions & 2 deletions GenderPayGap.WebUI/Search/SearchCacheUpdaterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class SearchCacheUpdaterService : IHostedService, IDisposable
{
private Timer timer;

private bool updateInProgress = false;

public Task StartAsync(CancellationToken stoppingToken)
{
CustomLogger.Information("Starting timer (SearchRepository.StartCacheUpdateThread)");
Expand All @@ -27,18 +29,29 @@ public Task StartAsync(CancellationToken stoppingToken)

private void DoWork(object state)
{
if (updateInProgress)
{
CustomLogger.Information("Cache update postponed - another update is already in progress (SearchRepository.StartCacheUpdateThread)");
return;
}

updateInProgress = true;
CustomLogger.Information("Starting cache update (SearchRepository.StartCacheUpdateThread)");

try
{
SearchRepository.LoadSearchDataIntoCache();

CustomLogger.Information("Finished cache update (SearchRepository.StartCacheUpdateThread)");
}
catch (Exception ex)
{
CustomLogger.Error($"Error during cache update (SearchRepository.StartCacheUpdateThread): {ex.Message} {ex.StackTrace}", ex);
}

CustomLogger.Information("Finished cache update (SearchRepository.StartCacheUpdateThread)");
finally
{
updateInProgress = false;
}
}

public Task StopAsync(CancellationToken stoppingToken)
Expand Down

0 comments on commit bd35974

Please sign in to comment.