Skip to content

Commit

Permalink
Added a prefix identifier to redis lock keys (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianwium authored Apr 10, 2024
1 parent 14754c5 commit eb0e289
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/api/src/application/Yoma.Core.Api/Common/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Yoma.Core.Api.Common
{
internal class Constants
internal static class Constants
{
internal const string RequestHeader_ApiKey = "X-ApiKey";
internal const string Authorization_Policy = "yoma_core_api";
Expand Down
3 changes: 1 addition & 2 deletions src/api/src/domain/Yoma.Core.Domain/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ public static class Constants
public const string Role_User = "User";
public const string Role_Admin = "Admin";
public const string Role_OrganizationAdmin = "OrganisationAdmin";

public static readonly string[] Roles_Supported = [Role_User, Role_Admin, Role_OrganizationAdmin];

public const string ModifiedBy_System_Username = "[email protected]";
public const string Redis_LockIdentifier_Prefix = "yoma.api.core:locks:";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Reflection;
using Yoma.Core.Domain.Core;
using Yoma.Core.Domain.Core.Helpers;
using Yoma.Core.Domain.Core.Interfaces;
using Yoma.Core.Domain.Core.Models;
Expand Down Expand Up @@ -65,7 +66,7 @@ public OrganizationBackgroundService(ILogger<OrganizationBackgroundService> logg
#region Public Memebers
public async Task ProcessDeclination()
{
const string lockIdentifier = "organization_process_declination";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}organization_process_declination";
var dateTimeNow = DateTimeOffset.UtcNow;
var executeUntil = dateTimeNow.AddHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours);
var lockDuration = executeUntil - dateTimeNow + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);
Expand All @@ -81,7 +82,7 @@ public async Task ProcessDeclination()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

_logger.LogInformation("Processing organization declination");

Expand Down Expand Up @@ -163,7 +164,7 @@ public async Task ProcessDeclination()

public async Task ProcessDeletion()
{
const string lockIdentifier = "organization_process_deletion";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}organization_process_deletion";
var dateTimeNow = DateTimeOffset.UtcNow;
var executeUntil = dateTimeNow.AddHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours);
var lockDuration = executeUntil - dateTimeNow + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);
Expand All @@ -179,7 +180,7 @@ public async Task ProcessDeletion()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

_logger.LogInformation("Processing organization deletion");

Expand Down Expand Up @@ -226,7 +227,7 @@ public async Task ProcessDeletion()

public async Task SeedLogoAndDocuments()
{
const string lockIdentifier = "organization_seed_logos_and_documents";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}organization_seed_logos_and_documents";
var lockDuration = TimeSpan.FromHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours) + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);

if (!await _distributedLockService.TryAcquireLockAsync(lockIdentifier, lockDuration))
Expand All @@ -240,7 +241,7 @@ public async Task SeedLogoAndDocuments()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

if (!_appSettings.TestDataSeedingEnvironmentsAsEnum.HasFlag(_environmentProvider.Environment))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Reflection;
using Yoma.Core.Domain.Core;
using Yoma.Core.Domain.Core.Helpers;
using Yoma.Core.Domain.Core.Interfaces;
using Yoma.Core.Domain.Core.Models;
Expand Down Expand Up @@ -45,7 +46,7 @@ public UserBackgroundService(ILogger<UserBackgroundService> logger,
#region Public Members
public async Task SeedPhotos()
{
const string lockIdentifier = "user_seed_photos";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}user_seed_photos";
var lockDuration = TimeSpan.FromHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours) + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);

if (!await _distributedLockService.TryAcquireLockAsync(lockIdentifier, lockDuration))
Expand All @@ -59,7 +60,7 @@ public async Task SeedPhotos()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

if (!_appSettings.TestDataSeedingEnvironmentsAsEnum.HasFlag(_environmentProvider.Environment))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Hangfire.Storage;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Yoma.Core.Domain.Core;
using Yoma.Core.Domain.Core.Interfaces;
using Yoma.Core.Domain.Core.Models;
using Yoma.Core.Domain.LaborMarketProvider.Interfaces;
Expand Down Expand Up @@ -108,7 +109,7 @@ public SkillSearchResults Search(SkillSearchFilter filter)

public async Task SeedSkills(bool onStartupInitialSeeding)
{
const string lockIdentifier = "skill_seed";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}skill_seed";
var lockDuration = TimeSpan.FromHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours) + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);

if (!await _distributedLockService.TryAcquireLockAsync(lockIdentifier, lockDuration))
Expand All @@ -122,7 +123,7 @@ public async Task SeedSkills(bool onStartupInitialSeeding)
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Options;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using Yoma.Core.Domain.Core;
using Yoma.Core.Domain.Core.Helpers;
using Yoma.Core.Domain.Core.Interfaces;
using Yoma.Core.Domain.Core.Models;
Expand Down Expand Up @@ -71,7 +72,7 @@ public MyOpportunityBackgroundService(ILogger<MyOpportunityBackgroundService> lo
#region Public Members
public async Task ProcessVerificationRejection()
{
const string lockIdentifier = "myopportunity_process_verification_rejection";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}myopportunity_process_verification_rejection";
var dateTimeNow = DateTimeOffset.UtcNow;
var executeUntil = dateTimeNow.AddHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours);
var lockDuration = executeUntil - dateTimeNow + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);
Expand All @@ -87,7 +88,7 @@ public async Task ProcessVerificationRejection()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

