Skip to content

Commit

Permalink
Use DB entities to generate remind message and load reminders to proc…
Browse files Browse the repository at this point in the history
…ess one by one.
  • Loading branch information
Misha12 committed Oct 18, 2023
1 parent bb64cb4 commit 688d43d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
24 changes: 10 additions & 14 deletions src/GrillBot.App/Helpers/RemindHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ public RemindHelper(IDiscordClient discordClient, ITextsManager texts)

public async Task<string> ProcessRemindAsync(Database.Entity.RemindMessage remind, bool force)
{
var embed = await CreateRemindEmbedAsync(remind, force);
var embed = CreateRemindEmbed(remind, force);

var destination = await DiscordClient.FindUserAsync(remind.ToUserId.ToUlong());
if (destination == null) return NotSentRemind;
var targetUser = await DiscordClient.FindUserAsync(remind.ToUserId.ToUlong());
if (targetUser is null)
return NotSentRemind;

try
{
var postponeComponents = CreatePostponeComponents(force);
var msg = await destination.SendMessageAsync(embed: embed.Build(), components: postponeComponents);
var msg = await targetUser.SendMessageAsync(embed: embed.Build(), components: postponeComponents);

return msg.Id.ToString();
}
catch (HttpException ex) when (ex.DiscordCode == DiscordErrorCode.CannotSendMessageToUser)
Expand Down Expand Up @@ -58,7 +60,7 @@ public async Task<string> ProcessRemindAsync(Database.Entity.RemindMessage remin
return ComponentsHelper.CreateWrappedComponents(components);
}

private async Task<EmbedBuilder> CreateRemindEmbedAsync(Database.Entity.RemindMessage remind, bool force = false)
private EmbedBuilder CreateRemindEmbed(Database.Entity.RemindMessage remind, bool force = false)
{
const string localeBase = "RemindModule/NotifyMessage/";

Expand All @@ -70,19 +72,13 @@ private async Task<EmbedBuilder> CreateRemindEmbedAsync(Database.Entity.RemindMe
.WithTitle(Texts[force ? localeBase + "ForceTitle" : localeBase + "Title", locale])
.AddField(Texts[localeBase + "Fields/Id", locale], remind.Id, true);

if (remind.FromUserId != remind.ToUserId)
{
var fromUser = await DiscordClient.FindUserAsync(remind.FromUserId.ToUlong());

if (fromUser != null)
embed.AddField(Texts[localeBase + "Fields/From", locale], fromUser.GetFullName(), true);
}
if (remind.FromUserId != remind.ToUserId && remind.FromUser is not null)
embed.AddField(Texts[localeBase + "Fields/From", locale], remind.FromUser.GetDisplayName(), true);

if (remind.Postpone > 0)
embed.AddField(Texts[localeBase + "Fields/Attention", locale], Texts[localeBase + "Postponed", locale].FormatWith(remind.Postpone));

embed
.AddField(Texts[localeBase + "Fields/Message", locale], remind.Message);
embed.AddField(Texts[localeBase + "Fields/Message", locale], remind.Message);

if (!force)
embed.AddField(Texts[localeBase + "Fields/Options", locale], Texts[localeBase + "Options", locale]);
Expand Down
18 changes: 9 additions & 9 deletions src/GrillBot.App/Jobs/RemindCronJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public RemindCronJob(FinishRemind finishRemind, GrillBotDatabaseBuilder database

protected override async Task RunAsync(IJobExecutionContext context)
{
var reminders = await GetIdsForProcessAsync();
await using var repository = DatabaseBuilder.CreateRepository();

var id = await repository.Remind.GetFirstIdForProcessAsync();
var result = new Dictionary<long, string>();
foreach (var id in reminders)

while (id != 0)
{
try
{
Expand All @@ -37,23 +39,21 @@ protected override async Task RunAsync(IJobExecutionContext context)
result.Add(id, CreateReportMessage(ex));
await LoggingManager.ErrorAsync(nameof(RemindCronJob), $"An error occured while processing remind #{id}", ex);
}
finally
{
id = await repository.Remind.GetFirstIdForProcessAsync();
}
}

if (result.Count > 0)
{
var resultBuilder = new StringBuilder($"Processed reminders ({reminders.Count}):").AppendLine()
var resultBuilder = new StringBuilder($"Processed reminders ({result.Count}):").AppendLine()
.AppendJoin("\n", result.Select(o => $"{o.Key}: {o.Value}"));

context.Result = resultBuilder.ToString();
}
}

private async Task<List<long>> GetIdsForProcessAsync()
{
await using var repository = DatabaseBuilder.CreateRepository();
return await repository.Remind.GetRemindIdsForProcessAsync();
}

private string CreateReportMessage(Exception? exception = null)
{
var result = new List<string>();
Expand Down
10 changes: 6 additions & 4 deletions src/GrillBot.Database/Services/Repository/RemindRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ public RemindRepository(GrillBotContext context, ICounterManager counter) : base
{
}

public async Task<List<long>> GetRemindIdsForProcessAsync()
public async Task<long> GetFirstIdForProcessAsync()
{
using (CreateCounter())
{
return await Context.Reminders.AsNoTracking()
var query = Context.Reminders.AsNoTracking()
.Where(o => o.RemindMessageId == null && o.At <= DateTime.Now)
.Select(o => o.Id)
.ToListAsync();
.OrderBy(o => o.At)
.Select(o => o.Id);

return await query.FirstOrDefaultAsync();
}
}

Expand Down

0 comments on commit 688d43d

Please sign in to comment.