Skip to content

Commit

Permalink
Fix booster command sometimes becoming unresponsive
Browse files Browse the repository at this point in the history
  • Loading branch information
Citrinate committed Sep 7, 2024
1 parent 72592b3 commit b526584
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 5 additions & 1 deletion BoosterManager/Boosters/BoosterQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using BoosterManager.Localization;

namespace BoosterManager {
internal sealed class BoosterQueue {
internal sealed class BoosterQueue : IDisposable {
private readonly Bot Bot;
private readonly Timer Timer;
private readonly ConcurrentHashSet<Booster> Boosters = new(new BoosterComparer());
Expand Down Expand Up @@ -37,6 +37,10 @@ internal BoosterQueue(Bot bot) {
Timeout.Infinite
);
}

public void Dispose() {
Timer.Dispose();
}

internal void Start() {
Utilities.InBackground(async() => await Run().ConfigureAwait(false));
Expand Down
13 changes: 7 additions & 6 deletions BoosterManager/Handlers/BoosterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Linq;

namespace BoosterManager {
internal sealed class BoosterHandler {
internal sealed class BoosterHandler : IDisposable {
private readonly Bot Bot;
internal BoosterDatabase BoosterDatabase { get; private set; }
internal readonly BoosterQueue BoosterQueue;
Expand All @@ -24,14 +24,15 @@ private BoosterHandler(Bot bot, BoosterDatabase boosterDatabase) {
BoosterQueue = new BoosterQueue(Bot);
}

public void Dispose() {
BoosterQueue.Dispose();
}

internal static void AddHandler(Bot bot, BoosterDatabase boosterDatabase) {
if (BoosterHandlers.ContainsKey(bot.BotName)) {
// Bot's config was reloaded, cancel and then restore jobs
BoosterHandlers[bot.BotName].CancelBoosterJobs();
BoosterHandlers[bot.BotName].BoosterDatabase = boosterDatabase;
BoosterHandlers[bot.BotName].RestoreBoosterJobs();

return;
BoosterHandlers[bot.BotName].Dispose();
BoosterHandlers.TryRemove(bot.BotName, out BoosterHandler? _);
}

BoosterHandler handler = new BoosterHandler(bot, boosterDatabase);
Expand Down

0 comments on commit b526584

Please sign in to comment.