_logger.LogInformation("Processing 'my' opportunity verification rejection");

Expand Down Expand Up @@ -174,7 +175,7 @@ public async Task ProcessVerificationRejection()

public async Task SeedPendingVerifications()
{
const string lockIdentifier = "myopportunity_seed_pending_verifications]";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}myopportunity_seed_pending_verifications]";
var lockDuration = TimeSpan.FromHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours) + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);

if (!await _distributedLockService.TryAcquireLockAsync(lockIdentifier, lockDuration))
Expand All @@ -188,7 +189,7 @@ public async Task SeedPendingVerifications()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

if (!_appSettings.TestDataSeedingEnvironmentsAsEnum.HasFlag(_environmentProvider.Environment))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hangfire.Storage;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Yoma.Core.Domain.Core;
using Yoma.Core.Domain.Core.Helpers;
using Yoma.Core.Domain.Core.Interfaces;
using Yoma.Core.Domain.Core.Models;
Expand Down Expand Up @@ -56,7 +57,7 @@ public OpportunityBackgroundService(ILogger<OpportunityBackgroundService> logger
#region Public Members
public async Task ProcessExpiration()
{
const string lockIdentifier = "opporrtunity_process_expiration";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}opportunity_process_expiration";
var dateTimeNow = DateTimeOffset.UtcNow;
var executeUntil = dateTimeNow.AddHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours);
var lockDuration = executeUntil - dateTimeNow + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);
Expand All @@ -72,7 +73,7 @@ public async Task ProcessExpiration()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

_logger.LogInformation("Processing opportunity expiration");

Expand Down Expand Up @@ -120,7 +121,7 @@ public async Task ProcessExpiration()

public async Task ProcessExpirationNotifications()
{
const string lockIdentifier = "opporrtunity_process_expiration_notifications";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}opportunity_process_expiration_notifications";
var lockDuration = TimeSpan.FromHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours) + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);

if (!await _distributedLockService.TryAcquireLockAsync(lockIdentifier, lockDuration))
Expand All @@ -134,7 +135,7 @@ public async Task ProcessExpirationNotifications()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

_logger.LogInformation("Processing opportunity expiration notifications");

Expand Down Expand Up @@ -168,7 +169,7 @@ public async Task ProcessExpirationNotifications()

public async Task ProcessDeletion()
{
const string lockIdentifier = "opporrtunity_process_deletion";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}opportunity_process_deletion";
var dateTimeNow = DateTimeOffset.UtcNow;
var executeUntil = dateTimeNow.AddHours(_scheduleJobOptions.DefaultScheduleMaxIntervalInHours);
var lockDuration = executeUntil - dateTimeNow + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);
Expand All @@ -184,7 +185,7 @@ public async Task ProcessDeletion()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

_logger.LogInformation("Processing opportunity deletion");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Transactions;
using Yoma.Core.Domain.Core;
using Yoma.Core.Domain.Core.Interfaces;
using Yoma.Core.Domain.Core.Models;
using Yoma.Core.Domain.MyOpportunity.Interfaces;
Expand Down Expand Up @@ -56,7 +57,7 @@ public RewardBackgroundService(ILogger<SSIBackgroundService> logger,
#region Public Members
public async Task ProcessWalletCreation()
{
const string lockIdentifier = "reward_process_wallet_creation";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}reward_process_wallet_creation";
var dateTimeNow = DateTimeOffset.UtcNow;
var executeUntil = dateTimeNow.AddHours(_scheduleJobOptions.RewardWalletCreationScheduleMaxIntervalInHours);
var lockDuration = executeUntil - dateTimeNow + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);
Expand All @@ -72,7 +73,7 @@ public async Task ProcessWalletCreation()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

_logger.LogInformation("Processing Reward wallet creation");

Expand Down Expand Up @@ -138,7 +139,7 @@ await _executionStrategyService.ExecuteInExecutionStrategyAsync(async () =>

public async Task ProcessRewardTransactions()
{
const string lockIdentifier = "reward_process_transactions";
const string lockIdentifier = $"{Constants.Redis_LockIdentifier_Prefix}reward_process_transactions";
var dateTimeNow = DateTimeOffset.UtcNow;
var executeUntil = dateTimeNow.AddHours(_scheduleJobOptions.RewardTransactionScheduleMaxIntervalInHours);
var lockDuration = executeUntil - dateTimeNow + TimeSpan.FromMinutes(_scheduleJobOptions.DistributedLockDurationBufferInMinutes);
Expand All @@ -154,7 +155,7 @@ public async Task ProcessRewardTransactions()
using (JobStorage.Current.GetConnection().AcquireDistributedLock(lockIdentifier, lockDuration))
{
_logger.LogInformation("Lock '{lockIdentifier}' acquired by {hostName} at {dateStamp}. Lock duration set to {lockDurationInMinutes} minutes",
lockIdentifier, Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);
lockIdentifier, System.Environment.MachineName, DateTimeOffset.UtcNow, lockDuration.TotalMinutes);

_logger.LogInformation("Processing reward transactions");

Expand Down
Loading

0 comments on commit eb0e289

Please sign in to comment.