74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
using System;
|
|
|
|
namespace HRM.UI.Realtime.Jobs
|
|
{
|
|
/// <summary>
|
|
/// The live, mutable state of one job. Only JobRunner and JobProgressReporter change it, always under <see cref="Sync"/>;
|
|
/// everybody else gets an immutable <see cref="JobSnapshot"/> copy.
|
|
/// </summary>
|
|
internal sealed class JobState
|
|
{
|
|
public readonly object Sync = new object();
|
|
|
|
public JobState(string runId, JobRequest request)
|
|
{
|
|
RunId = runId;
|
|
Request = request;
|
|
Status = JobStatus.Queued;
|
|
QueuedAt = DateTime.Now;
|
|
}
|
|
|
|
public string RunId { get; }
|
|
public JobRequest Request { get; }
|
|
|
|
public JobStatus Status;
|
|
public double Percent;
|
|
public string Stage;
|
|
public string Detail;
|
|
public int? Current;
|
|
public int? Total;
|
|
public DateTime QueuedAt;
|
|
public DateTime? StartedAt;
|
|
public DateTime? FinishedAt;
|
|
public object Result;
|
|
public string Error;
|
|
public long Sequence;
|
|
|
|
public bool IsFinished => Status == JobStatus.Succeeded || Status == JobStatus.Failed;
|
|
|
|
/// <summary>A consistent copy. Call it while holding <see cref="Sync"/>.</summary>
|
|
public JobSnapshot ToSnapshot()
|
|
{
|
|
DateTime now = DateTime.Now;
|
|
double elapsed = StartedAt.HasValue ? ((FinishedAt ?? now) - StartedAt.Value).TotalSeconds : 0;
|
|
|
|
double? eta = null;
|
|
if (Status == JobStatus.Running && Percent >= 2 && Percent < 100 && elapsed >= 3)
|
|
eta = Math.Round(elapsed * (100 - Percent) / Percent, 0);
|
|
else if (IsFinished)
|
|
eta = 0;
|
|
|
|
return new JobSnapshot
|
|
{
|
|
RunId = RunId,
|
|
Key = Request.Key,
|
|
Title = Request.Title,
|
|
Status = Status,
|
|
Percent = Math.Round(Percent, 1),
|
|
Stage = Stage,
|
|
Detail = Detail,
|
|
Current = Current,
|
|
Total = Total,
|
|
QueuedAt = QueuedAt,
|
|
StartedAt = StartedAt,
|
|
FinishedAt = FinishedAt,
|
|
ElapsedSeconds = Math.Round(elapsed, 0),
|
|
EtaSeconds = eta,
|
|
Sequence = Sequence,
|
|
Result = Result,
|
|
Error = Error
|
|
};
|
|
}
|
|
}
|
|
}
|