From e2ddc3c1d49f05bfcb0faed89dc11369181c76ba Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Wed, 22 Nov 2017 08:07:56 +0100 Subject: [PATCH] Fixed multiple responses? closes #1867, #1747 --- .../Modules/Administration/SelfCommands.cs | 2 +- NadekoBot.Core/Services/Impl/StatsService.cs | 2 +- NadekoBot.Core/Services/ShardsCoordinator.cs | 32 +++++++++---------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/NadekoBot.Core/Modules/Administration/SelfCommands.cs b/NadekoBot.Core/Modules/Administration/SelfCommands.cs index 38fe17de..893f495e 100644 --- a/NadekoBot.Core/Modules/Administration/SelfCommands.cs +++ b/NadekoBot.Core/Modules/Administration/SelfCommands.cs @@ -272,7 +272,7 @@ namespace NadekoBot.Modules.Administration return; } var pub = _cache.Redis.GetSubscriber(); - pub.Publish(_creds.RedisKey() + "_shardcoord_restart", + pub.Publish(_creds.RedisKey() + "_shardcoord_stop", JsonConvert.SerializeObject(_client.ShardId), StackExchange.Redis.CommandFlags.FireAndForget); await ReplyConfirmLocalized("shard_reconnecting", Format.Bold("#" + shardid)).ConfigureAwait(false); diff --git a/NadekoBot.Core/Services/Impl/StatsService.cs b/NadekoBot.Core/Services/Impl/StatsService.cs index 5cc91918..df9e4740 100644 --- a/NadekoBot.Core/Services/Impl/StatsService.cs +++ b/NadekoBot.Core/Services/Impl/StatsService.cs @@ -21,7 +21,7 @@ namespace NadekoBot.Core.Services.Impl private readonly IBotCredentials _creds; private readonly DateTime _started; - public const string BotVersion = "2.5.7"; + public const string BotVersion = "2.5.8"; public string Author => "Kwoth#2560"; public string Library => "Discord.Net"; diff --git a/NadekoBot.Core/Services/ShardsCoordinator.cs b/NadekoBot.Core/Services/ShardsCoordinator.cs index 68255e9f..b07cf31b 100644 --- a/NadekoBot.Core/Services/ShardsCoordinator.cs +++ b/NadekoBot.Core/Services/ShardsCoordinator.cs @@ -31,6 +31,14 @@ namespace NadekoBot.Core.Services } } + public bool TryPeek(out int id) + { + lock (_locker) + { + return _queue.TryPeek(out id); + } + } + public bool TryDequeue(out int id) { lock (_locker) @@ -90,7 +98,6 @@ namespace NadekoBot.Core.Services { //add it to the list of shards which should be started #if DEBUG - if (i > 0) _shardStartQueue.Enqueue(i); else @@ -102,7 +109,7 @@ namespace NadekoBot.Core.Services var msg = _defaultShardState.Clone(); msg.ShardId = i; //this is to avoid the shard coordinator thinking that - //the shard is unresponsive while startup up + //the shard is unresponsive while starting up var delay = 45; #if GLOBAL_NADEKO delay = 180; @@ -123,12 +130,7 @@ namespace NadekoBot.Core.Services OnDataReceived, CommandFlags.FireAndForget); - //restart is called when shzard should be stopped and then started again - sub.Subscribe(_key + "_shardcoord_restart", - OnRestart, - CommandFlags.FireAndForget); - - //called to kill the shard + //called to stop the shard, although the shard will start again when it finds out it's dead sub.Subscribe(_key + "_shardcoord_stop", OnStop, CommandFlags.FireAndForget); @@ -160,13 +162,6 @@ namespace NadekoBot.Core.Services try { p?.Dispose(); } catch { } } - private void OnRestart(RedisChannel ch, RedisValue data) - { - var shardId = JsonConvert.DeserializeObject(data); - OnStop(shardId); - _shardProcesses[shardId] = StartShard(shardId); - } - private void OnDataReceived(RedisChannel ch, RedisValue data) { var msg = JsonConvert.DeserializeObject(data); @@ -220,7 +215,7 @@ namespace NadekoBot.Core.Services do { //start a shard which is scheduled for start every 6 seconds - while (_shardStartQueue.TryDequeue(out var id)) + while (_shardStartQueue.TryPeek(out var id)) { // if the shard is on the waiting list again // remove it since it's starting up now @@ -234,8 +229,11 @@ namespace NadekoBot.Core.Services _log.Warn("Auto-restarting shard {0}", id); } var p = StartShard(id); - + var toRemove = _shardProcesses[id]; + try { toRemove?.Kill(); } catch { } + try { toRemove?.Dispose(); } catch { } _shardProcesses[id] = p; + _shardStartQueue.TryDequeue(out var __); await Task.Delay(6000).ConfigureAwait(false); } tsc.TrySetResult(true);