Ejemplo n.º 1
0
 private void CheckBatchExecutionErrors(DataGridViewRow row)
 {
     try
     {
         string batchStatus     = DgvUtils.GetRowValue(ref row, WUCollums.Status).ToString();
         string operationResult = DgvUtils.GetRowValue(ref row, WUCollums.OperationResults).ToString();
         if (string.Equals(batchStatus, "ThreadError", StringComparison.CurrentCultureIgnoreCase))
         {
             throw new Exception($"BatchExecutionError: {operationResult}");
         }
     }
     catch { }
 }
Ejemplo n.º 2
0
        private void Act_InstallUpdatesBatchExecutor(object rowObject)
        {
            bool            isRebootRequired = false;
            DateTime        lastReboot       = new DateTime();
            DataGridViewRow row        = (DataGridViewRow)rowObject;
            string          host       = row.Cells["Host"].Value.ToString();
            string          operResult = string.Empty;
            Stopwatch       sw         = new Stopwatch();

            sw.Start();
            try
            {
                int minutesRebootLastRebootCheck = (int)numUpDownMinutesReboot.Value;
                int rebootCheckAttempts          = (int)numUpDownAttemptsNumber.Value;

                // Failover Cluster Services
                if (chkBoxCluster.Checked)
                {
                    DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Moving_ClusterResources (1/5)");
                    osManager.StartHostReadiness(host, ref row, chkBoxClusterResources.Checked);
                    bool isClustered = Convert.ToBoolean(DgvUtils.GetRowValue(ref row, WUCollums.Cluster));
                    if (isClustered)
                    {
                        DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Executing remote powershell to move resources...");
                        osManager.FailOverClusterNode(host);
                    }
                }

                // Install Updates
                DgvUtils.SetRowStyleForeColor(ref row, WUCollums.Status, Color.Black);
                DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Inst_Updates (2/5)");

                Act_InstallUpdatesExecutor(row);
                //On error it stops the batch
                CheckBatchExecutionErrors(row);

                isRebootRequired = Convert.ToBoolean(DgvUtils.GetRowValue(ref row, WUCollums.RebootRequired));
                int updatesInstalled = Convert.ToInt32(DgvUtils.GetRowValue(ref row, WUCollums.Updates));

                // Reboot
                DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Rebooting (3/5)");
                if (isRebootRequired)
                {
                    int    attempts           = 0;
                    string errorRebootMessage = string.Empty;
                    osManager.StartReboot(host, ref row);
                    pinger.BeginStart(host, row);

                    // Testing server when it comes online
                    do
                    {
                        //Wait 30 seconds
                        Thread.Sleep(30000);
                        if (attempts <= rebootCheckAttempts)
                        {
                            try
                            {
                                lastReboot = osManager.GetLastBootDateTimeObject(host, ref row);
                                DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, string.Empty);
                                DgvUtils.SetRowValue(ref row, WUCollums.LastBoot, lastReboot.ToString("dd/MM/yyyy HH:mm"));
                            }
                            catch
                            {
                                // returns a generic date just to keep the execution
                                lastReboot = new DateTime(2000, 1, 1);
                            }
                        }
                        else
                        {
                            errorRebootMessage = $"Number of attempts has been reached";
                            break;
                        }

                        attempts++;

                        DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, $"Host has not completed the reboot yet...(attempt: {attempts})");

                        // Check if the last boot time attribute
                    } while ((int)(DateTime.Now - lastReboot).TotalMinutes >= minutesRebootLastRebootCheck);

                    // Stop the execution when it has reached the attempts limit
                    if (!string.IsNullOrEmpty(errorRebootMessage))
                    {
                        throw new Exception(errorRebootMessage);
                    }
                    DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Server Online");

                    pinger.BeginStop(row);

                    osManager.StartHostReadiness(host, ref row, chkBoxClusterResources.Checked);
                }
                else
                {
                    DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, "Reboot not required");
                }

                // Count Updates
                if (updatesInstalled != 0)
                {
                    DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Count_Updates (4/5)");
                    // Give more time to rpc services start
                    Thread.Sleep(40000);
                    Act_CountUpdatesExecutor(row);
                    //On error it stops the batch
                    CheckBatchExecutionErrors(row);
                }

                DgvUtils.SetRowValue(ref row, WUCollums.BatchStep, "Finished (5/5)");
            }
            catch (Exception ex)
            {
                DgvUtils.SetRowValue(ref row, WUCollums.Status, "ThreadError");
                DgvUtils.SetRowStyleForeColor(ref row, WUCollums.Status, Color.Red);
                DgvUtils.SetRowValue(ref row, WUCollums.OperationResults, ex.Message);
            }
            finally
            {
                sw.Stop();
                operResult = DgvUtils.GetRowValue(ref row, WUCollums.OperationResults).ToString();
                DgvUtils.SetRowValue(ref row, WUCollums.OperationResults,
                                     string.Format("Duration: {0} min  {1}", (int)sw.Elapsed.Duration().TotalMinutes, operResult));

                if (semaphore.Release() == (int)numUpDownTreads.Value)
                {
                    semaphore.Dispose();
                }

                this.Sys_RemoveThreadRow(ref row);
            }
        }