forked from ddrilling/AsbCloudServer
170 lines
5.2 KiB
C#
170 lines
5.2 KiB
C#
|
using System;
|
|||
|
using System.Diagnostics;
|
|||
|
|
|||
|
namespace AsbCloudInfrastructure.Background
|
|||
|
{
|
|||
|
public class BackgroudWorkDto
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор работы. Должен быть уникальным. Используется в логах и передается в колбэки.
|
|||
|
/// </summary>
|
|||
|
public string Id { get; init; } = null!;
|
|||
|
|
|||
|
public class CurrentStateInfo
|
|||
|
{
|
|||
|
private string state = "start";
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Время последнего запуска
|
|||
|
/// </summary>
|
|||
|
public DateTime Start { get; } = DateTime.Now;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Текущее время выполнения
|
|||
|
/// </summary>
|
|||
|
public TimeSpan ExecutionTime => DateTime.Now - Start;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Текстовое описание того, что происходит в задаче.
|
|||
|
/// </summary>
|
|||
|
public string State
|
|||
|
{
|
|||
|
get => state;
|
|||
|
internal set
|
|||
|
{
|
|||
|
state = value;
|
|||
|
StateUpdate = DateTime.Now;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public double Progress { get; internal set; } = 0;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Время последнего запуска
|
|||
|
/// </summary>
|
|||
|
public DateTime StateUpdate { get; private set; } = DateTime.Now;
|
|||
|
}
|
|||
|
|
|||
|
public class LastErrorInfo: LastCompleteInfo
|
|||
|
{
|
|||
|
public LastErrorInfo(CurrentStateInfo state, string errorText)
|
|||
|
: base(state)
|
|||
|
{
|
|||
|
ErrorText = errorText;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Последняя ошибка
|
|||
|
/// </summary>
|
|||
|
public string ErrorText { get; init; } = null!;
|
|||
|
}
|
|||
|
|
|||
|
public class LastCompleteInfo
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Дата запуска
|
|||
|
/// </summary>
|
|||
|
public DateTime Start {get; init;}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Дата завершения
|
|||
|
/// </summary>
|
|||
|
public DateTime End { get; init; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Продолжительность последнего выполнения
|
|||
|
/// </summary>
|
|||
|
public TimeSpan ExecutionTime => End - Start;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Состояние на момент завершения
|
|||
|
/// </summary>
|
|||
|
public string State { get; init; }
|
|||
|
|
|||
|
public LastCompleteInfo(CurrentStateInfo state)
|
|||
|
{
|
|||
|
Start = state.Start;
|
|||
|
End = DateTime.Now;
|
|||
|
State = state.State;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Текущее состояние
|
|||
|
/// </summary>
|
|||
|
public CurrentStateInfo? CurrentState { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Последняя ошибка
|
|||
|
/// </summary>
|
|||
|
public LastErrorInfo? LastError { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Последняя завершенная
|
|||
|
/// </summary>
|
|||
|
public LastCompleteInfo? LastComplete { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Кол-во запусков
|
|||
|
/// </summary>
|
|||
|
public int CountStart { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Кол-во завершений
|
|||
|
/// </summary>
|
|||
|
public int CountComplete { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Кол-во ошибок
|
|||
|
/// </summary>
|
|||
|
public int CountErrors { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Максимально допустимое время выполнения работы
|
|||
|
/// </summary>
|
|||
|
public TimeSpan Timeout { get; set; } = TimeSpan.FromMinutes(1);
|
|||
|
|
|||
|
private string WorkNameForTrace => $"Backgroud work:\"{Id}\"";
|
|||
|
|
|||
|
protected void SetStatusStart()
|
|||
|
{
|
|||
|
CurrentState = new();
|
|||
|
CountStart++;
|
|||
|
Trace.TraceInformation($"{WorkNameForTrace} state: starting");
|
|||
|
}
|
|||
|
|
|||
|
protected void UpdateStatus(string newState, double? progress)
|
|||
|
{
|
|||
|
if (CurrentState is null)
|
|||
|
return;
|
|||
|
|
|||
|
CurrentState.State = newState;
|
|||
|
if (progress.HasValue)
|
|||
|
CurrentState.Progress = progress.Value;
|
|||
|
|
|||
|
Trace.TraceInformation($"{WorkNameForTrace} state: {newState}");
|
|||
|
}
|
|||
|
|
|||
|
protected void SetStatusComplete(System.Threading.Tasks.TaskStatus status)
|
|||
|
{
|
|||
|
if (CurrentState is null)
|
|||
|
return;
|
|||
|
|
|||
|
LastComplete = new (CurrentState);
|
|||
|
CurrentState = null;
|
|||
|
CountComplete++;
|
|||
|
Trace.TraceInformation($"{WorkNameForTrace} state: completed");
|
|||
|
}
|
|||
|
|
|||
|
protected void SetLastError(string errorMessage)
|
|||
|
{
|
|||
|
if (CurrentState is null)
|
|||
|
return;
|
|||
|
|
|||
|
LastError = new LastErrorInfo(CurrentState, errorMessage);
|
|||
|
CurrentState = null;
|
|||
|
CountErrors++;
|
|||
|
Trace.TraceError($"{WorkNameForTrace} throw exception[{CountErrors}]: {errorMessage}");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|