Skip to content

Commit

Permalink
Deleting old accounts is now a scheduled job.
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Jun 15, 2024
1 parent 4644340 commit cefe5af
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions SS14.Auth.Shared/Config/AccountConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SS14.Auth.Shared.Config;

public sealed class AccountConfiguration
{
/// <summary>
/// Delete unconfirmed accounts after this many days.
/// </summary>
public int DeleteUnconfirmedAfter { get; set; } = 3;
}
1 change: 1 addition & 0 deletions SS14.Auth.Shared/StartupHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static void AddShared(IServiceCollection services, IConfiguration config)
services.Configure<MutexOptions>(config.GetSection("Mutex"));
services.Configure<PatreonConfiguration>(config.GetSection("Patreon"));
services.Configure<AccountLogRetentionConfiguration>(config.GetSection("AccountLogRetention"));
services.Configure<AccountConfiguration>(config.GetSection("Account"));
services.Configure<SecurityStampValidatorOptions>(options =>
{
// The fact that this isn't default absolutely baffles me.
Expand Down
32 changes: 32 additions & 0 deletions SS14.Auth/Jobs/DeleteUnconfirmedAccountsJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Quartz;
using SS14.Auth.Shared.Config;
using SS14.Auth.Shared.Data;

namespace SS14.Auth.Jobs;

public sealed class DeleteUnconfirmedAccounts(ApplicationDbContext dbContext,
ILogger<DeleteUnconfirmedAccounts> logger,
IOptions<AccountConfiguration> configuration) : IJob
{
public async Task Execute(IJobExecutionContext context)
{
var optionsValue = configuration.Value;

var retainMinimum = DateTime.UtcNow - TimeSpan.FromDays(optionsValue.DeleteUnconfirmedAfter);

// TODO: Use ExecuteDelete() on newer EF Core version.
var accounts = await dbContext.Users
.Where(user => !user.EmailConfirmed && user.CreatedTime < retainMinimum)
.ToListAsync(context.CancellationToken);

dbContext.RemoveRange(accounts);
await dbContext.SaveChangesAsync();
logger.LogInformation("Deleted {Count} unconfirmed accounts from database", accounts.Count);
}
}
5 changes: 5 additions & 0 deletions SS14.Auth/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public void ConfigureServices(IServiceCollection services)
{
schedule.RepeatForever().WithIntervalInHours(24);
}));
q.ScheduleJob<DeleteUnconfirmedAccounts>(trigger => trigger.WithSimpleSchedule(schedule =>
{
schedule.RepeatForever().WithIntervalInHours(24);
}));
}
});
services.AddQuartzHostedService();
Expand Down

0 comments on commit cefe5af

Please sign in to comment.