Exemplo n.º 1
0
 static public void SetAlarmState(EnumAlarmState state, TimeSpan?timeout = null)
 {
     if (timeout.HasValue)
     {
         AlarmStateTimeout = DateTime.UtcNow.Add(timeout.Value);
     }
     AlarmState = state;
 }
Exemplo n.º 2
0
        public static int Main(string[] args)
        {
            AlarmState = EnumAlarmState.On;

            try
            {
                Console.WriteLine($"Git commit: {clsVersion.GitCommitHash}");
                if (clsVersion.VersionChangeFlag)
                {
                    Console.WriteLine("NEW VERSION DETECTED");
                }

                Console.WriteLine($"ARGS={Environment.CommandLine}");
                BotURL = Environment.GetEnvironmentVariable("BOTURL");

                if (string.IsNullOrEmpty(BotURL))
                {
                    Console.WriteLine("'BOTURL' Google Spreadsheet URL missing.");
                    return((int)enumRunState.Error);
                }
                Console.WriteLine($"URL={BotURL}");

                Spreadsheet = new clsSpreadsheet(BotURL);
                var log = Spreadsheet.LoadSettings();
                Console.WriteLine(log);

                SettingsList.TryGetValue("BotName", out BotName);

                string value;
                if (SettingsList.TryGetValue("AlarmOffWarningMinutes", out value))
                {
                    uint.TryParse(value, out AlarmOffWarningMinutes);
                }

                string DiscordToken;
                if (!SettingsList.TryGetValue("Discord-Token", out DiscordToken))
                {
                    Console.WriteLine("Discord-Token not found");
                    return((int)enumRunState.Error);
                }

                clsEmail.GetSettings();

                Console.WriteLine("Starting monitoring");
                const int apiTimeout = 2000;
                const int loopWait   = 3000;

                DateTime LastBotStart    = DateTime.UtcNow;
                int      BotErrorCounter = 0;
                using (Bot = new BotClient(DiscordToken))
                {
                    Bot.RunAsync();
                    if (log.Contains("rror:"))
                    {
                        Bot.SendAlert($"```{log}```");
                    }

                    while (RunState == enumRunState.Run)
                    {
                        //Query every node.
                        foreach (var node in NodesList.Values.Where(x => x.Monitor))
                        {
                            node.GetHeightAsync(apiTimeout);
                        }

                        ApplicationHold.WaitOne(apiTimeout); //Wait for the timeout

                        foreach (var group in NodeGroupList.Values)
                        {
                            group.Monitor();
                        }

                        foreach (var network in NetworkList.Values)
                        {
                            network.CheckStall();
                        }

                        if (AlarmStateTimeout.HasValue)
                        {
                            if (AlarmStateTimeout.Value < DateTime.UtcNow)
                            {
                                AlarmState = EnumAlarmState.On;
                            }
                        }
                        else if (AlarmState == EnumAlarmState.Off && (DateTime.UtcNow - AlarmOffTime).TotalMinutes > (AlarmOffWarningMinutes * (AlarmOffWarningMultiplier + 1)))
                        {
                            Bot.Our_BotAlert.SendMessageAsync($"Warning! The alarm has been off {(DateTime.UtcNow - AlarmOffTime).TotalMinutes:0} minutes.  You might want to reset it.");
                            AlarmOffWarningMultiplier++;
                        }

                        AlarmManager.Process();

                        //Check the status of the Discord connection.  If it disconnects, it doesn't always restart.
                        if (!Bot.LastHeartbeatd.HasValue || (DateTime.UtcNow - Bot.LastHeartbeatd.Value).TotalSeconds > 90)
                        {
                            if ((DateTime.UtcNow - LastBotStart).TotalSeconds > 120)
                            {
                                Console.WriteLine("Discord not connected. Restarting...");
                                BotErrorCounter++;
                                if (BotErrorCounter == 2)
                                {
                                    clsEmail.EmailAlertList("Bot: Discord not connected.");
                                }
                                Bot._client.Dispose();
                                LastBotStart = DateTime.UtcNow;
                                Bot          = new BotClient(DiscordToken);
                                Bot.RunAsync();
                            }
                        }
                        else
                        {
                            BotErrorCounter = 0;
                        }

                        ApplicationHold.WaitOne(loopWait);
                    }

                    Console.WriteLine($"Exit Code: {RunState} ({(int)RunState})");

                    switch (RunState)
                    {
                    case enumRunState.Update:
                        Bot.Our_BotAlert.SendMessageAsync("Shutting down to update. Back soon. :grin:").Wait(1000);
                        break;

                    case enumRunState.Restart:
                        Bot.Our_BotAlert.SendMessageAsync("Shutting down to restart. :relieved:").Wait(1000);
                        break;

                    case enumRunState.MonoArgs:
                        Bot.Our_BotAlert.SendMessageAsync("Shutting down to restart in Debug mode. :spy:").Wait(1000);
                        break;

                    case enumRunState.Stop:
                        Bot.Our_BotAlert.SendMessageAsync("Goodbye! :sleeping:").Wait(1000);
                        break;
                    }
                }
                return((int)RunState);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR: {ex.Message}");
                return((int)enumRunState.Error);
            }
        }