From 44fdd4ff23fb514f058bfffb766b5d05540d9f18 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 20 Jun 2021 10:46:19 +0200 Subject: [PATCH] Added ICoordinator for bots which arent' sharded (the ones which won't be using NadekoBot.Coordinator) --- .../Services/CoordinatorRunner.cs | 4 ++ src/NadekoBot/Bot.cs | 31 +++++---- .../Services/Impl/SingleProcessCoordinator.cs | 68 +++++++++++++++++++ 3 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 src/NadekoBot/Services/Impl/SingleProcessCoordinator.cs diff --git a/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs b/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs index 4f4f2bedd..d6afa9d87 100644 --- a/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs +++ b/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs @@ -193,6 +193,10 @@ namespace NadekoBot.Coordinator Arguments = string.Format(_config.ShardStartArgs, shardId, Environment.ProcessId), + EnvironmentVariables = + { + {"NADEKOBOT_IS_COORDINATED", "1"} + } // CreateNoWindow = true, // UseShellExecute = false, }); diff --git a/src/NadekoBot/Bot.cs b/src/NadekoBot/Bot.cs index e4962ffe9..ec5ecdc4d 100644 --- a/src/NadekoBot/Bot.cs +++ b/src/NadekoBot/Bot.cs @@ -119,7 +119,7 @@ namespace NadekoBot AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray(); } - var s = new ServiceCollection() + var svcs = new ServiceCollection() .AddSingleton(Credentials) .AddSingleton(_db) .AddSingleton(Client) @@ -139,25 +139,30 @@ namespace NadekoBot .AddMusic() ; - s.AddHttpClient(); - s.AddHttpClient("memelist").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + svcs.AddHttpClient(); + svcs.AddHttpClient("memelist").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false }); - s.LoadFrom(Assembly.GetAssembly(typeof(CommandHandler))); + svcs.LoadFrom(Assembly.GetAssembly(typeof(CommandHandler))); - // todo if sharded - s - .AddSingleton() - .AddSingleton(x => (IReadyExecutor)x.GetRequiredService()); + if (Environment.GetEnvironmentVariable("NADEKOBOT_IS_COORDINATED") != "1") + { + svcs.AddSingleton(); + } + else + { + svcs.AddSingleton() + .AddSingleton(x => (IReadyExecutor)x.GetRequiredService()); + } - s.AddSingleton(x => x.GetService()); - s.AddSingleton(x => x.GetService()); - s.AddSingleton(x => x.GetService()); + svcs.AddSingleton(x => x.GetService()); + svcs.AddSingleton(x => x.GetService()); + svcs.AddSingleton(x => x.GetService()); //initialize Services - Services = s.BuildServiceProvider(); + Services = svcs.BuildServiceProvider(); var commandHandler = Services.GetService(); if (Client.ShardId == 0) @@ -166,7 +171,7 @@ namespace NadekoBot } //what the fluff - commandHandler.AddServices(s); + commandHandler.AddServices(svcs); _ = LoadTypeReaders(typeof(Bot).Assembly); sw.Stop(); diff --git a/src/NadekoBot/Services/Impl/SingleProcessCoordinator.cs b/src/NadekoBot/Services/Impl/SingleProcessCoordinator.cs new file mode 100644 index 000000000..65cd2f372 --- /dev/null +++ b/src/NadekoBot/Services/Impl/SingleProcessCoordinator.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading.Tasks; +using Discord.WebSocket; +using NadekoBot.Core.Services; +using Serilog; + +namespace NadekoBot.Services +{ + public class SingleProcessCoordinator : ICoordinator + { + private readonly IBotCredentials _creds; + private readonly DiscordSocketClient _client; + + public SingleProcessCoordinator(IBotCredentials creds, DiscordSocketClient client) + { + _creds = creds; + _client = client; + } + public bool RestartBot() + { + if (string.IsNullOrWhiteSpace(_creds.RestartCommand?.Cmd) + || string.IsNullOrWhiteSpace(_creds.RestartCommand?.Args)) + { + Log.Error("You must set RestartCommand.Cmd and RestartCommand.Args in credentials.json"); + return false; + } + + Process.Start(_creds.RestartCommand.Cmd, _creds.RestartCommand.Args); + _ = Task.Run(async () => + { + await Task.Delay(2000); + Die(); + }); + return true; + } + + public void Die() + { + Environment.Exit(5); + } + + public bool RestartShard(int shardId) + { + return RestartBot(); + } + + public IList GetAllShardStatuses() + { + return new[] + { + new ShardStatus() + { + ConnectionState = _client.ConnectionState, + GuildCount = _client.Guilds.Count, + LastUpdate = DateTime.UtcNow, + ShardId = _client.ShardId + } + }; + } + + public int GetGuildCount() + { + return _client.Guilds.Count; + } + } +} \ No newline at end of file