Skip to content

Commit

Permalink
Merge pull request #43 from devFelicity/develop
Browse files Browse the repository at this point in the history
fix(tasks): add html debug + db refresh attempt
  • Loading branch information
MoonieGZ authored Jan 2, 2024
2 parents ba4c1a3 + f808774 commit ff6d430
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 35 deletions.
8 changes: 4 additions & 4 deletions API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

<ItemGroup>
<PackageReference Include="Discord.Net.Webhook" Version="3.13.0" />
<PackageReference Include="DotNetBungieAPI" Version="2.12.2" />
<PackageReference Include="DotNetBungieAPI" Version="2.12.3" />
<PackageReference Include="DotNetBungieAPI.AspNet.Security.OAuth.Providers" Version="1.0.2" />
<PackageReference Include="DotNetBungieAPI.DefinitionProvider.Sqlite" Version="1.5.3" />
<PackageReference Include="DotNetBungieAPI.DefinitionProvider.Sqlite" Version="1.5.4" />
<PackageReference Include="DotNetBungieAPI.HashReferences" Version="1.22.1" />
<PackageReference Include="DotNetBungieAPI.Models" Version="2.11.3" />
<PackageReference Include="DotNetBungieAPI.Service.Abstractions" Version="2.11.1" />
<PackageReference Include="DotNetBungieAPI.Models" Version="2.11.4" />
<PackageReference Include="DotNetBungieAPI.Service.Abstractions" Version="2.11.2" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
25 changes: 20 additions & 5 deletions Tasks/UserRefresh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,17 +20,17 @@ public class UserRefresh(

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

while (!stoppingToken.IsCancellationRequested)
{
TaskSchedulerService.Tasks.First(t => t.Name == ServiceName).IsRunning = true;

try
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

var users = await db.Users.Include(u => u.BungieProfiles).ToListAsync(stoppingToken);
var bungieProfiles = users.SelectMany(u => u.BungieProfiles)
// TODO: .Where(u => u.NeverExpire)
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions Tasks/VendorsAdepts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public class VendorsAdepts(

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

// TODO: raise this to 5/10 minutes
await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken);

Expand All @@ -45,6 +40,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

try
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

var vendorUser = db.Users.Include(u => u.BungieProfiles)
.FirstOrDefault(x => x.Id == UserExtensions.SignId(Variables.OwnerId));

Expand Down
10 changes: 5 additions & 5 deletions Tasks/VendorsGunsmith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public class VendorsGunsmith(

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

// TODO: raise this to 5/10 minutes
await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken);

Expand All @@ -35,6 +30,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

try
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

var vendorUser = db.Users.Include(u => u.BungieProfiles)
.FirstOrDefault(x => x.Id == UserExtensions.SignId(Variables.OwnerId));

Expand Down
10 changes: 5 additions & 5 deletions Tasks/VendorsIronBanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public class VendorsIronBanner(

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

// TODO: raise this to 5/10 minutes
await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken);

Expand All @@ -36,6 +31,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

try
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

var userList = new List<VendorUser?>
{
db.VendorUsers.FirstOrDefault(x => x.VendorId == VendorId && x.Resets == 0 && x.Rank < 10),
Expand Down
10 changes: 5 additions & 5 deletions Tasks/VendorsTrials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public class VendorsTrials(

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

// TODO: raise this to 5/10 minutes
await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken);

Expand All @@ -36,6 +31,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

try
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

var userList = new List<VendorUser?>
{
db.VendorUsers.FirstOrDefault(x => x.VendorId == VendorId && x.Resets == 0 && x.Rank < 10),
Expand Down
10 changes: 5 additions & 5 deletions Tasks/VendorsWarTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public class VendorsWarTable(

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

// TODO: raise this to 5/10 minutes
await Task.Delay(DateTimeExtensions.GetRoundTimeSpan(2), stoppingToken);

Expand All @@ -35,6 +30,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

try
{
using var scope = services.CreateScope();
var db =
scope.ServiceProvider
.GetRequiredService<DbManager>();

var vendorUser = db.Users.Include(u => u.BungieProfiles)
.FirstOrDefault(x => x.Id == UserExtensions.SignId(Variables.BotId));

Expand Down
5 changes: 5 additions & 0 deletions Util/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public static DateTime GetRoundTime(DateTime currentTime, int roundMinutes)

return nextRoundTime;
}

public static long GetCurrentTimestamp()
{
return DateTimeOffset.Now.ToUnixTimeSeconds();
}
}
16 changes: 15 additions & 1 deletion Util/VendorTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> SingleVendorUpdate(
IBungieClient bungieClient,
DbManager db,
Expand Down Expand Up @@ -105,11 +108,22 @@ public static async Task<bool> 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;
Expand Down

0 comments on commit ff6d430

Please sign in to comment.