#7 Added background service to periodically delete expired tickets
This commit is contained in:
parent
18a5a64d4e
commit
713d8c9956
|
@ -15,8 +15,5 @@ namespace AccessQueueService.Data
|
|||
public int DeleteExpiredTickets();
|
||||
public bool RemoveUser(string userId);
|
||||
public bool DidDequeueUntilFull(int activeSeconds, int expirationSeconds, int capacityLimit);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,11 +50,6 @@ namespace AccessQueueService.Data
|
|||
return count;
|
||||
}
|
||||
|
||||
public void RemoveUser(string userId)
|
||||
{
|
||||
_accessTickets.Remove(userId);
|
||||
}
|
||||
|
||||
public bool DidDequeueUntilFull(int activeSeconds, int expirationSeconds, int capacityLimit)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
|
|
|
@ -11,6 +11,7 @@ builder.Services.AddEndpointsApiExplorer();
|
|||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSingleton<IAccessService, AccessService>();
|
||||
builder.Services.AddSingleton<IAccessQueueRepo, TakeANumberAccessQueueRepo>();
|
||||
builder.Services.AddHostedService<AccessCleanupBackgroundService>();
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
namespace AccessQueueService.Services
|
||||
{
|
||||
public class AccessCleanupBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IAccessService _accessService;
|
||||
private readonly IConfiguration _config;
|
||||
|
||||
public AccessCleanupBackgroundService(IAccessService accessService, IConfiguration config)
|
||||
{
|
||||
_accessService = accessService;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var cleanupIntervalMillis = _config.GetValue<int>("AccessQueue:CleanupIntervalSeconds") * 1000;
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
_accessService.DeleteExpiredTickets();
|
||||
await Task.Delay(cleanupIntervalMillis, stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,7 +9,8 @@
|
|||
"CapacityLimit": 100, // Maximum number of active users
|
||||
"ActivitySeconds": 900, // Time since last access before a user is considered inactive
|
||||
"ExpirationSeconds": 43200, // 12 hours - Time before a user access is revoked
|
||||
"RollingExpiration": true // Whether to extend expiration time on access
|
||||
"RollingExpiration": true, // Whether to extend expiration time on access
|
||||
"CleanupIntervalSeconds": 60 // Interval for cleaning up expired users
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue