mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 01:38:27 -04:00
Small fix for a reminder query
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
#nullable disable
|
#nullable disable
|
||||||
using LinqToDB;
|
using LinqToDB;
|
||||||
using LinqToDB.EntityFrameworkCore;
|
using LinqToDB.EntityFrameworkCore;
|
||||||
|
using NadekoBot.Common.ModuleBehaviors;
|
||||||
using NadekoBot.Services.Database.Models;
|
using NadekoBot.Services.Database.Models;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace NadekoBot.Modules.Utility.Services;
|
namespace NadekoBot.Modules.Utility.Services;
|
||||||
|
|
||||||
public class RemindService : INService
|
public class RemindService : INService, IReadyExecutor
|
||||||
{
|
{
|
||||||
private readonly Regex _regex =
|
private readonly Regex _regex =
|
||||||
new(
|
new(
|
||||||
@@ -28,37 +29,41 @@ public class RemindService : INService
|
|||||||
_db = db;
|
_db = db;
|
||||||
_creds = creds;
|
_creds = creds;
|
||||||
_eb = eb;
|
_eb = eb;
|
||||||
_ = StartReminderLoop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task StartReminderLoop()
|
public async Task OnReadyAsync()
|
||||||
{
|
{
|
||||||
while (true)
|
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(15));
|
||||||
|
while (await timer.WaitForNextTickAsync())
|
||||||
{
|
{
|
||||||
await Task.Delay(15000);
|
await OnReminderLoopTickInternalAsync();
|
||||||
try
|
}
|
||||||
{
|
}
|
||||||
var now = DateTime.UtcNow;
|
|
||||||
var reminders = await GetRemindersBeforeAsync(now);
|
|
||||||
if (reminders.Count == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Log.Information("Executing {ReminderCount} reminders", reminders.Count);
|
private async Task OnReminderLoopTickInternalAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
var reminders = await GetRemindersBeforeAsync(now);
|
||||||
|
if (reminders.Count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
// make groups of 5, with 1.5 second inbetween each one to ensure against ratelimits
|
Log.Information("Executing {ReminderCount} reminders", reminders.Count);
|
||||||
foreach (var group in reminders.Chunk(5))
|
|
||||||
{
|
// make groups of 5, with 1.5 second inbetween each one to ensure against ratelimits
|
||||||
var executedReminders = group.ToList();
|
foreach (var group in reminders.Chunk(5))
|
||||||
await executedReminders.Select(ReminderTimerAction).WhenAll();
|
|
||||||
await RemoveReminders(executedReminders.Select(x => x.Id));
|
|
||||||
await Task.Delay(1500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
Log.Warning(ex, "Error in reminder loop: {ErrorMessage}", ex.Message);
|
var executedReminders = group.ToList();
|
||||||
|
await executedReminders.Select(ReminderTimerAction).WhenAll();
|
||||||
|
await RemoveReminders(executedReminders.Select(x => x.Id));
|
||||||
|
await Task.Delay(1500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log.Warning(ex, "Error in reminder loop: {ErrorMessage}", ex.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task RemoveReminders(IEnumerable<int> reminders)
|
private async Task RemoveReminders(IEnumerable<int> reminders)
|
||||||
@@ -71,10 +76,10 @@ public class RemindService : INService
|
|||||||
await uow.SaveChangesAsync();
|
await uow.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<List<Reminder>> GetRemindersBeforeAsync(DateTime now)
|
private async Task<List<Reminder>> GetRemindersBeforeAsync(DateTime now)
|
||||||
{
|
{
|
||||||
using var uow = _db.GetDbContext();
|
await using var uow = _db.GetDbContext();
|
||||||
return uow.Reminders
|
return await uow.Reminders
|
||||||
.ToLinqToDBTable()
|
.ToLinqToDBTable()
|
||||||
.Where(x => x.ServerId / 4194304 % (ulong)_creds.TotalShards == (ulong)_client.ShardId
|
.Where(x => x.ServerId / 4194304 % (ulong)_creds.TotalShards == (ulong)_client.ShardId
|
||||||
&& x.When < now)
|
&& x.When < now)
|
||||||
|
Reference in New Issue
Block a user