Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/SocialAgent.Data/Repositories/ISocialDataRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public interface ISocialDataRepository
// Poll State
Task<PollState?> GetPollStateAsync(string providerId, CancellationToken ct = default);
Task UpsertPollStateAsync(PollState state, CancellationToken ct = default);

// Retention
Task<int> PurgeOldPostsAsync(DateTimeOffset olderThan, CancellationToken ct = default);
Task<int> PurgeOldNotificationsAsync(DateTimeOffset olderThan, CancellationToken ct = default);
}
14 changes: 14 additions & 0 deletions src/SocialAgent.Data/Repositories/SocialDataRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,18 @@ public async Task UpsertPollStateAsync(PollState state, CancellationToken ct = d
}
await db.SaveChangesAsync(ct);
}

public async Task<int> PurgeOldPostsAsync(DateTimeOffset olderThan, CancellationToken ct = default)
{
return await db.Posts
.Where(p => p.CreatedAt < olderThan)
.ExecuteDeleteAsync(ct);
}

public async Task<int> PurgeOldNotificationsAsync(DateTimeOffset olderThan, CancellationToken ct = default)
{
return await db.Notifications
.Where(n => n.CreatedAt < olderThan)
.ExecuteDeleteAsync(ct);
}
}
1 change: 1 addition & 0 deletions src/SocialAgent.Host/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
// Background services
builder.Services.AddHostedService<DatabaseMigrationService>();
builder.Services.AddHostedService<SocialMediaPollingService>();
builder.Services.AddHostedService<DataRetentionService>();

// Authentication (API key required in non-Development environments)
builder.Services.AddApiKeyAuthentication(builder.Configuration);
Expand Down
54 changes: 54 additions & 0 deletions src/SocialAgent.Host/Services/DataRetentionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using SocialAgent.Data.Repositories;

namespace SocialAgent.Host.Services;

public class DataRetentionService(
IServiceScopeFactory scopeFactory,
ILogger<DataRetentionService> logger,
IConfiguration configuration) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var retentionDays = configuration.GetValue("SocialAgent:RetentionDays", 30);
var intervalHours = configuration.GetValue("SocialAgent:RetentionCheckIntervalHours", 24);

logger.LogInformation(
"Data retention service starting: {RetentionDays} day retention, checking every {Interval}h",
retentionDays, intervalHours);

// Delay startup to let migrations complete
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);

while (!stoppingToken.IsCancellationRequested)
{
try
{
await PurgeOldDataAsync(retentionDays, stoppingToken);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
logger.LogError(ex, "Error during data retention purge");
}

await Task.Delay(TimeSpan.FromHours(intervalHours), stoppingToken);
}
}

private async Task PurgeOldDataAsync(int retentionDays, CancellationToken ct)
{
var cutoff = DateTimeOffset.UtcNow.AddDays(-retentionDays);

using var scope = scopeFactory.CreateScope();
var repository = scope.ServiceProvider.GetRequiredService<ISocialDataRepository>();

var postsDeleted = await repository.PurgeOldPostsAsync(cutoff, ct);
var notificationsDeleted = await repository.PurgeOldNotificationsAsync(cutoff, ct);

if (postsDeleted > 0 || notificationsDeleted > 0)
{
logger.LogInformation(
"Data retention purge: deleted {Posts} posts and {Notifications} notifications older than {Days} days",
postsDeleted, notificationsDeleted, retentionDays);
}
}
}
2 changes: 2 additions & 0 deletions src/SocialAgent.Host/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
},
"SocialAgent": {
"PollingIntervalMinutes": 5,
"RetentionDays": 30,
"RetentionCheckIntervalHours": 24,
"DatabaseProvider": "Sqlite",
"Providers": {
"Mastodon": {
Expand Down
Loading