Files
Tranga/API/Workers/PeriodicWorkers/SendNotificationsWorker.cs
glax 3b8570cf57 Allow requests to be cancelled.
Make workers have a CancellationTokenSource
2025-09-01 23:26:49 +02:00

29 lines
1.1 KiB
C#

using API.Schema.NotificationsContext;
using API.Schema.NotificationsContext.NotificationConnectors;
namespace API.Workers;
public class SendNotificationsWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
: BaseWorkerWithContext<NotificationsContext>(dependsOn), IPeriodic
{
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
public TimeSpan Interval { get; set; } = interval??TimeSpan.FromMinutes(1);
protected override async Task<BaseWorker[]> DoWorkInternal()
{
NotificationConnector[] connectors = DbContext.NotificationConnectors.ToArray();
Notification[] notifications = DbContext.Notifications.Where(n => n.IsSent == false).ToArray();
foreach (Notification notification in notifications)
{
foreach (NotificationConnector connector in connectors)
{
connector.SendNotification(notification.Title, notification.Message);
notification.IsSent = true;
}
}
await DbContext.Sync(CancellationTokenSource.Token);
return [];
}
}