private void WorkerUpdateIngestManifest_DoWork(object sender, DoWorkEventArgs e)
        {
            Debug.WriteLine("WorkerUpdateIngestManifest_DoWork");
            BackgroundWorker worker = sender as BackgroundWorker;

            Mainform myform = (Mainform)this.FindForm();

            while (true)
            {
                var manifestsupdated = _context.IngestManifests.ToList();

                foreach (var im in _context.IngestManifests.AsEnumerable())
                {
                    var img = _MyObservIngestManifest.Where(i => i.Id == im.Id).FirstOrDefault();
                    if (img != null)
                    {
                        if (im.Statistics.PendingFilesCount == 0 && img.PendingFiles != im.Statistics.PendingFilesCount)
                        {
                            // Notify if upload completed for one bulk ingest container
                            myform.Notify(string.Format("Bulk ingest completed with {0} error(s)", im.Statistics.ErrorFilesCount), string.Format("Container '{0}'", im.Name), im.Statistics.ErrorFilesCount > 0);
                            myform.TextBoxLogWriteLine(string.Format("Bulk ingest on container '{0}' completed with {1} error(s)", im.Name, im.Statistics.ErrorFilesCount), im.Statistics.ErrorFilesCount > 0);
                            myform.DoRefreshGridAssetV(false);
                        }

                        img.State         = im.State;
                        img.LastModified  = im.LastModified.ToLocalTime().ToString("G");
                        img.PendingFiles  = im.Statistics.PendingFilesCount;
                        img.FinishedFiles = im.Statistics.FinishedFilesCount;

                        if (im.Statistics.FinishedFilesCount + im.Statistics.PendingFilesCount == 0)
                        {
                            img.Progress = 101;
                        }
                        else
                        {
                            img.Progress = (float)im.Statistics.FinishedFilesCount / (float)(im.Statistics.FinishedFilesCount + im.Statistics.PendingFilesCount) * 100;
                        }
                    }
                }

                System.Threading.Thread.Sleep(10000); // 10s
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
Beispiel #2
0
        public void DoJobProgress(JobExtension job)
        {
            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            _MyListJobsMonitored.Add(job.Job.Name, tokenSource); // to track the task and be able to cancel it later

            Debug.WriteLine("launch job monitor : " + job.Job.Name);

            _client.RefreshTokenIfNeeded();

            Task.Run(() =>
            {
                try
                {
                    Job myJob = null;

                    do
                    {
                        myJob = _client.AMSclient.Jobs.Get(_client.credentialsEntry.ResourceGroup, _client.credentialsEntry.AccountName, job.TransformName, job.Job.Name);

                        if (token.IsCancellationRequested == true)
                        {
                            return;
                        }

                        int index = -1;
                        foreach (JobEntryV3 je in _MyObservJobV3) // let's search for index
                        {
                            if (je.Name == myJob.Name)
                            {
                                index = _MyObservJobV3.IndexOf(je);
                                break;
                            }
                        }

                        if (index >= 0) // we found it
                        {               // we update the observation collection
                            var progress = ReturnProgressJob(myJob);

                            _MyObservJobV3[index].Progress = progress.progress;
                            _MyObservJobV3[index].Priority = myJob.Priority;
                            //_MyObservJobV3[index].StartTime = myJob...StartTime.HasValue ? ((DateTime)myJob.StartTime).ToLocalTime().ToString("G") : null;
                            //_MyObservJobV3[index].EndTime = myJob.EndTime.HasValue ? ((DateTime)myJob.EndTime).ToLocalTime().ToString("G") : null;

                            _MyObservJobV3[index].State = myJob.State;


                            /*
                             * // let's calculate the estipated time
                             * string ETAstr = "", Durationstr = "";
                             * if (progress > 3)
                             * {
                             *  DateTime startlocaltime = ((DateTime)myJob.StartTime).ToLocalTime();
                             *  TimeSpan interval = (TimeSpan)(DateTime.Now - startlocaltime);
                             *  DateTime ETA = DateTime.Now.AddSeconds((100d / progress - 1d) * interval.TotalSeconds);
                             *  TimeSpan estimatedduration = (TimeSpan)(ETA - startlocaltime);
                             *
                             *  ETAstr = "Estimated: " + ETA.ToString("G");
                             *  Durationstr = "Estimated: " + estimatedduration.ToString(@"d\.hh\:mm\:ss");
                             *  _MyObservJobV3[index].EndTime = ETA.ToString(@"G") + " ?";
                             *  _MyObservJobV3[index].Duration = myJob.EndTime.HasValue ?
                             *               ((TimeSpan)((DateTime)myJob.EndTime - (DateTime)myJob.StartTime)).ToString(@"d\.hh\:mm\:ss")
                             *               : estimatedduration.ToString(@"d\.hh\:mm\:ss") + " ?";
                             * }
                             */

                            int indexdisplayed = -1;
                            foreach (JobEntryV3 je in _MyObservJobV3) // let's search for index in the page
                            {
                                if (je.Name == myJob.Name)
                                {
                                    indexdisplayed = _MyObservJobV3.IndexOf(je);
                                    try
                                    {
                                        this.BeginInvoke(new Action(() =>
                                        {
                                            this.Rows[indexdisplayed].Cells[this.Columns["Progress"].Index].ToolTipText = progress.sb.ToString(); // mouse hover info
                                            if (progress.progress != 0)
                                            {
                                                //  this.Rows[indexdisplayed].Cells[this.Columns["EndTime"].Index].ToolTipText = ETAstr;// mouse hover info
                                                //      this.Rows[indexdisplayed].Cells[this.Columns["Duration"].Index].ToolTipText = Durationstr;// mouse hover info
                                            }
                                            this.Refresh();
                                        }));
                                    }
                                    catch
                                    {
                                    }

                                    break;
                                }
                            }
                        }

                        if (myJob != null && myJob.State != Microsoft.Azure.Management.Media.Models.JobState.Finished && myJob.State != Microsoft.Azure.Management.Media.Models.JobState.Error && myJob.State != Microsoft.Azure.Management.Media.Models.JobState.Canceled)
                        {
                            Debug.WriteLine("wait for status : " + myJob.Name);
                            Task.Delay(JobRefreshIntervalInMilliseconds).Wait();
                        }
                        else
                        {
                            break;
                        }
                    }while (myJob.State != Microsoft.Azure.Management.Media.Models.JobState.Finished &&
                            myJob.State != Microsoft.Azure.Management.Media.Models.JobState.Error &&
                            myJob.State != Microsoft.Azure.Management.Media.Models.JobState.Canceled);

                    // job finished
                    _client.RefreshTokenIfNeeded();
                    myJob = _client.AMSclient.Jobs.Get(_client.credentialsEntry.ResourceGroup, _client.credentialsEntry.AccountName, job.TransformName, job.Job.Name);

                    int index2 = -1;
                    foreach (JobEntryV3 je in _MyObservJobV3) // let's search for index
                    {
                        if (je.Name == myJob.Name)
                        {
                            index2 = _MyObservJobV3.IndexOf(je);
                            break;
                        }
                    }
                    if (index2 >= 0)                             // we found it
                    {                                            // we update the observation collection
                        StringBuilder sb2 = new StringBuilder(); // display percentage for each task for mouse hover (tooltiptext)

                        double progress2 = 0;
                        for (int i = 0; i < myJob.Outputs.Count; i++)
                        {
                            JobOutput output = myJob.Outputs[i];

                            if (output.State == Microsoft.Azure.Management.Media.Models.JobState.Processing)
                            {
                                progress2 += output.Progress;

                                sb2.AppendLine(string.Format("{0} % ({1})", Convert.ToInt32(output.Progress).ToString(), output.Label));
                            }
                        }
                        if (myJob.Outputs.Count > 0)
                        {
                            progress2 = progress2 / myJob.Outputs.Count;
                        }

                        _MyObservJobV3[index2].Progress = 101d; // progress;  we don't want the progress bar to be displayed
                        _MyObservJobV3[index2].Priority = myJob.Priority;
                        _MyObservJobV3[index2].State    = myJob.State;

                        if (_MyListJobsMonitored.ContainsKey(myJob.Name)) // we want to display only one time
                        {
                            _MyListJobsMonitored.Remove(myJob.Name);      // let's remove from the list of monitored jobs
                            Mainform myform = (Mainform)this.FindForm();


                            // string status = Enum.GetName(typeof(Microsoft.Azure.Management.Media.Models.JobState), myJob.State).ToLower();
                            string status = myJob.State.ToString();

                            myform.BeginInvoke(new Action(() =>
                            {
                                myform.Notify(string.Format("Job {0}", status), string.Format("Job {0}", _MyObservJobV3[index2].Name), myJob.State == Microsoft.Azure.Management.Media.Models.JobState.Error);
                                myform.TextBoxLogWriteLine(string.Format("Job '{0}' : {1}.", _MyObservJobV3[index2].Name, status), myJob.State == Microsoft.Azure.Management.Media.Models.JobState.Error);
                                if (myJob.State == Microsoft.Azure.Management.Media.Models.JobState.Error)
                                {
                                    foreach (var output in myJob.Outputs)
                                    {
                                        if (output.Error != null && output.Error.Details != null)
                                        {
                                            for (int i = 0; i < output.Error.Details.Count(); i++)
                                            {
                                                myform.TextBoxLogWriteLine(string.Format("Output '{0}', Error : {1}", output.Label, output.Error + " : " + output.Error.Message), true);
                                            }
                                        }
                                    }
                                }
                                myform.DoRefreshGridAssetV(false);
                            }));

                            this.BeginInvoke(new Action(() =>
                            {
                                this.Refresh();
                            }));
                        }
                    }
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(Program.GetErrorMessage(e), "Job Monitoring Error");
                    Debug.WriteLine("error job monitor : " + Program.GetErrorMessage(ex));
                }
            }, token);
        }