Files
Tranga/API/Workers/PeriodicWorkers/MaintenanceWorkers/RemoveOldNotificationsWorker.cs
glax 53276e858b
Some checks are pending
Docker Image CI / build (push) Waiting to run
Actions initial commit
2025-10-16 02:53:02 +02:00

36 lines
1.4 KiB
C#

using System.Diagnostics.CodeAnalysis;
using API.Schema.NotificationsContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
/// <summary>
/// Removes sent notifications from database
/// </summary>
public class RemoveOldNotificationsWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
: BaseWorkerWithContexts(dependsOn), IPeriodic
{
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
public TimeSpan Interval { get; set; } = interval ?? TimeSpan.FromHours(1);
[SuppressMessage("ReSharper", "InconsistentNaming")]
private NotificationsContext NotificationsContext = null!;
protected override void SetContexts(IServiceScope serviceScope)
{
NotificationsContext = GetContext<NotificationsContext>(serviceScope);
}
protected override async Task<BaseWorker[]> DoWorkInternal()
{
Log.Debug("Removing old notifications...");
int removed = await NotificationsContext.Notifications.Where(n => n.IsSent).ExecuteDeleteAsync(CancellationToken);
Log.Debug($"Removed {removed} old notifications...");
if(await NotificationsContext.Sync(CancellationToken, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } e)
Log.Error($"Failed to save database changes: {e.exceptionMessage}");
return [];
}
}