Many IDisposable fixes. GlobalNadeko won't have file watchers for creds. Wallet simplified

This commit is contained in:
Kwoth
2022-03-21 15:33:18 +01:00
parent 4cf3bdb53a
commit b7d1fd1b47
25 changed files with 229 additions and 221 deletions

View File

@@ -14,7 +14,7 @@ public class CurrencyService : ICurrencyService, INService
public Task<IWallet> GetWalletAsync(ulong userId, CurrencyType type = CurrencyType.Default)
{
if (type == CurrencyType.Default)
return Task.FromResult<IWallet>(new DefaultWallet(userId, _db.GetDbContext()));
return Task.FromResult<IWallet>(new DefaultWallet(userId, _db));
throw new ArgumentOutOfRangeException(nameof(type));
}
@@ -29,7 +29,7 @@ public class CurrencyService : ICurrencyService, INService
{
foreach (var userId in userIds)
{
await using var wallet = await GetWalletAsync(userId);
var wallet = await GetWalletAsync(userId);
await wallet.Add(amount, txData);
}
@@ -68,7 +68,7 @@ public class CurrencyService : ICurrencyService, INService
long amount,
TxData txData)
{
await using var wallet = await GetWalletAsync(userId);
var wallet = await GetWalletAsync(userId);
await wallet.Add(amount, txData);
}
@@ -77,7 +77,7 @@ public class CurrencyService : ICurrencyService, INService
long amount,
TxData txData)
{
await using var wallet = await GetWalletAsync(user.Id);
var wallet = await GetWalletAsync(user.Id);
await wallet.Add(amount, txData);
}
@@ -86,7 +86,7 @@ public class CurrencyService : ICurrencyService, INService
long amount,
TxData txData)
{
await using var wallet = await GetWalletAsync(userId);
var wallet = await GetWalletAsync(userId);
return await wallet.Take(amount, txData);
}
@@ -95,7 +95,7 @@ public class CurrencyService : ICurrencyService, INService
long amount,
TxData txData)
{
await using var wallet = await GetWalletAsync(user.Id);
var wallet = await GetWalletAsync(user.Id);
return await wallet.Take(amount, txData);
}
}

View File

