Files
hcs/Hcs.WebApp/BackgroundServices/CampaignManagers/ExportRequiredRegistryElementsManager_15_7_0_1.cs

60 lines
2.4 KiB
C#

using Hcs.WebApp.Data.Hcs;
using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{
public class ExportRequiredRegistryElementsManager_15_7_0_1(IServiceScope scope, OperationExecutionState operationExecutionState, Campaign campaign) : ManagerBase(scope, operationExecutionState, campaign)
{
public override async Task StartAsync()
{
if (campaign.Step > 0) return;
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
using var context = headquartersService.GetNewContext();
using var transaction = await context.Database.BeginTransactionAsync();
IEnumerable<Operation> operations = null;
try
{
await headquartersService.SetCampaignStartedAsync(context, campaign.Id);
await headquartersService.SetCampaignStepAsync(context, campaign.Id, 1);
var registryCount = await registryService.GetRegistryCountAsync(context);
operations = await headquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1);
await registryService.SetOperationsToRegistriesAsync(context, operations);
await transaction.CommitAsync();
}
catch
{
transaction?.Rollback();
throw;
}
if (operations != null)
{
foreach (var operation in operations)
{
operationExecutionState.EnqueueOperation(operation);
}
}
State = IManager.ManagerState.Started;
}
public override async Task CheckStateAsync()
{
if (State == IManager.ManagerState.Started)
{
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
var hasActiveOperations = await headquartersService.HasActiveOperationsAsync(campaign.Id);
if (!hasActiveOperations)
{
State = IManager.ManagerState.Ended;
await headquartersService.SetCampaignEndedAsync(campaign.Id);
}
}
}
}
}