Example #1
0
        internal static async Task <List <MachineDataCenterTuple> > GetMachinesByDataCenter(ILogger log, IonosAPI ionos, string[] dataCenterIds)
        {
            var result = new List <MachineDataCenterTuple>();

            foreach (string id in dataCenterIds)
            {
                var machines = await IonosHelper.GetMachines(log, ionos, id);

                foreach (IonosMachine m in machines)
                {
                    result.Add(new MachineDataCenterTuple(id, m));
                }
            }
            return(result);
        }
Example #2
0
        internal static async void EnsureMinimumRunningServers(ILogger log, IonosAPI ionos, string[] ionosDataCenterIds)
        {
            var machines = await IonosHelper.GetMachinesByDataCenter(log, ionos, ionosDataCenterIds);

            var runningMachines = machines.Where(m => m.Machine.Properties.VmState != "SHUTOFF").ToList();
            var toStart         = TimeHelper.GetMinimumActiveMachines() - runningMachines.Count;
            var toStartMachines = machines.Where(m => m.Machine.Properties.VmState == "SHUTOFF")
                                  .Take(toStart)
                                  .Take(MAX_SERVER_INCREASE)
                                  .ToList();

            if (toStartMachines.Count > 0)
            {
                log.LogInformation($"Found '{toStartMachines.Count}' machines to start");
                toStartMachines.AsParallel()
                .ForAll(async m => await ionos.TurnMachineOn(m.Machine.Id, m.DataCenter).ConfigureAwait(false));
            }
            else
            {
                log.LogInformation("Found '0' machines to start");
            }
        }