2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services.Impl;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.ShardCom;
|
2017-10-10 16:24:36 +00:00
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-10-13 04:14:54 +00:00
|
|
|
|
namespace NadekoBot.Core.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-10-10 16:24:36 +00:00
|
|
|
|
private readonly string _key;
|
2017-07-18 02:25:30 +00:00
|
|
|
|
private readonly Process[] _shardProcesses;
|
2017-06-22 21:59:54 +00:00
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly Logger _log;
|
2017-07-05 15:38:38 +00:00
|
|
|
|
private readonly int _curProcessId;
|
2017-10-10 16:24:36 +00:00
|
|
|
|
private readonly ConnectionMultiplexer _redis;
|
|
|
|
|
private ShardComMessage _defaultShardState;
|
2017-07-05 15:38:38 +00:00
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
public ShardsCoordinator()
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-10-10 16:24:36 +00:00
|
|
|
|
//load main stuff
|
2017-06-19 13:42:10 +00:00
|
|
|
|
LogSetup.SetupLogger();
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-07-17 19:42:36 +00:00
|
|
|
|
_creds = new BotCredentials();
|
2017-10-13 00:21:39 +00:00
|
|
|
|
|
|
|
|
|
_log.Info("Starting NadekoBot v" + StatsService.BotVersion);
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_key = _creds.RedisKey();
|
2017-10-13 00:21:39 +00:00
|
|
|
|
_redis = ConnectionMultiplexer.Connect("127.0.0.1");
|
2017-10-09 22:04:02 +00:00
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
//setup initial shard statuses
|
|
|
|
|
_defaultShardState = new ShardComMessage()
|
|
|
|
|
{
|
|
|
|
|
ConnectionState = Discord.ConnectionState.Disconnected,
|
|
|
|
|
Guilds = 0,
|
|
|
|
|
Time = DateTime.Now - TimeSpan.FromMinutes(1)
|
|
|
|
|
};
|
|
|
|
|
var db = _redis.GetDatabase();
|
|
|
|
|
_shardProcesses = new Process[_creds.TotalShards];
|
|
|
|
|
for (int i = 0; i < _creds.TotalShards; i++)
|
2017-10-09 22:04:02 +00:00
|
|
|
|
{
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_defaultShardState.ShardId = i;
|
2017-10-15 08:20:49 +00:00
|
|
|
|
db.ListRightPush(_key + "_shardstats",
|
2017-10-10 16:24:36 +00:00
|
|
|
|
JsonConvert.SerializeObject(_defaultShardState),
|
2017-10-15 08:20:49 +00:00
|
|
|
|
flags: CommandFlags.FireAndForget);
|
2017-10-09 22:04:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_curProcessId = Process.GetCurrentProcess().Id;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_redis = ConnectionMultiplexer.Connect("127.0.0.1");
|
|
|
|
|
var sub = _redis.GetSubscriber();
|
|
|
|
|
sub.Subscribe(_key + "_shardcoord_send",
|
|
|
|
|
OnDataReceived,
|
|
|
|
|
CommandFlags.FireAndForget);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
sub.Subscribe(_key + "_shardcoord_restart",
|
|
|
|
|
OnRestart,
|
|
|
|
|
CommandFlags.FireAndForget);
|
2017-07-05 15:38:38 +00:00
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
sub.Subscribe(_key + "_shardcoord_stop",
|
|
|
|
|
OnStop,
|
|
|
|
|
CommandFlags.FireAndForget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnStop(RedisChannel ch, RedisValue data)
|
|
|
|
|
{
|
|
|
|
|
var shardId = JsonConvert.DeserializeObject<int>(data);
|
|
|
|
|
var db = _redis.GetDatabase();
|
|
|
|
|
_defaultShardState.ShardId = shardId;
|
2017-10-15 08:20:49 +00:00
|
|
|
|
db.ListSetByIndex(_key + "_shardstats",
|
2017-10-10 16:24:36 +00:00
|
|
|
|
shardId,
|
|
|
|
|
JsonConvert.SerializeObject(_defaultShardState),
|
|
|
|
|
CommandFlags.FireAndForget);
|
|
|
|
|
var p = _shardProcesses[shardId];
|
|
|
|
|
_shardProcesses[shardId] = null;
|
|
|
|
|
try { p?.Kill(); } catch { }
|
|
|
|
|
try { p?.Dispose(); } catch { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnRestart(RedisChannel ch, RedisValue data)
|
|
|
|
|
{
|
|
|
|
|
OnStop(ch, data);
|
|
|
|
|
var shardId = JsonConvert.DeserializeObject<int>(data);
|
|
|
|
|
_shardProcesses[shardId] = StartShard(shardId);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
private void OnDataReceived(RedisChannel ch, RedisValue data)
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-10-10 16:24:36 +00:00
|
|
|
|
var msg = JsonConvert.DeserializeObject<ShardComMessage>(data);
|
|
|
|
|
if (msg == null)
|
|
|
|
|
return;
|
|
|
|
|
var db = _redis.GetDatabase();
|
2017-10-15 08:20:49 +00:00
|
|
|
|
db.ListSetByIndex(_key + "_shardstats",
|
2017-10-10 16:24:36 +00:00
|
|
|
|
msg.ShardId,
|
|
|
|
|
data,
|
|
|
|
|
CommandFlags.FireAndForget);
|
|
|
|
|
if (msg.ConnectionState == Discord.ConnectionState.Disconnected
|
|
|
|
|
|| msg.ConnectionState == Discord.ConnectionState.Disconnecting)
|
|
|
|
|
{
|
|
|
|
|
_log.Error("!!! SHARD {0} IS IN {1} STATE !!!", msg.ShardId, msg.ConnectionState.ToString());
|
|
|
|
|
}
|
|
|
|
|
return;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 02:23:11 +00:00
|
|
|
|
public async Task RunAsync()
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-10-15 07:39:46 +00:00
|
|
|
|
int i = 0;
|
|
|
|
|
#if DEBUG
|
|
|
|
|
i = 1;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
for (; i < _creds.TotalShards; i++)
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-10-10 16:24:36 +00:00
|
|
|
|
var p = StartShard(i);
|
|
|
|
|
|
|
|
|
|
_shardProcesses[i] = p;
|
|
|
|
|
await Task.Delay(6000);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
private Process StartShard(int shardId)
|
|
|
|
|
{
|
|
|
|
|
return Process.Start(new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = _creds.ShardRunCommand,
|
|
|
|
|
Arguments = string.Format(_creds.ShardRunArguments, shardId, _curProcessId, "")
|
|
|
|
|
});
|
|
|
|
|
// last "" in format is for backwards compatibility
|
|
|
|
|
// because current startup commands have {2} in them probably
|
|
|
|
|
}
|
|
|
|
|
|
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-10-10 16:24:36 +00:00
|
|
|
|
foreach (var p in _shardProcesses)
|
|
|
|
|
{
|
|
|
|
|
try { p.Kill(); } catch { }
|
|
|
|
|
try { p.Dispose(); } catch { }
|
|
|
|
|
}
|
|
|
|
|
return;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
2017-07-06 17:30:22 +00:00
|
|
|
|
|
|
|
|
|
await Task.Delay(-1);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|