Packages can be loaded/unloaded. IUnloadableService interface added whose method Unload, if service implements it, will be called when the module is unloaded.

This commit is contained in:
Master Kwoth
2017-10-05 00:51:12 +02:00
parent 599245b1ca
commit 33ac43e1b5
74 changed files with 866 additions and 520 deletions

View File

@ -4,8 +4,6 @@ using NadekoBot.Services.Database.Models;
using Discord;
using NadekoBot.Modules.Xp.Common;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace NadekoBot.Modules.Xp.Services
{

View File

@ -26,7 +26,7 @@ using ImageSharp.Drawing.Brushes;
namespace NadekoBot.Modules.Xp.Services
{
public class XpService : INService
public class XpService : INService, IUnloadableService
{
private enum NotifOf { Server, Global } // is it a server level-up or global level-up notification
@ -55,7 +55,9 @@ namespace NadekoBot.Modules.Xp.Services
private readonly ConcurrentQueue<UserCacheItem> _addMessageXp
= new ConcurrentQueue<UserCacheItem>();
private readonly Timer updateXpTimer;
private readonly Timer _updateXpTimer;
private readonly CancellationTokenSource _clearRewardTimerTokenSource;
private readonly Task _clearRewardTimer;
private readonly HttpClient http = new HttpClient();
private FontFamily _usernameFontFamily;
private FontFamily _clubFontFamily;
@ -115,7 +117,7 @@ namespace NadekoBot.Modules.Xp.Services
_cmd.OnMessageNoTrigger += _cmd_OnMessageNoTrigger;
updateXpTimer = new Timer(async _ =>
_updateXpTimer = new Timer(async _ =>
{
try
{
@ -241,19 +243,20 @@ namespace NadekoBot.Modules.Xp.Services
_log.Warn(ex);
}
}, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
_clearRewardTimerTokenSource = new CancellationTokenSource();
var token = _clearRewardTimerTokenSource.Token;
//just a first line, in order to prevent queries. But since other shards can try to do this too,
//i'll check in the db too.
var clearRewardTimer = Task.Run(async () =>
_clearRewardTimer = Task.Run(async () =>
{
while (true)
while (!token.IsCancellationRequested)
{
_rewardedUsers.Clear();
await Task.Delay(TimeSpan.FromMinutes(_bc.BotConfig.XpMinutesTimeout));
}
});
}, token);
}
public IEnumerable<XpRoleReward> GetRoleRewards(ulong id)
@ -751,5 +754,17 @@ namespace NadekoBot.Modules.Xp.Services
return new PathCollection(cornerToptLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight);
}
public Task Unload()
{
_cmd.OnMessageNoTrigger -= _cmd_OnMessageNoTrigger;
if (!_clearRewardTimerTokenSource.IsCancellationRequested)
_clearRewardTimerTokenSource.Cancel();
_updateXpTimer.Change(Timeout.Infinite, Timeout.Infinite);
_clearRewardTimerTokenSource.Dispose();
return Task.CompletedTask;
}
}
}