diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs | 496 |
1 files changed, 73 insertions, 423 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs b/Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs index fce815bc3..4b0066d7f 100644 --- a/Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs +++ b/Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs @@ -17,15 +17,13 @@ namespace Tango.Integration.Operation /// <seealso cref="Tango.Core.ExtendedObject" /> public class JobHandler : ExtendedObject { - protected Action _cancelAction; - protected List<Segment> _effectiveSegments; - protected String _lastStatusMessage; - protected int _last_unit; - protected bool _finalizing; - protected JobHandlerModes _mode; - protected double _last_progress; - protected const int PROGRESS_REPORT_RANGE_METERS = 5; - protected bool loggedContinueMessage; + private Action _cancelAction; + private List<Segment> _effectiveSegments; + private String _lastStatusMessage; + private int _last_unit; + private bool _finalizing; + private JobHandlerModes _mode; + private double _last_progress; #region Events @@ -79,40 +77,15 @@ namespace Tango.Integration.Operation /// </summary> public event EventHandler<SpoolChangeRequiredEventArgs> SpoolChangeRequired; - /// <summary> - /// Occurs when <see cref="CanCancel"/> has changed. - /// </summary> - public event EventHandler CanCancelChanged; - #endregion #region Properties - private JobStatus _jobStatus; - /// <summary> - /// Gets or sets the current job status that was used to invalidate this handler. - /// </summary> - public JobStatus JobStatus - { - get { return _jobStatus; } - set { _jobStatus = value; RaisePropertyChangedAuto(); } - } - /// <summary> /// Gets a value indicating whether this handler job has been canceled. /// </summary> public bool IsCanceled { get; internal set; } - private bool _canCancel; - /// <summary> - /// Gets a value indicating whether the job can be canceled. - /// </summary> - public bool CanCancel - { - get { return _canCancel; } - internal set { _canCancel = value; RaisePropertyChangedAuto(); CanCancelChanged?.Invoke(this, new EventArgs()); } - } - /// <summary> /// Gets the process parameters. /// </summary> @@ -147,14 +120,14 @@ namespace Tango.Integration.Operation /// </summary> public JobHandler() { - CanCancel = true; + } /// <summary> /// Initializes a new instance of the <see cref="JobHandler"/> class. /// </summary> /// <param name="cancelAction">The cancel action.</param> - public JobHandler(Action cancelAction, Job job, JobTicket jobTicket, ProcessParametersTable processParameters, JobHandlerModes mode) : this() + internal JobHandler(Action cancelAction, Job job, JobTicket jobTicket, ProcessParametersTable processParameters, JobHandlerModes mode) : this() { _mode = mode; @@ -171,7 +144,7 @@ namespace Tango.Integration.Operation _effectiveSegments = Job.EffectiveSegments.ToList(); - _cancelAction = () => { cancelAction(); }; + _cancelAction = () => { IsCanceled = true; cancelAction(); }; Status = new RunningJobStatus(); @@ -205,7 +178,7 @@ namespace Tango.Integration.Operation //Create all segments int segment_index = 1; - for (int j = 0; j < Math.Max(Job.NumberOfUnits, 1); j++) + for (int j = 0; j < Job.NumberOfUnits; j++) { for (int i = 0; i < _effectiveSegments.Count; i++) { @@ -241,7 +214,7 @@ namespace Tango.Integration.Operation /// Raises the status received event. /// </summary> /// <param name="status">The status.</param> - public void RaiseStatusReceived(JobStatus status) + internal void RaiseStatusReceived(JobStatus status) { InvalidateJobProgress(status); } @@ -250,11 +223,10 @@ namespace Tango.Integration.Operation /// Raises the failed event. /// </summary> /// <param name="ex">The ex.</param> - public void RaiseFailed(Exception ex) + internal void RaiseFailed(Exception ex) { - LogManager.Log($"Job failed at position {Status.Progress}/{Status.TotalProgress}..."); Status.IsFailed = true; - RaiseStatusChanged(); + StatusChanged?.Invoke(this, Status); RaisePropertyChanged(nameof(Status)); Failed?.Invoke(this, ex); Stopped?.Invoke(this, new EventArgs()); @@ -263,36 +235,13 @@ namespace Tango.Integration.Operation /// <summary> /// Raises the completed event. /// </summary> - public void RaiseCompleted() + internal void RaiseCompleted() { - //This will compensate on any missing progress from Shlomo, but also will tell the wrong progress if job is really completed with a large progress mistake. - // Might be worth to compensate only on small drifts like the below (ProgressMinusSettingsUp)... - //InvalidateJobProgress(new JobStatus() - //{ - // Progress = Status.TotalProgress, - // CurrentSegmentIndex = 0, - //}); - - LogManager.Log($"Job completed at position {Status.Progress}/{Status.TotalProgress}..."); - - //If drift is smaller than 10cm auto correct it. - if (Math.Abs(Status.TotalProgressMinusSettingUp - Status.ProgressMinusSettingUp) < 0.1) - { - LogManager.Log($"Job completed with a small drift in the progress minus setting up calculation. ({Status.ProgressMinusSettingUp}/{Status.TotalProgressMinusSettingUp}). Compensating..."); - Status.ProgressMinusSettingUp = Status.TotalProgressMinusSettingUp; - } - //If the overall progress is correct? Fix the minus setting up. There is a problem with the delta calc! - else if (Status.Progress == Status.TotalProgress) - { - LogManager.Log($"Job completed with a small drift in the progress minus setting up calculation but the overall progress seems OK. ({Status.ProgressMinusSettingUp}/{Status.TotalProgressMinusSettingUp}). Compensating..."); - Status.ProgressMinusSettingUp = Status.TotalProgressMinusSettingUp; - } - Status.Segments.Last().Completed = true; Status.RemainingUnits = 0; Status.IsFinalizing = false; Status.IsCompleted = true; - RaiseStatusChanged(); + StatusChanged?.Invoke(this, Status); RaisePropertyChanged(nameof(Status)); Completed?.Invoke(this, new EventArgs()); Stopped?.Invoke(this, new EventArgs()); @@ -301,11 +250,10 @@ namespace Tango.Integration.Operation /// <summary> /// Raises the canceled event. /// </summary> - public void RaiseCanceled() + internal void RaiseCanceled() { - LogManager.Log($"Job canceled at position {Status.Progress}/{Status.TotalProgress}..."); Status.IsCanceled = true; - RaiseStatusChanged(); + StatusChanged?.Invoke(this, Status); RaisePropertyChanged(nameof(Status)); Canceled?.Invoke(this, new EventArgs()); Stopped?.Invoke(this, new EventArgs()); @@ -327,342 +275,71 @@ namespace Tango.Integration.Operation #region Private Methods - //protected virtual void InvalidateJobProgress(JobStatus s) - //{ - // JobStatus = s; - // bool invalidProgress = false; - - // if (_last_progress != s.Progress) - // { - // if (s.Progress <= PROGRESS_REPORT_RANGE_METERS || s.Progress >= Status.TotalProgress - PROGRESS_REPORT_RANGE_METERS) - // { - // LogManager.Log($"Updating job progress {s.Progress}/{Status.TotalProgress}..."); - // } - // else if (!loggedContinueMessage) - // { - // loggedContinueMessage = true; - // LogManager.Log($"Progress logging will continue {PROGRESS_REPORT_RANGE_METERS} meters before completion..."); - // } - // } - - // if (s.Progress < 0) - // { - // LogManager.Log($"Invalid job progress received '{s.Progress}'.", LogCategory.Error); - // invalidProgress = true; - // } - - // if (s.Progress > Status.TotalProgress) - // { - // LogManager.Log($"Invalid job progress received '{s.Progress}' while total progress is '{Status.TotalProgress}'.", LogCategory.Error); - // invalidProgress = true; - // } - - // if (s.Progress < _last_progress) - // { - // LogManager.Log($"Invalid job progress received '{s.Progress}' while last progress was '{_last_progress}'."); - // invalidProgress = true; - // } - - // if (invalidProgress) - // { - // return; - // } - - // _last_progress = s.Progress; - - // //Job Status - // if (IsCanceled) - // { - // Status.IsCanceled = IsCanceled; - // RaiseStatusChanged(); - // return; - // } - - // List<Segment> unit_segments = new List<Segment>(); - // double delta = s.Progress - Status.Progress; - - // Status.Progress = s.Progress; - // Status.RemainingTime = Status.TotalTime - Job.TranslateProgressToTime(Status.Progress, ProcessParameters); - // Status.RemainingProgress = Status.TotalProgress - Status.Progress; - - // if (Status.SettingUpProgress < Status.SettingUpTotalProgress) - // { - // Status.SettingUpProgress += delta; - // } - // else - // { - // if (Status.IsSettingUp && Status.Progress > 0) - // { - // Status.IsSettingUp = false; - // } - - // Status.ProgressMinusSettingUp += delta; - // } - - // if (s.Progress < Job.LengthIncludingNumberOfUnits || _mode == JobHandlerModes.SettingUp) - // { - // Status.ProgressWithoutFinalization += delta; - - // unit_segments = _effectiveSegments.ToList(); - - // if (Job.EnableInterSegment && Job.NumberOfUnits > 1 && Status.RemainingUnits > 1) - // { - // unit_segments.Add(Job.CreateInterSegment(Job.InterSegmentLength)); - // } - - // if (unit_segments.Count != Status.CurrentUnitSegments.Count) - // { - // Status.CurrentUnitSegments = unit_segments; - // } - - // Status.CurrentUnitTotalProgress = Status.RemainingUnits > 1 && Job.EnableInterSegment ? Job.Length + (Job.InterSegmentLength) : Job.Length; - - // if (_mode == JobHandlerModes.Finalization) - // { - // Status.CurrentUnitProgress += delta; - // } - // else - // { - // if (!Status.IsSettingUp) - // { - // Status.CurrentUnitProgress += delta; - // } - // } - - // if (Status.CurrentUnitProgress >= Status.CurrentUnitTotalProgress) - // { - // Status.CurrentUnitProgress = 0; - // Status.CurrentUnit++; - // } - - // Status.RemainingUnits = Job.NumberOfUnits - Status.CurrentUnit; - - // if (s.Message != _lastStatusMessage && s.Message != String.Empty) - // { - // Status.Message = s.Message; - // } - // else - // { - // Status.Message = null; - // } - - // _lastStatusMessage = s.Message; - - // RaiseStatusChanged(); - - // //Segments Completion - - // if (Status.CurrentUnit > _last_unit) - // { - // foreach (var segment in Status.CurrentUnitSegments) - // { - // segment.Started = false; - // segment.Completed = false; - // } - - // if (Job.NumberOfUnits > 1) - // { - // RaiseUnitCompleted(_last_unit); - // } - // } - - // _last_unit = Status.CurrentUnit; - - - // for (int i = 0; i < Status.CurrentUnitSegments.Count; i++) - // { - // Segment segment = Status.CurrentUnitSegments[i]; - // double previousSegmentsLengthWithThis = Status.CurrentUnitSegments.Take(i + 1).Sum(x => x.LengthWithFactor); - // TimeSpan segmentsDuration = Job.TranslateProgressToTime(previousSegmentsLengthWithThis, ProcessParameters); - // TimeSpan segmentRemainingTime = segmentsDuration - Job.TranslateProgressToTime(Status.CurrentUnitProgress, ProcessParameters); - - // if (i == 0 && Status.CurrentUnitProgress > 0) - // { - // if (!segment.Started) - // { - // segment.Started = true; - // RaiseSegmentStarted(segment); - // } - // } - - // if (Status.CurrentUnitProgress >= previousSegmentsLengthWithThis) - // { - // if (!segment.Completed) - // { - // segment.Completed = true; - // RaiseSegmentCompleted(segment); - // } - - // if (i < Status.CurrentUnitSegments.Count - 1) - // { - // if (!Status.CurrentUnitSegments[i + 1].Started) - // { - // Status.CurrentUnitSegments[i + 1].Started = true; - // RaiseSegmentStarted(Status.CurrentUnitSegments[i + 1]); - // } - // } - // } - - // if (segment.Started && !segment.Completed) - // { - // segment.RemainingTime = segmentRemainingTime; - // } - // } - - - // //Set Segment Completion for All Segments List - // for (int i = 0; i < Status.Segments.Count; i++) - // { - // Segment segment = Status.Segments[i]; - // double previousSegmentsLengthWithThis = Status.Segments.Take(i + 1).Sum(x => x.LengthWithFactor); - // TimeSpan segmentsDuration = Job.TranslateProgressToTime(previousSegmentsLengthWithThis, ProcessParameters); - // TimeSpan segmentRemainingTime = segmentsDuration - Job.TranslateProgressToTime(Status.Progress, ProcessParameters); - - // segment.Progress = Math.Min(Math.Max((previousSegmentsLengthWithThis - segment.Length - Status.Progress) * -1, 0), segment.Length); - - // if (i == 0 && Status.Progress > 0) - // { - // if (!segment.Started) - // { - // segment.Started = true; - // Status.CurrentSegment = segment; - // } - // } - - // if (Status.Progress >= previousSegmentsLengthWithThis) - // { - // if (!segment.Completed) - // { - // segment.Completed = true; - // } - - // if (i < Status.Segments.Count - 1) - // { - // if (!Status.Segments[i + 1].Started) - // { - // Status.Segments[i + 1].Started = true; - // Status.CurrentSegment = Status.Segments[i + 1]; - // } - // } - // } - - // if (segment.Started && !segment.Completed) - // { - // segment.RemainingTime = segmentRemainingTime; - // } - // } - // } - // else - // { - // //Finalizing - // if (!_finalizing) - // { - // _finalizing = true; - // Status.IsFinalizing = true; - // var last_Segment = _effectiveSegments.Last().Clone(); - // last_Segment.Length = ProcessParameters.DryerBufferLengthMeters; - // Status.CurrentUnitSegments = new List<Segment> { last_Segment }; - // Status.CurrentUnitTotalProgress = last_Segment.Length; - // Status.CurrentUnitProgress = 0; - // Status.ProgressWithoutFinalization = Status.TotalProgressWithoutFinalization; - // RaiseFinalizing(); - // } - - // Status.CurrentUnitProgress += delta; - // Status.FinalizingProgress += delta; - // } - //} - - protected virtual void InvalidateJobProgress(JobStatus s) + private void InvalidateJobProgress(JobStatus s) { - JobStatus = s; + bool invalidProgress = false; - if (_last_progress != s.Progress) - { - if (s.Progress <= PROGRESS_REPORT_RANGE_METERS || s.Progress >= Status.TotalProgress - PROGRESS_REPORT_RANGE_METERS) - { - LogManager.Log($"Updating job progress {s.Progress}/{Status.TotalProgress}..."); - } - else if (!loggedContinueMessage) - { - loggedContinueMessage = true; - LogManager.Log($"Progress logging will continue {PROGRESS_REPORT_RANGE_METERS} meters before completion..."); - } - } + LogManager.Log($"Updating job progress {s.Progress}/{Status.TotalProgress}..."); if (s.Progress < 0) { LogManager.Log($"Invalid job progress received '{s.Progress}'.", LogCategory.Error); - return; + invalidProgress = true; } if (s.Progress > Status.TotalProgress) { LogManager.Log($"Invalid job progress received '{s.Progress}' while total progress is '{Status.TotalProgress}'.", LogCategory.Error); - return; + invalidProgress = true; } if (s.Progress < _last_progress) { LogManager.Log($"Invalid job progress received '{s.Progress}' while last progress was '{_last_progress}'."); + invalidProgress = true; + } + + if (invalidProgress) + { + return; } _last_progress = s.Progress; + //Job Status + if (IsCanceled) + { + Status.IsCanceled = IsCanceled; + StatusChanged?.Invoke(this, Status); + return; + } + List<Segment> unit_segments = new List<Segment>(); + double delta = s.Progress - Status.Progress; Status.Progress = s.Progress; Status.RemainingTime = Status.TotalTime - Job.TranslateProgressToTime(Status.Progress, ProcessParameters); Status.RemainingProgress = Status.TotalProgress - Status.Progress; - if (s.Progress < Status.SettingUpTotalProgress || Status.SettingUpProgress < Status.SettingUpTotalProgress) + if (Status.SettingUpProgress < Status.SettingUpTotalProgress) { - Status.SettingUpProgress = Math.Min(s.Progress, this.Status.SettingUpTotalProgress); - Status.IsSettingUp = true; + Status.SettingUpProgress += delta; } - if (s.Progress >= Status.SettingUpTotalProgress) + else { if (Status.IsSettingUp && Status.Progress > 0) { Status.IsSettingUp = false; } - Status.ProgressMinusSettingUp = s.Progress - this.Status.SettingUpTotalProgress; + Status.ProgressMinusSettingUp += delta; } - int units = (int)Math.Max(Job.NumberOfUnits, 1); - if (s.Progress < Job.LengthIncludingNumberOfUnits || _mode == JobHandlerModes.SettingUp) { - Status.ProgressWithoutFinalization = s.Progress; + Status.ProgressWithoutFinalization += delta; unit_segments = _effectiveSegments.ToList(); - Status.CurrentUnitProgress = 0.0; - double previousUnitsLengthWithoutThis = 0.0; - for (int index = 0; index < units; ++index) - { - Status.CurrentUnit = index; - double unitLength = !Job.EnableInterSegment || index >= units - 1 ? Job.Length : Job.Length + Job.InterSegmentLength; - if (_mode == JobHandlerModes.Finalization) - { - if (s.Progress < unitLength + previousUnitsLengthWithoutThis) - { - Status.CurrentUnitProgress = s.Progress - previousUnitsLengthWithoutThis; - break; - } - } - else if (s.Progress <= previousUnitsLengthWithoutThis + unitLength + Status.SettingUpProgress) - { - if (!Status.IsSettingUp) - { - Status.CurrentUnitProgress = s.Progress - previousUnitsLengthWithoutThis - this.Status.SettingUpProgress; - break; - } - break; - } - previousUnitsLengthWithoutThis += unitLength; - } - Status.RemainingUnits = this.Job.NumberOfUnits - this.Status.CurrentUnit; if (Job.EnableInterSegment && Job.NumberOfUnits > 1 && Status.RemainingUnits > 1) { @@ -676,6 +353,26 @@ namespace Tango.Integration.Operation Status.CurrentUnitTotalProgress = Status.RemainingUnits > 1 && Job.EnableInterSegment ? Job.Length + (Job.InterSegmentLength) : Job.Length; + if (_mode == JobHandlerModes.Finalization) + { + Status.CurrentUnitProgress += delta; + } + else + { + if (!Status.IsSettingUp) + { + Status.CurrentUnitProgress += delta; + } + } + + if (Status.CurrentUnitProgress >= Status.CurrentUnitTotalProgress) + { + Status.CurrentUnitProgress = 0; + Status.CurrentUnit++; + } + + Status.RemainingUnits = Job.NumberOfUnits - Status.CurrentUnit; + if (s.Message != _lastStatusMessage && s.Message != String.Empty) { Status.Message = s.Message; @@ -687,10 +384,10 @@ namespace Tango.Integration.Operation _lastStatusMessage = s.Message; - - RaiseStatusChanged(); + StatusChanged?.Invoke(this, Status); //Segments Completion + if (Status.CurrentUnit > _last_unit) { foreach (var segment in Status.CurrentUnitSegments) @@ -701,7 +398,7 @@ namespace Tango.Integration.Operation if (Job.NumberOfUnits > 1) { - RaiseUnitCompleted(_last_unit); + UnitCompleted?.Invoke(this, _last_unit); } } @@ -720,7 +417,7 @@ namespace Tango.Integration.Operation if (!segment.Started) { segment.Started = true; - RaiseSegmentStarted(segment); + SegmentStarted?.Invoke(this, segment); } } @@ -729,7 +426,7 @@ namespace Tango.Integration.Operation if (!segment.Completed) { segment.Completed = true; - RaiseSegmentCompleted(segment); + SegmentCompleted?.Invoke(this, segment); } if (i < Status.CurrentUnitSegments.Count - 1) @@ -737,7 +434,7 @@ namespace Tango.Integration.Operation if (!Status.CurrentUnitSegments[i + 1].Started) { Status.CurrentUnitSegments[i + 1].Started = true; - RaiseSegmentStarted(Status.CurrentUnitSegments[i + 1]); + SegmentStarted?.Invoke(this, Status.CurrentUnitSegments[i + 1]); } } } @@ -748,6 +445,7 @@ namespace Tango.Integration.Operation } } + //Set Segment Completion for All Segments List for (int i = 0; i < Status.Segments.Count; i++) { @@ -803,60 +501,12 @@ namespace Tango.Integration.Operation Status.CurrentUnitTotalProgress = last_Segment.Length; Status.CurrentUnitProgress = 0; Status.ProgressWithoutFinalization = Status.TotalProgressWithoutFinalization; - RaiseFinalizing(); + Finalizing?.Invoke(this, new EventArgs()); } - Status.CurrentUnitProgress = s.Progress - Job.LengthIncludingNumberOfUnits; - Status.FinalizingProgress = s.Progress - Status.TotalProgressWithoutFinalization; - } - } - - #endregion - - #region Protected Methods - - protected void RaiseStatusChanged() - { - StatusChanged?.Invoke(this, Status); - } - - protected void RaiseSegmentStarted(Segment segment) - { - if (segment.IsInterSegment) - { - LogManager.Log($"Inter Segment started."); - } - else - { - LogManager.Log($"Segment {segment.SegmentIndex} of unit {Status.CurrentUnit + 1} started..."); - } - - SegmentStarted?.Invoke(this, segment); - } - - protected void RaiseSegmentCompleted(Segment segment) - { - if (segment.IsInterSegment) - { - LogManager.Log($"Inter Segment completed."); + Status.CurrentUnitProgress += delta; + Status.FinalizingProgress += delta; } - else - { - LogManager.Log($"Segment {segment.SegmentIndex} of unit {Status.CurrentUnit + 1} completed."); - } - SegmentCompleted?.Invoke(this, segment); - } - - protected void RaiseUnitCompleted(int unit) - { - LogManager.Log($"Unit {unit + 1} completed..."); - UnitCompleted?.Invoke(this, unit); - } - - protected void RaiseFinalizing() - { - LogManager.Log($"Finalizing..."); - Finalizing?.Invoke(this, new EventArgs()); } #endregion |
