private ServerStateManager() { client = new AmazonEC2Client(new BasicAWSCredentials(BotConfig.GetCachedConfig().Aws.EC2AccessKey, BotConfig.GetCachedConfig().Aws.EC2AccessSecret), RegionEndpoint.GetBySystemName(BotConfig.GetCachedConfig().Aws.Region)); }
public async Task StopServer() { while (true) { await Task.Delay((int)BotConfig.GetCachedConfig().PollingDelay); MineStat ms = new MineStat(BotConfig.GetCachedConfig().Minecraft.MinecraftServerIP, BotConfig.GetCachedConfig().Minecraft.MinecraftServerPort); Console.WriteLine("Current player count: " + ms.CurrentPlayers); if (ms.CurrentPlayers == "0") { Console.WriteLine("Server player is count is zero. Sleeping, and checking again."); await Task.Delay((int)BotConfig.GetCachedConfig().StopServerDelay); ms = new MineStat(BotConfig.GetCachedConfig().Minecraft.MinecraftServerIP, BotConfig.GetCachedConfig().Minecraft.MinecraftServerPort); if (ms.ServerUp && ms.CurrentPlayers == "0") { Console.WriteLine("Player count is still zero. Stopping."); await Embeds.EditMessage(Embeds.ServerStopping()); var stopRequest = new StopInstancesRequest(); var request = new StopInstancesRequest(); request.InstanceIds = new List <string>() { BotConfig.GetCachedConfig().Aws.EC2InstanceId }; request.Force = false; var response = await client.StopInstancesAsync(request); ushort pollCounter = 0; while (pollCounter < BotConfig.GetCachedConfig().Aws.MaxInstanceStartAttempts) { pollCounter++; await Task.Delay(5000); var serverState = await this.GetState(); if (serverState.InstanceState.Code == 80) { break; } } await Embeds.EditMessage(Embeds.ServerStopped()); break; } } } IsWorking = false; }
public async Task StartServer() { Console.WriteLine("Starting server."); IsWorking = true; await Embeds.EditMessage(Embeds.ServerStarting()); var request = new StartInstancesRequest(); request.InstanceIds = new List <string>() { BotConfig.GetCachedConfig().Aws.EC2InstanceId }; var response = await client.StartInstancesAsync(request); bool suc = await AwaitServerStart(); if (suc) { while (true) { await Task.Delay((int)BotConfig.GetCachedConfig().PollingDelay); try { MineStat ms = new MineStat(BotConfig.GetCachedConfig().Minecraft.MinecraftServerIP, BotConfig.GetCachedConfig().Minecraft.MinecraftServerPort); if (ms.ServerUp) { Console.WriteLine("MC Server up."); break; } Console.WriteLine("MC Server is not up."); } catch { Console.WriteLine("Polling Minecraft server to see if it is online.. resulted in error."); } } await Embeds.EditMessage(Embeds.ServerStarted()); await StopServer(); } else { Console.WriteLine("Server start failed due to maxing out maxInstanceStartAttempts."); return; } }