[rss] Unsubscriber service cleanup.

This commit is contained in:
West14
2022-04-21 14:53:25 +03:00
parent 04a660181d
commit 41a8eaefc2
+10 -13
View File
@@ -15,26 +15,20 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
{
public class Unsubscriber : AbstractTimedHostedService
{
protected readonly ConcurrentQueue<UserUnsubscribe> _queue;
protected readonly IServiceScopeFactory _scopeFactory;
protected readonly ConcurrentQueue<UserUnsubscribe> Queue;
protected readonly IServiceScopeFactory ScopeFactory;
protected override TimeSpan TimerPeriod => TimeSpan.FromSeconds(1);
public Unsubscriber(ILogger<Unsubscriber> logger, IBotInstance bot, ConcurrentQueue<UserUnsubscribe> queue, IServiceScopeFactory scopeFactory) : base(logger, bot)
{
_scopeFactory = scopeFactory;
_queue = queue;
ScopeFactory = scopeFactory;
Queue = queue;
}
protected override async Task OnRun()
{
if (_queue.Count == 0)
{
return;
}
UserUnsubscribe user;
if (!_queue.TryDequeue(out user))
if (Queue.IsEmpty || !Queue.TryDequeue(out var user))
{
return;
}
@@ -44,10 +38,13 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
private async Task UnsubscribeUser(ChatId chatId)
{
using var scope = _scopeFactory.CreateScope();
using var scope = ScopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<RichSiteSummaryContext>();
dbContext.Subscriptions.RemoveRange(dbContext.Subscriptions.Where(s => s.SubscriberId == chatId.Identifier));
var subscriptions = dbContext.Subscriptions;
subscriptions.RemoveRange(
subscriptions.Where(s => s.SubscriberId == chatId.Identifier)
);
await dbContext.SaveChangesAsync();
}
}