mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2024-10-02 20:13:13 +00:00
Added ICoordinator for bots which arent' sharded (the ones which won't be using NadekoBot.Coordinator)
This commit is contained in:
parent
77904a3b00
commit
44fdd4ff23
3 changed files with 90 additions and 13 deletions
|
@ -193,6 +193,10 @@ namespace NadekoBot.Coordinator
|
||||||
Arguments = string.Format(_config.ShardStartArgs,
|
Arguments = string.Format(_config.ShardStartArgs,
|
||||||
shardId,
|
shardId,
|
||||||
Environment.ProcessId),
|
Environment.ProcessId),
|
||||||
|
EnvironmentVariables =
|
||||||
|
{
|
||||||
|
{"NADEKOBOT_IS_COORDINATED", "1"}
|
||||||
|
}
|
||||||
// CreateNoWindow = true,
|
// CreateNoWindow = true,
|
||||||
// UseShellExecute = false,
|
// UseShellExecute = false,
|
||||||
});
|
});
|
||||||
|
|
|
@ -119,7 +119,7 @@ namespace NadekoBot
|
||||||
AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
|
AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
var s = new ServiceCollection()
|
var svcs = new ServiceCollection()
|
||||||
.AddSingleton<IBotCredentials>(Credentials)
|
.AddSingleton<IBotCredentials>(Credentials)
|
||||||
.AddSingleton(_db)
|
.AddSingleton(_db)
|
||||||
.AddSingleton(Client)
|
.AddSingleton(Client)
|
||||||
|
@ -139,25 +139,30 @@ namespace NadekoBot
|
||||||
.AddMusic()
|
.AddMusic()
|
||||||
;
|
;
|
||||||
|
|
||||||
s.AddHttpClient();
|
svcs.AddHttpClient();
|
||||||
s.AddHttpClient("memelist").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
|
svcs.AddHttpClient("memelist").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
|
||||||
{
|
{
|
||||||
AllowAutoRedirect = false
|
AllowAutoRedirect = false
|
||||||
});
|
});
|
||||||
|
|
||||||
s.LoadFrom(Assembly.GetAssembly(typeof(CommandHandler)));
|
svcs.LoadFrom(Assembly.GetAssembly(typeof(CommandHandler)));
|
||||||
|
|
||||||
// todo if sharded
|
if (Environment.GetEnvironmentVariable("NADEKOBOT_IS_COORDINATED") != "1")
|
||||||
s
|
{
|
||||||
.AddSingleton<ICoordinator, RemoteGrpcCoordinator>()
|
svcs.AddSingleton<ICoordinator, SingleProcessCoordinator>();
|
||||||
.AddSingleton<IReadyExecutor>(x => (IReadyExecutor)x.GetRequiredService<ICoordinator>());
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
svcs.AddSingleton<ICoordinator, RemoteGrpcCoordinator>()
|
||||||
|
.AddSingleton<IReadyExecutor>(x => (IReadyExecutor)x.GetRequiredService<ICoordinator>());
|
||||||
|
}
|
||||||
|
|
||||||
s.AddSingleton<IReadyExecutor>(x => x.GetService<SelfService>());
|
svcs.AddSingleton<IReadyExecutor>(x => x.GetService<SelfService>());
|
||||||
s.AddSingleton<IReadyExecutor>(x => x.GetService<CustomReactionsService>());
|
svcs.AddSingleton<IReadyExecutor>(x => x.GetService<CustomReactionsService>());
|
||||||
s.AddSingleton<IReadyExecutor>(x => x.GetService<RepeaterService>());
|
svcs.AddSingleton<IReadyExecutor>(x => x.GetService<RepeaterService>());
|
||||||
|
|
||||||
//initialize Services
|
//initialize Services
|
||||||
Services = s.BuildServiceProvider();
|
Services = svcs.BuildServiceProvider();
|
||||||
var commandHandler = Services.GetService<CommandHandler>();
|
var commandHandler = Services.GetService<CommandHandler>();
|
||||||
|
|
||||||
if (Client.ShardId == 0)
|
if (Client.ShardId == 0)
|
||||||
|
@ -166,7 +171,7 @@ namespace NadekoBot
|
||||||
}
|
}
|
||||||
|
|
||||||
//what the fluff
|
//what the fluff
|
||||||
commandHandler.AddServices(s);
|
commandHandler.AddServices(svcs);
|
||||||
_ = LoadTypeReaders(typeof(Bot).Assembly);
|
_ = LoadTypeReaders(typeof(Bot).Assembly);
|
||||||
|
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
|
|
68
src/NadekoBot/Services/Impl/SingleProcessCoordinator.cs
Normal file
68
src/NadekoBot/Services/Impl/SingleProcessCoordinator.cs
Normal file
|
@ -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<ShardStatus> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue