2024-07-04 11:02:45 +05:00
|
|
|
|
using System;
|
2023-10-08 19:45:21 +05:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
namespace AsbCloudApp.Data;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Информация о фоновой работе
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class BackgroundWorkDto
|
2023-10-08 19:45:21 +05:00
|
|
|
|
{
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Идентификатор работы. Должен быть уникальным. Используется в логах и передается в колбэки.
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public string Id { get; init; } = null!;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Класс описания состояния
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class CurrentStateInfo
|
2023-10-08 19:45:21 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
private string state = "start";
|
|
|
|
|
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Время последнего запуска
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public DateTime Start { get; } = DateTime.Now;
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Текущее время выполнения
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public TimeSpan ExecutionTime => DateTime.Now - Start;
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Текстовое описание того, что происходит в задаче.
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public string State
|
2023-10-08 19:45:21 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
get => state;
|
|
|
|
|
internal set
|
2023-10-08 19:45:21 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
state = value;
|
|
|
|
|
StateUpdate = DateTime.Now;
|
2023-10-08 19:45:21 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Прогресс
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public double Progress { get; internal set; } = 0;
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Время последнего запуска
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public DateTime StateUpdate { get; private set; } = DateTime.Now;
|
|
|
|
|
}
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Инфо о последней ошибке
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class LastErrorInfo : LastCompleteInfo
|
|
|
|
|
{
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
///
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <param name="state"></param>
|
|
|
|
|
/// <param name="errorText"></param>
|
|
|
|
|
public LastErrorInfo(CurrentStateInfo state, string errorText)
|
|
|
|
|
: base(state)
|
|
|
|
|
{
|
|
|
|
|
ErrorText = errorText;
|
|
|
|
|
}
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Последняя ошибка
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public string ErrorText { get; init; } = null!;
|
|
|
|
|
}
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Инфо о последнем завершении
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class LastCompleteInfo
|
|
|
|
|
{
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Дата запуска
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public DateTime Start { get; init; }
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Дата завершения
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public DateTime End { get; init; }
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Продолжительность последнего выполнения
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public TimeSpan ExecutionTime => End - Start;
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// Состояние на момент завершения
|
2023-10-08 19:45:21 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
public string State { get; init; }
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// <summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// ctor
|
2023-10-08 21:20:28 +05:00
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <param name="state"></param>
|
|
|
|
|
public LastCompleteInfo(CurrentStateInfo state)
|
2023-10-08 19:45:21 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
Start = state.Start;
|
|
|
|
|
End = DateTime.Now;
|
|
|
|
|
State = state.State;
|
2023-10-08 19:45:21 +05:00
|
|
|
|
}
|
2024-08-19 10:01:07 +05:00
|
|
|
|
}
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Текущее состояние
|
|
|
|
|
/// </summary>
|
|
|
|
|
public CurrentStateInfo? CurrentState { get; private set; }
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Последняя ошибка
|
|
|
|
|
/// </summary>
|
|
|
|
|
public LastErrorInfo? LastError { get; private set; }
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Последняя завершенная
|
|
|
|
|
/// </summary>
|
|
|
|
|
public LastCompleteInfo? LastComplete { get; private set; }
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Кол-во запусков
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int CountStart { get; private set; }
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Кол-во завершений
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int CountComplete { get; private set; }
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Кол-во ошибок
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int CountErrors { get; private set; }
|
2023-10-08 19:45:21 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Максимально допустимое время выполнения работы
|
|
|
|
|
/// </summary>
|
|
|
|
|
public TimeSpan Timeout { get; set; } = TimeSpan.FromMinutes(1);
|
|
|
|
|
|
|
|
|
|
private string WorkNameForTrace => $"Backgroud work:\"{Id}\"";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновления состояния при запуске работы
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected void SetStatusStart()
|
|
|
|
|
{
|
|
|
|
|
CurrentState = new();
|
|
|
|
|
CountStart++;
|
|
|
|
|
Trace.TraceInformation($"{WorkNameForTrace} state: starting");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновления состояния в процессе работы
|
|
|
|
|
/// </summary>
|
|
|
|
|
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[{100*progress:#}%]: {newState}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновления состояния при успешном завершении работы
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected void SetStatusComplete()
|
|
|
|
|
{
|
|
|
|
|
CountComplete++;
|
|
|
|
|
if (CurrentState is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
LastComplete = new(CurrentState);
|
|
|
|
|
CurrentState = null;
|
|
|
|
|
Trace.TraceInformation($"{WorkNameForTrace} state: completed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновления состояния при ошибке в работе
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected void SetLastError(string errorMessage)
|
|
|
|
|
{
|
|
|
|
|
CountErrors++;
|
|
|
|
|
if (CurrentState is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
LastError = new LastErrorInfo(CurrentState, errorMessage);
|
|
|
|
|
CurrentState = null;
|
|
|
|
|
Trace.TraceError($"{WorkNameForTrace} throw exception[{CountErrors}]: {errorMessage}");
|
2023-10-08 19:45:21 +05:00
|
|
|
|
}
|
|
|
|
|
}
|