private void SaasRestartService_Click()

in 2LCS/Forms/MainForm.cs [2338:2400]


        private void SaasRestartService_Click(object sender, EventArgs e)
        {
            using var form = new ChooseService
            {
                AvailableServices = _httpClientHelper.GetServicesToRestart()
            };
            form.ShowDialog();
            if (form.Cancelled || (form.ServicesToRestart == null)) return;
            Cursor = Cursors.WaitCursor;

            StringBuilder log = new StringBuilder();
            foreach (DataGridViewRow row in saasDataGridView.SelectedRows)
            {
                foreach (var service in form.ServicesToRestart)
                {
                    var instance = (CloudHostedInstance)row.DataBoundItem;
                    log.AppendLine($"Validating ongoing actions of {instance.InstanceId} instance...");
                    ActionDetails actions;
                    var attempt = 1;
                    do
                    {
                        actions = _httpClientHelper.GetOngoingActionDetails(instance);
                        if (actions != null)
                        {
                            log.AppendLine($"Attempt {attempt}. Ongoing action found! Delaying next attempt for 30 seconds...");
                            log.AppendLine($"Action status: {actions.ActionStatusText}");
                            log.AppendLine($"Action type: {actions.ActionType}");
                            log.AppendLine($"Action name: {actions.Name}");
                            log.AppendLine($"Action start date: {actions.StartDate}");
                            System.Threading.Thread.Sleep(1000 * 30);
                            attempt++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    while (attempt <= 10);
                    if (actions == null)
                    {
                        log.AppendLine($"Restarting {service.Value} service of {instance.InstanceId} instance...");
                        var response = _httpClientHelper.RestartService(instance, service.Value);
                        log.AppendLine($"Result:  {response.Message}");
                        log.AppendLine();
                    }
                    else
                    {
                        log.AppendLine($" Previous ongoing action was not finished. Restart will not be executed.");
                    }
                }
            }
            Cursor = Cursors.Default;

            if (log.Length != 0)
            {
                using var logForm = new LogDisplay
                {
                    LogEntries = log.ToString(),
                    Text = $"Restarting service(s) log"
                };
                logForm.ShowDialog();
            }
        }