示例#1
0
        public void Nanny()
        {
            Thread.Sleep(5000);
            if (ConfigObject.Bots.Any(bot => !bot.TMDisabled))
            {
                Balancer.Init();
            }
            Dictionary <string, DateTime> lastRestart = new Dictionary <string, DateTime>();
            TimeSpan restartInterval = new TimeSpan(0, 2, 0);

            while (!disposed)
            {
                try {
                    foreach (RunningBot bot in botProcs)
                    {
                        if (bot.IsRunning && bot.TheBot.WantToRestart())
                        {
                            DateTime now = DateTime.Now;
                            if (lastRestart.TryGetValue(bot.BotConfig.Username, out DateTime dt))
                            {
                                TimeSpan timeGone = now.Subtract(dt);
                                if (restartInterval.CompareTo(timeGone) > 0)
                                {
                                    mongoLog.ApiError($"Nanny is waiting {restartInterval.Subtract(timeGone).TotalSeconds} seconds");
                                    VK.Alert($"Nanny is waiting {restartInterval.Subtract(timeGone).TotalSeconds} seconds");
                                    Thread.Sleep(restartInterval.Subtract(timeGone));
                                }
                            }
                            mongoLog.ApiError($"Nanny is restarting bot {bot.BotConfig.Username}");
                            VK.Alert($"Nanny is restarting bot {bot.BotConfig.Username}");
                            RestartBot(bot.BotConfig.Username);
                            lastRestart[bot.BotConfig.Username] = DateTime.Now;
                            Thread.Sleep(100);
                        }
                    }
                } catch (Exception ex) {
                    mongoLog.Error($"Няне больно =(\n{ex.Message}{ex.StackTrace}");
                    VK.Alert($"Няне больно =(\n{ex.Message}{ex.StackTrace}");
                } finally {
                    Thread.Sleep(5000);
                }
            }
        }