2017-09-29 22:46:33 +00:00
|
|
|
|
using NadekoBot.Services.Impl;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.ShardCom;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-09-29 22:46:33 +00:00
|
|
|
|
namespace NadekoBot.Services
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
|
|
|
|
public class ShardsCoordinator
|
|
|
|
|
{
|
2017-07-17 19:42:36 +00:00
|
|
|
|
private readonly BotCredentials _creds;
|
2017-07-18 02:25:30 +00:00
|
|
|
|
private readonly Process[] _shardProcesses;
|
2017-06-21 21:12:24 +00:00
|
|
|
|
public ShardComMessage[] Statuses { get; }
|
2017-06-22 21:59:54 +00:00
|
|
|
|
public int GuildCount => Statuses.ToArray()
|
|
|
|
|
.Where(x => x != null)
|
|
|
|
|
.Sum(x => x.Guilds);
|
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly Logger _log;
|
|
|
|
|
private readonly ShardComServer _comServer;
|
2017-07-05 15:38:38 +00:00
|
|
|
|
private readonly int _curProcessId;
|
|
|
|
|
|
2017-10-09 22:04:02 +00:00
|
|
|
|
public ShardsCoordinator(IDataCache cache)
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
|
|
|
|
LogSetup.SetupLogger();
|
2017-07-17 19:42:36 +00:00
|
|
|
|
_creds = new BotCredentials();
|
2017-07-18 02:25:30 +00:00
|
|
|
|
_shardProcesses = new Process[_creds.TotalShards];
|
2017-07-17 19:42:36 +00:00
|
|
|
|
Statuses = new ShardComMessage[_creds.TotalShards];
|
2017-10-09 22:04:02 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Statuses.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Statuses[i] = new ShardComMessage();
|
|
|
|
|
var s = Statuses[i];
|
|
|
|
|
s.ConnectionState = Discord.ConnectionState.Disconnected;
|
|
|
|
|
s.Guilds = 0;
|
|
|
|
|
s.ShardId = i;
|
|
|
|
|
s.Time = DateTime.Now - TimeSpan.FromMinutes(1);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2017-10-09 22:04:02 +00:00
|
|
|
|
_comServer = new ShardComServer(cache);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
_comServer.Start();
|
|
|
|
|
|
|
|
|
|
_comServer.OnDataReceived += _comServer_OnDataReceived;
|
2017-07-05 15:38:38 +00:00
|
|
|
|
|
|
|
|
|
_curProcessId = Process.GetCurrentProcess().Id;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task _comServer_OnDataReceived(ShardComMessage msg)
|
|
|
|
|
{
|
|
|
|
|
Statuses[msg.ShardId] = msg;
|
|
|
|
|
if (msg.ConnectionState == Discord.ConnectionState.Disconnected || msg.ConnectionState == Discord.ConnectionState.Disconnecting)
|
2017-06-21 21:12:24 +00:00
|
|
|
|
_log.Error("!!! SHARD {0} IS IN {1} STATE", msg.ShardId, msg.ConnectionState.ToString());
|
2017-06-19 13:42:10 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 02:23:11 +00:00
|
|
|
|
public async Task RunAsync()
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-07-17 19:42:36 +00:00
|
|
|
|
for (int i = 1; i < _creds.TotalShards; i++)
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
|
|
|
|
var p = Process.Start(new ProcessStartInfo()
|
|
|
|
|
{
|
2017-07-17 19:42:36 +00:00
|
|
|
|
FileName = _creds.ShardRunCommand,
|
2017-10-09 22:04:02 +00:00
|
|
|
|
Arguments = string.Format(_creds.ShardRunArguments, i, _curProcessId, "")
|
2017-06-19 13:42:10 +00:00
|
|
|
|
});
|
2017-10-09 22:04:02 +00:00
|
|
|
|
// last "" in format is for backwards compatibility
|
|
|
|
|
// because current startup commands have {2} in them probably
|
2017-06-24 05:23:59 +00:00
|
|
|
|
await Task.Delay(5000);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 02:23:11 +00:00
|
|
|
|
public async Task RunAndBlockAsync()
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-06-20 02:23:11 +00:00
|
|
|
|
await RunAsync().ConfigureAwait(false);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Error(ex);
|
|
|
|
|
}
|
2017-07-06 17:30:22 +00:00
|
|
|
|
|
|
|
|
|
await Task.Delay(-1);
|
2017-07-18 02:25:30 +00:00
|
|
|
|
foreach (var p in _shardProcesses)
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
|
|
|
|
try { p.Kill(); } catch { }
|
|
|
|
|
try { p.Dispose(); } catch { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|