101 lines
3.5 KiB
C#
101 lines
3.5 KiB
C#
|
|
using HRM.BO;
|
||
|
|
using System;
|
||
|
|
using System.Threading;
|
||
|
|
|
||
|
|
namespace HRM.UI.Realtime.Jobs
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// The IJobProgress a job's work receives. Work may call Report thousands of times a second; this class keeps the
|
||
|
|
/// latest values and sends at most one message per throttle interval, except that a new stage is sent at once.
|
||
|
|
/// A trailing timer makes sure the last value is always sent, even when the work goes quiet after it.
|
||
|
|
/// </summary>
|
||
|
|
internal sealed class JobProgressReporter : IJobProgress, IDisposable
|
||
|
|
{
|
||
|
|
private readonly JobState _state;
|
||
|
|
private readonly IRealtimePublisher _publisher;
|
||
|
|
private readonly TimeSpan _throttle;
|
||
|
|
private readonly Timer _flushTimer;
|
||
|
|
private DateTime _lastSent = DateTime.MinValue;
|
||
|
|
private bool _pending;
|
||
|
|
private bool _timerArmed;
|
||
|
|
private bool _disposed;
|
||
|
|
|
||
|
|
public JobProgressReporter(JobState state, IRealtimePublisher publisher, TimeSpan throttle, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
_state = state;
|
||
|
|
_publisher = publisher;
|
||
|
|
_throttle = throttle;
|
||
|
|
CancellationToken = cancellationToken;
|
||
|
|
_flushTimer = new Timer(_ => Flush(), null, Timeout.Infinite, Timeout.Infinite);
|
||
|
|
}
|
||
|
|
|
||
|
|
public CancellationToken CancellationToken { get; }
|
||
|
|
|
||
|
|
public void Report(double percent, string stage = null, string detail = null, int? current = null, int? total = null)
|
||
|
|
{
|
||
|
|
JobSnapshot toSend = null;
|
||
|
|
lock (_state.Sync)
|
||
|
|
{
|
||
|
|
if (_disposed || _state.IsFinished)
|
||
|
|
return;
|
||
|
|
|
||
|
|
bool stageChanged = stage != null && !string.Equals(stage, _state.Stage, StringComparison.Ordinal);
|
||
|
|
|
||
|
|
if (double.IsNaN(percent)) percent = _state.Percent;
|
||
|
|
// Never backwards, never 100 before the work has really returned.
|
||
|
|
_state.Percent = Math.Max(_state.Percent, Math.Min(Math.Max(percent, 0), 99.9));
|
||
|
|
if (stage != null) _state.Stage = stage;
|
||
|
|
_state.Detail = detail;
|
||
|
|
_state.Current = current;
|
||
|
|
_state.Total = total;
|
||
|
|
|
||
|
|
DateTime now = DateTime.Now;
|
||
|
|
if (stageChanged || now - _lastSent >= _throttle)
|
||
|
|
{
|
||
|
|
_state.Sequence++;
|
||
|
|
_lastSent = now;
|
||
|
|
_pending = false;
|
||
|
|
toSend = _state.ToSnapshot();
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
_pending = true;
|
||
|
|
if (!_timerArmed)
|
||
|
|
{
|
||
|
|
_timerArmed = true;
|
||
|
|
_flushTimer.Change(_throttle, Timeout.InfiniteTimeSpan);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (toSend != null)
|
||
|
|
_ = _publisher.JobChangedAsync(toSend);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Flush()
|
||
|
|
{
|
||
|
|
JobSnapshot toSend = null;
|
||
|
|
lock (_state.Sync)
|
||
|
|
{
|
||
|
|
_timerArmed = false;
|
||
|
|
if (_disposed || !_pending || _state.IsFinished)
|
||
|
|
return;
|
||
|
|
_state.Sequence++;
|
||
|
|
_lastSent = DateTime.Now;
|
||
|
|
_pending = false;
|
||
|
|
toSend = _state.ToSnapshot();
|
||
|
|
}
|
||
|
|
_ = _publisher.JobChangedAsync(toSend);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Dispose()
|
||
|
|
{
|
||
|
|
lock (_state.Sync)
|
||
|
|
{
|
||
|
|
_disposed = true;
|
||
|
|
}
|
||
|
|
_flushTimer.Dispose();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|