diff --git a/API.csproj b/API.csproj index 117a655..9b855ed 100644 --- a/API.csproj +++ b/API.csproj @@ -23,12 +23,12 @@ - + - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Tasks/UserRefresh.cs b/Tasks/UserRefresh.cs index c8106e4..ae98ce4 100644 --- a/Tasks/UserRefresh.cs +++ b/Tasks/UserRefresh.cs @@ -4,6 +4,7 @@ using API.Util; using DotNetBungieAPI.HashReferences; using DotNetBungieAPI.Models.Destiny; +using DotNetBungieAPI.Models.Exceptions; using DotNetBungieAPI.Service.Abstractions; using Microsoft.EntityFrameworkCore; @@ -19,17 +20,17 @@ public class UserRefresh( protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - using var scope = services.CreateScope(); - var db = - scope.ServiceProvider - .GetRequiredService(); - while (!stoppingToken.IsCancellationRequested) { TaskSchedulerService.Tasks.First(t => t.Name == ServiceName).IsRunning = true; try { + using var scope = services.CreateScope(); + var db = + scope.ServiceProvider + .GetRequiredService(); + var users = await db.Users.Include(u => u.BungieProfiles).ToListAsync(stoppingToken); var bungieProfiles = users.SelectMany(u => u.BungieProfiles) // TODO: .Where(u => u.NeverExpire) @@ -123,9 +124,23 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) await db.SaveChangesAsync(stoppingToken); } + catch (BungieHtmlResponseErrorException e) + { + logger.LogError(e, "Exception in {service}", ServiceName); + + var fileName = $"Logs/bungie-error-{DateTimeExtensions.GetCurrentTimestamp()}.html"; + + await File.WriteAllTextAsync(fileName, e.Html, stoppingToken); + + await DiscordTools.SendMessage(DiscordTools.WebhookChannel.Logs, + $"Exception in {ServiceName}:\n\n>>> {e.GetType()}: Logs saved to {fileName}"); + } catch (Exception e) { logger.LogError(e, "Exception in {service}", ServiceName); + + await DiscordTools.SendMessage(DiscordTools.WebhookChannel.Logs, + $"Exception in {ServiceName}:\n\n>>> {e.GetType()}: {e.Message}"); } TaskSchedulerService.Tasks.First(t => t.Name == ServiceName).IsRunning = false; diff --git a/Tasks/VendorsAdepts.cs b/Tasks/VendorsAdepts.cs index cdc566e..eee3565 100644 --- a/Tasks/VendorsAdepts.cs +++ b/Tasks/VendorsAdepts.cs @@ -23,11 +23,6 @@ public class VendorsAdepts( protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - using var scope = services.CreateScope(); - var db = - scope.ServiceProvider - .GetRequiredService(); - // TODO: raise this to 5/10 minutes await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken); @@ -45,6 +40,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) try { + using var scope = services.CreateScope(); + var db = + scope.ServiceProvider + .GetRequiredService(); + var vendorUser = db.Users.Include(u => u.BungieProfiles) .FirstOrDefault(x => x.Id == UserExtensions.SignId(Variables.OwnerId)); diff --git a/Tasks/VendorsGunsmith.cs b/Tasks/VendorsGunsmith.cs index 023f4e8..d4e3c98 100644 --- a/Tasks/VendorsGunsmith.cs +++ b/Tasks/VendorsGunsmith.cs @@ -18,11 +18,6 @@ public class VendorsGunsmith( protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - using var scope = services.CreateScope(); - var db = - scope.ServiceProvider - .GetRequiredService(); - // TODO: raise this to 5/10 minutes await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken); @@ -35,6 +30,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) try { + using var scope = services.CreateScope(); + var db = + scope.ServiceProvider + .GetRequiredService(); + var vendorUser = db.Users.Include(u => u.BungieProfiles) .FirstOrDefault(x => x.Id == UserExtensions.SignId(Variables.OwnerId)); diff --git a/Tasks/VendorsIronBanner.cs b/Tasks/VendorsIronBanner.cs index d74275c..e11d0b8 100644 --- a/Tasks/VendorsIronBanner.cs +++ b/Tasks/VendorsIronBanner.cs @@ -19,11 +19,6 @@ public class VendorsIronBanner( protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - using var scope = services.CreateScope(); - var db = - scope.ServiceProvider - .GetRequiredService(); - // TODO: raise this to 5/10 minutes await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken); @@ -36,6 +31,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) try { + using var scope = services.CreateScope(); + var db = + scope.ServiceProvider + .GetRequiredService(); + var userList = new List { db.VendorUsers.FirstOrDefault(x => x.VendorId == VendorId && x.Resets == 0 && x.Rank < 10), diff --git a/Tasks/VendorsTrials.cs b/Tasks/VendorsTrials.cs index ba961f8..0e07249 100644 --- a/Tasks/VendorsTrials.cs +++ b/Tasks/VendorsTrials.cs @@ -19,11 +19,6 @@ public class VendorsTrials( protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - using var scope = services.CreateScope(); - var db = - scope.ServiceProvider - .GetRequiredService(); - // TODO: raise this to 5/10 minutes await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken); @@ -36,6 +31,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) try { + using var scope = services.CreateScope(); + var db = + scope.ServiceProvider + .GetRequiredService(); + var userList = new List { db.VendorUsers.FirstOrDefault(x => x.VendorId == VendorId && x.Resets == 0 && x.Rank < 10), diff --git a/Tasks/VendorsWarTable.cs b/Tasks/VendorsWarTable.cs index a8a0f5e..04fcddd 100644 --- a/Tasks/VendorsWarTable.cs +++ b/Tasks/VendorsWarTable.cs @@ -18,11 +18,6 @@ public class VendorsWarTable( protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - using var scope = services.CreateScope(); - var db = - scope.ServiceProvider - .GetRequiredService(); - // TODO: raise this to 5/10 minutes await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken); @@ -35,6 +30,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) try { + using var scope = services.CreateScope(); + var db = + scope.ServiceProvider + .GetRequiredService(); + var vendorUser = db.Users.Include(u => u.BungieProfiles) .FirstOrDefault(x => x.Id == UserExtensions.SignId(Variables.BotId)); diff --git a/Util/DateTimeExtensions.cs b/Util/DateTimeExtensions.cs index 35fc729..672e209 100644 --- a/Util/DateTimeExtensions.cs +++ b/Util/DateTimeExtensions.cs @@ -18,4 +18,9 @@ public static DateTime GetRoundTime(DateTime currentTime, int roundMinutes) return nextRoundTime; } + + public static long GetCurrentTimestamp() + { + return DateTimeOffset.Now.ToUnixTimeSeconds(); + } } diff --git a/Util/VendorTools.cs b/Util/VendorTools.cs index 5f0240c..b214a70 100644 --- a/Util/VendorTools.cs +++ b/Util/VendorTools.cs @@ -5,12 +5,15 @@ using DotNetBungieAPI.Extensions; using DotNetBungieAPI.Models.Destiny; using DotNetBungieAPI.Models.Destiny.Definitions.Vendors; +using DotNetBungieAPI.Models.Exceptions; using DotNetBungieAPI.Service.Abstractions; namespace API.Util; public static class VendorTools { + private const string ServiceName = "VendorTools"; + public static async Task SingleVendorUpdate( IBungieClient bungieClient, DbManager db, @@ -105,11 +108,22 @@ public static async Task SingleVendorUpdate( success = true; } + catch (BungieHtmlResponseErrorException e) + { + logger.LogError(e, "Exception in {service}", ServiceName); + + var fileName = $"Logs/bungie-error-{DateTimeExtensions.GetCurrentTimestamp()}.html"; + + await File.WriteAllTextAsync(fileName, e.Html, stoppingToken); + + await DiscordTools.SendMessage(DiscordTools.WebhookChannel.Logs, + $"Exception in {ServiceName}:\n\n>>> {e.GetType()}: Logs saved to {fileName}"); + } catch (Exception e) { await DiscordTools.SendMessage(DiscordTools.WebhookChannel.Logs, $"**Exception in SingleVendorUpdate({vendorId}).**\n\n>>> **{e.GetType()}**: {e.Message}"); - logger.LogError(e, "Exception in {service}", "SingleVendorUpdate"); + logger.LogError(e, "Exception in {service}", ServiceName); } return success;