@@ -6,7 +6,7 @@ public static class CurrencyServiceExtensions
{
public static async Task<long> GetBalanceAsync(this ICurrencyService cs, ulong userId)
{
await using var wallet = await cs.GetWalletAsync(userId);
var wallet = await cs.GetWalletAsync(userId);
return await wallet.GetBalance();
}
@@ -18,8 +18,8 @@ public static class CurrencyServiceExtensions
string fromName,
string note)
{
await using var fromWallet = await cs.GetWalletAsync(fromId);
await using var toWallet = await cs.GetWalletAsync(toId);
var fromWallet = await cs.GetWalletAsync(fromId);
var toWallet = await cs.GetWalletAsync(toId);
var extra = new TxData("gift", fromName, note, fromId);

View File

@@ -7,48 +7,53 @@ namespace NadekoBot.Services.Currency;
public class DefaultWallet : IWallet
{
private readonly DbService _db;
public ulong UserId { get; }
private readonly NadekoContext _ctx;
public DefaultWallet(ulong userId, NadekoContext ctx)
public DefaultWallet(ulong userId, DbService db)
{
UserId = userId;
_ctx = ctx;
_db = db;
}
public Task<long> GetBalance()
=> _ctx.DiscordUser
.ToLinqToDBTable()
.Where(x => x.UserId == UserId)
.Select(x => x.CurrencyAmount)
.FirstOrDefaultAsync();
public async Task<long> GetBalance()
{
await using var ctx = _db.GetDbContext();
return await ctx.DiscordUser
.ToLinqToDBTable()
.Where(x => x.UserId == UserId)
.Select(x => x.CurrencyAmount)
.FirstOrDefaultAsync();
}
public async Task<bool> Take(long amount, TxData txData)
{
if (amount < 0)
throw new ArgumentOutOfRangeException(nameof(amount), "Amount to take must be non negative.");
var changed = await _ctx.DiscordUser
.Where(x => x.UserId == UserId && x.CurrencyAmount >= amount)
.UpdateAsync(x => new()
{
CurrencyAmount = x.CurrencyAmount - amount
});
await using var ctx = _db.GetDbContext();
var changed = await ctx.DiscordUser
.Where(x => x.UserId == UserId && x.CurrencyAmount >= amount)
.UpdateAsync(x => new()
{
CurrencyAmount = x.CurrencyAmount - amount
});
if (changed == 0)
return false;
await _ctx.CreateLinqToDbContext()
.InsertAsync(new CurrencyTransaction()
{
Amount = -amount,
Note = txData.Note,
UserId = UserId,
Type = txData.Type,
Extra = txData.Extra,
OtherId = txData.OtherId
});
await using var ctx2 = ctx.CreateLinqToDbContext();
await ctx2
.InsertAsync(new CurrencyTransaction()
{
Amount = -amount,
Note = txData.Note,
UserId = UserId,
Type = txData.Type,
Extra = txData.Extra,
OtherId = txData.OtherId
});
return true;
}
@@ -58,9 +63,11 @@ public class DefaultWallet : IWallet
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount), "Amount must be greater than 0.");
await using (var tran = await _ctx.Database.BeginTransactionAsync())
await using var ctx = _db.GetDbContext();
await using (var tran = await ctx.Database.BeginTransactionAsync())
{
var changed = await _ctx.DiscordUser
var changed = await ctx.DiscordUser
.Where(x => x.UserId == UserId)
.UpdateAsync(x => new()
{
@@ -69,7 +76,7 @@ public class DefaultWallet : IWallet
if (changed == 0)
{
await _ctx.DiscordUser
await ctx.DiscordUser
.ToLinqToDBTable()
.Value(x => x.UserId, UserId)
.Value(x => x.Username, "Unknown")
@@ -91,19 +98,7 @@ public class DefaultWallet : IWallet
OtherId = txData.OtherId
};
await _ctx.CreateLinqToDbContext()
.InsertAsync(ct);
}
public void Dispose()
{
_ctx.SaveChanges();
_ctx.Dispose();
}
public async ValueTask DisposeAsync()
{
await _ctx.SaveChangesAsync();
await _ctx.DisposeAsync();
await using var ctx2 = ctx.CreateLinqToDbContext();
await ctx2.InsertAsync(ct);
}
}

View File

@@ -1,6 +1,6 @@
namespace NadekoBot.Services.Currency;
public interface IWallet : IDisposable, IAsyncDisposable
public interface IWallet
{
public ulong UserId { get; }

View File

@@ -32,10 +32,9 @@ public class DbService
using var context = new NadekoContext(_options);
if (context.Database.GetPendingMigrations().Any())
{
var mContext = new NadekoContext(_migrateOptions);
using var mContext = new NadekoContext(_migrateOptions);
mContext.Database.Migrate();
mContext.SaveChanges();
mContext.Dispose();
}
context.Database.ExecuteSqlRaw("PRAGMA journal_mode=WAL");

View File

@@ -32,6 +32,7 @@ public sealed class BotCredsProvider : IBotCredsProvider
private readonly object _reloadLock = new();
private readonly IDisposable _changeToken;
public BotCredsProvider(int? totalShards = null)
{
@@ -52,9 +53,9 @@ public sealed class BotCredsProvider : IBotCredsProvider
_config = new ConfigurationBuilder().AddYamlFile(CredsPath, false, true)
.AddEnvironmentVariables("NadekoBot_")
.Build();
ChangeToken.OnChange(() => _config.GetReloadToken(), Reload);
#if !GLOBAL_NADEKO
_changeToken = ChangeToken.OnChange(() => _config.GetReloadToken(), Reload);
#endif
Reload();
}

View File

@@ -5,7 +5,7 @@ using System.Diagnostics;
namespace NadekoBot.Services;
public class StatsService : IStatsService, IReadyExecutor, INService, IDisposable
public sealed class StatsService : IStatsService, IReadyExecutor, INService, IDisposable
{
public const string BOT_VERSION = "4.0.4";