Change events args
This commit is contained in:
@ -5,7 +5,6 @@ using Hcs.WebApp.BackgroundServices.OperationExecutors;
|
||||
using Hcs.WebApp.Config;
|
||||
using Hcs.WebApp.Services;
|
||||
using Hcs.WebApp.Utils;
|
||||
using System.Transactions;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices
|
||||
{
|
||||
@ -40,25 +39,28 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
var messageGuid = string.Empty;
|
||||
try
|
||||
{
|
||||
var startedAt = DateTime.UtcNow;
|
||||
await headquartersService.SetOperationStartedAsync(operation.Id, startedAt);
|
||||
|
||||
state.InvokeOnOperationStarted(operation.Id, operation.CampaignId, startedAt);
|
||||
|
||||
var executor = executorFactory.CreateExecutor(scope, client, operation);
|
||||
await headquartersService.SetOperationStartedAsync(operation.Id);
|
||||
|
||||
state.InvokeOnOperationStarted(operation);
|
||||
|
||||
messageGuid = await executor.ExecuteAsync(stoppingToken);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await headquartersService.SetOperationEndedWithFailAsync(operation.Id, e.CombineMessages());
|
||||
var endedAt = DateTime.UtcNow;
|
||||
var failureReason = e.CombineMessages();
|
||||
await headquartersService.SetOperationEndedWithFailAsync(operation.Id, endedAt, failureReason);
|
||||
|
||||
state.InvokeOnOperationEnded(operation);
|
||||
state.InvokeOnOperationEnded(operation.Id, operation.CampaignId, endedAt, failureReason);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(messageGuid))
|
||||
{
|
||||
await headquartersService.SetOperationMessageGuidAsync(operation.Id, messageGuid);
|
||||
|
||||
state.InvokeOnOperationExecuted(operation);
|
||||
state.InvokeOnOperationExecuted(operation.Id, operation.CampaignId, messageGuid);
|
||||
}
|
||||
|
||||
state.UnsetProcessingOperation(operation);
|
||||
|
||||
@ -5,14 +5,18 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
{
|
||||
public class OperationExecutionState
|
||||
{
|
||||
public delegate void OperationStarted(int operationId, int campaignId, DateTime startedAt);
|
||||
public delegate void OperationExecuted(int operationId, int campaignId, string messageGuid);
|
||||
public delegate void OperationEnded(int operationId, int campaignId, DateTime endedAt, string failureReason);
|
||||
|
||||
private readonly ConcurrentQueue<Operation> operationsInQueue = new();
|
||||
private readonly HashSet<Operation> operationsInProcess = [];
|
||||
|
||||
public bool Ready { get; set; }
|
||||
|
||||
public event Action<Operation> OnOperationStarted;
|
||||
public event Action<Operation> OnOperationExecuted;
|
||||
public event Action<Operation> OnOperationEnded;
|
||||
public event OperationStarted OnOperationStarted;
|
||||
public event OperationExecuted OnOperationExecuted;
|
||||
public event OperationEnded OnOperationEnded;
|
||||
|
||||
public void EnqueueOperation(Operation operation)
|
||||
{
|
||||
@ -39,29 +43,29 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
return operationsInQueue.Any(x => x.CampaignId == campaignId) || operationsInProcess.Any(x => x.CampaignId == campaignId);
|
||||
}
|
||||
|
||||
public void InvokeOnOperationStarted(Operation operation)
|
||||
public void InvokeOnOperationStarted(int operationId, int campaignId, DateTime startedAt)
|
||||
{
|
||||
try
|
||||
{
|
||||
OnOperationStarted?.Invoke(operation);
|
||||
OnOperationStarted?.Invoke(operationId, campaignId, startedAt);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public void InvokeOnOperationExecuted(Operation operation)
|
||||
public void InvokeOnOperationExecuted(int operationId, int campaignId, string messageGuid)
|
||||
{
|
||||
try
|
||||
{
|
||||
OnOperationExecuted?.Invoke(operation);
|
||||
OnOperationExecuted?.Invoke(operationId, campaignId, messageGuid);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public void InvokeOnOperationEnded(Operation operation)
|
||||
public void InvokeOnOperationEnded(int operationId, int campaignId, DateTime endedAt, string failureReason)
|
||||
{
|
||||
try
|
||||
{
|
||||
OnOperationEnded?.Invoke(operation);
|
||||
OnOperationEnded?.Invoke(operationId, campaignId, endedAt, failureReason);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
@ -2,6 +2,6 @@
|
||||
{
|
||||
public interface IResultGetter
|
||||
{
|
||||
Task<bool> GetAsync();
|
||||
Task<ResultGetterResponse> GetAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,16 +8,21 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters.Nsi
|
||||
{
|
||||
public class ExportDataProviderNsiItemGetter_15_7_0_1(IClient client, IServiceScope scope, Operation operation) : ResultGetterBase(client, scope, operation)
|
||||
{
|
||||
public override async Task<bool> GetAsync()
|
||||
public override async Task<ResultGetterResponse> GetAsync()
|
||||
{
|
||||
var result = await client.Nsi.GetExportDataProviderNsiItemResultAsync(operation.MessageGuid!);
|
||||
if (!result.Ready)
|
||||
{
|
||||
return false;
|
||||
return new ResultGetterResponse()
|
||||
{
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
DateTime endedAt = default;
|
||||
|
||||
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
|
||||
|
||||
@ -47,7 +52,8 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters.Nsi
|
||||
}
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
await headquartersService.SetOperationEndedAsync(context, operation.Id);
|
||||
endedAt = DateTime.UtcNow;
|
||||
await headquartersService.SetOperationEndedAsync(context, operation.Id, endedAt);
|
||||
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
@ -59,7 +65,11 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters.Nsi
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
return new ResultGetterResponse()
|
||||
{
|
||||
success = true,
|
||||
endedAt = endedAt
|
||||
};
|
||||
}
|
||||
|
||||
throw Failure(result.ErrorMessage);
|
||||
|
||||
@ -8,16 +8,21 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon
|
||||
{
|
||||
public class ExportNsiItemGetter_15_7_0_1(IClient client, IServiceScope scope, Operation operation) : ResultGetterBase(client, scope, operation)
|
||||
{
|
||||
public override async Task<bool> GetAsync()
|
||||
public override async Task<ResultGetterResponse> GetAsync()
|
||||
{
|
||||
var result = await client.NsiCommon.GetExportNsiItemResultAsync(operation.MessageGuid!);
|
||||
if (!result.Ready)
|
||||
{
|
||||
return false;
|
||||
return new ResultGetterResponse()
|
||||
{
|
||||
success = false
|
||||
};
|
||||
}
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
DateTime endedAt = default;
|
||||
|
||||
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
|
||||
|
||||
@ -47,7 +52,8 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon
|
||||
}
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
await headquartersService.SetOperationEndedAsync(context, operation.Id);
|
||||
endedAt = DateTime.UtcNow;
|
||||
await headquartersService.SetOperationEndedAsync(context, operation.Id, endedAt);
|
||||
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
@ -59,7 +65,11 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
return new ResultGetterResponse()
|
||||
{
|
||||
success = true,
|
||||
endedAt = endedAt
|
||||
};
|
||||
}
|
||||
|
||||
throw Failure(result.ErrorMessage);
|
||||
|
||||
@ -10,7 +10,7 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters
|
||||
protected readonly IServiceScope scope = scope;
|
||||
protected readonly Operation operation = operation;
|
||||
|
||||
public abstract Task<bool> GetAsync();
|
||||
public abstract Task<ResultGetterResponse> GetAsync();
|
||||
|
||||
protected Exception Failure(IErrorMessage? errorMessage)
|
||||
{
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
namespace Hcs.WebApp.BackgroundServices.ResultGetters
|
||||
{
|
||||
public struct ResultGetterResponse
|
||||
{
|
||||
public bool success;
|
||||
public DateTime endedAt;
|
||||
}
|
||||
}
|
||||
@ -59,6 +59,9 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
{
|
||||
entry.state.timer += ITERATION_TIME;
|
||||
|
||||
var endedAt = DateTime.MinValue;
|
||||
var failureReason = string.Empty;
|
||||
|
||||
var send = entry.state.attempt switch
|
||||
{
|
||||
0 => entry.state.timer >= 10000,
|
||||
@ -72,20 +75,22 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
try
|
||||
{
|
||||
var resultGetter = resultGetterFactory.CreateResultGetter(scope, client, entry.operation);
|
||||
var success = await resultGetter.GetAsync();
|
||||
if (success)
|
||||
var response = await resultGetter.GetAsync();
|
||||
if (response.success)
|
||||
{
|
||||
entry.state.done = true;
|
||||
|
||||
endedAt = response.endedAt;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
await headquartersService.SetOperationEndedWithFailAsync(entry.operation.Id, e.CombineMessages());
|
||||
endedAt = DateTime.UtcNow;
|
||||
failureReason = e.CombineMessages();
|
||||
await headquartersService.SetOperationEndedWithFailAsync(entry.operation.Id, endedAt, failureReason);
|
||||
|
||||
entry.state.done = true;
|
||||
|
||||
state.InvokeOnOperationEnded(entry.operation);
|
||||
}
|
||||
|
||||
entry.state.attempt++;
|
||||
@ -95,7 +100,7 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
if (entry.state.done)
|
||||
{
|
||||
state.UnsetProcessingOperation(entry.operation);
|
||||
state.InvokeOnOperationEnded(entry.operation);
|
||||
state.InvokeOnOperationEnded(entry.operation.Id, entry.operation.CampaignId, endedAt, failureReason);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -5,12 +5,14 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
{
|
||||
public class ResultWaitState
|
||||
{
|
||||
public delegate void OperationEnded(int operationId, int campaignId, DateTime endedAt, string failureReason);
|
||||
|
||||
private readonly ConcurrentQueue<Operation> operationsInQueue = new();
|
||||
private readonly HashSet<Operation> operationsInProcess = [];
|
||||
|
||||
public bool Ready { get; set; }
|
||||
|
||||
public event Action<Operation> OnOperationEnded;
|
||||
public event OperationEnded OnOperationEnded;
|
||||
|
||||
public void EnqueueOperation(Operation operation)
|
||||
{
|
||||
@ -37,11 +39,11 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
return operationsInQueue.Any(x => x.CampaignId == campaignId) || operationsInProcess.Any(x => x.CampaignId == campaignId);
|
||||
}
|
||||
|
||||
public void InvokeOnOperationEnded(Operation operation)
|
||||
public void InvokeOnOperationEnded(int operationId, int campaignId, DateTime endedAt, string failureReason)
|
||||
{
|
||||
try
|
||||
{
|
||||
OnOperationEnded?.Invoke(operation);
|
||||
OnOperationEnded?.Invoke(operationId, campaignId, endedAt, failureReason);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user