Add transaction support between services
This commit is contained in:
@ -20,24 +20,23 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
{
|
||||
await InitializeStateAsync();
|
||||
|
||||
using var scope = scopeFactory.CreateScope();
|
||||
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
while (campaignManagementState.TryDequeueCampaign(out var campaign))
|
||||
{
|
||||
if (stoppingToken.IsCancellationRequested) return;
|
||||
|
||||
using var scope = scopeFactory.CreateScope();
|
||||
try
|
||||
{
|
||||
var manager = managerFactory.CreateManager(campaign);
|
||||
var manager = managerFactory.CreateManager(scope, campaign);
|
||||
await manager.StartAsync(stoppingToken);
|
||||
|
||||
managers.Add(manager);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
await headquartersService.SetCampaignEndedWithFail(campaign.Id, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,12 +1,24 @@
|
||||
using Hcs.WebApp.Data.Hcs;
|
||||
using Hcs.WebApp.Services;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
{
|
||||
public class ExportRequiredRegistryElementsManager_15_7_0_1(OperationExecutionState state, IServiceScopeFactory scopeFactory, Campaign campaign) : ManagerBase(state, scopeFactory, campaign)
|
||||
public class ExportRequiredRegistryElementsManager_15_7_0_1(IServiceScope scope, OperationExecutionState state, Campaign campaign) : ManagerBase(scope, state, campaign)
|
||||
{
|
||||
public override async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
// TODO
|
||||
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
|
||||
using var context = headquartersService.GetNewContext();
|
||||
using var transaction = await context.Database.BeginTransactionAsync(cancellationToken);
|
||||
try
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
{
|
||||
public abstract class ManagerBase(OperationExecutionState state, IServiceScopeFactory scopeFactory, Campaign campaign) : IManager
|
||||
public abstract class ManagerBase(IServiceScope scope, OperationExecutionState state, Campaign campaign) : IManager
|
||||
{
|
||||
protected readonly IServiceScope scope = scope;
|
||||
protected readonly OperationExecutionState state = state;
|
||||
protected readonly IServiceScopeFactory scopeFactory = scopeFactory;
|
||||
protected readonly Campaign campaign = campaign;
|
||||
|
||||
public IManager.ManagerState State { get; } = IManager.ManagerState.Created;
|
||||
|
||||
@ -2,17 +2,16 @@
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
{
|
||||
public class ManagerFactory(OperationExecutionState state, IServiceScopeFactory scopeFactory)
|
||||
public class ManagerFactory(OperationExecutionState state)
|
||||
{
|
||||
protected readonly OperationExecutionState state = state;
|
||||
protected readonly IServiceScopeFactory scopeFactory = scopeFactory;
|
||||
|
||||
public IManager CreateManager(Campaign campaign)
|
||||
public IManager CreateManager(IServiceScope scope, Campaign campaign)
|
||||
{
|
||||
switch (campaign.Type)
|
||||
{
|
||||
case Campaign.CampaignType.ExportRequiredRegistryElements_15_7_0_1:
|
||||
return new ExportRequiredRegistryElementsManager_15_7_0_1(state, scopeFactory, campaign);
|
||||
return new ExportRequiredRegistryElementsManager_15_7_0_1(scope, state, campaign);
|
||||
}
|
||||
|
||||
throw new NotImplementedException();
|
||||
|
||||
@ -25,19 +25,18 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
|
||||
InitializeClient();
|
||||
|
||||
var scope = scopeFactory.CreateScope();
|
||||
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
while (state.TryDequeueOperation(out var operation))
|
||||
{
|
||||
if (stoppingToken.IsCancellationRequested) return;
|
||||
|
||||
var scope = scopeFactory.CreateScope();
|
||||
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
var messageGuid = string.Empty;
|
||||
try
|
||||
{
|
||||
var executor = executorFactory.CreateExecutor(client, operation);
|
||||
var executor = executorFactory.CreateExecutor(scope, client, operation);
|
||||
await headquartersService.SetOperationStarted(operation.Id);
|
||||
messageGuid = await executor.ExecuteAsync(stoppingToken);
|
||||
}
|
||||
|
||||
@ -3,10 +3,10 @@ using Hcs.WebApp.Data.Hcs;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.OperationExecutors
|
||||
{
|
||||
public abstract class ExecutorBase(IClient client, IServiceScopeFactory scopeFactory, Operation operation) : IExecutor
|
||||
public abstract class ExecutorBase(IClient client, IServiceScope scope, Operation operation) : IExecutor
|
||||
{
|
||||
protected readonly IClient client = client;
|
||||
protected readonly IServiceScopeFactory scopeFactory = scopeFactory;
|
||||
protected readonly IServiceScope scope = scope;
|
||||
protected readonly Operation operation = operation;
|
||||
|
||||
public abstract Task<string> ExecuteAsync(CancellationToken cancellationToken);
|
||||
|
||||
@ -4,16 +4,14 @@ using Hcs.WebApp.Data.Hcs;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.OperationExecutors
|
||||
{
|
||||
public class ExecutorFactory(IServiceScopeFactory scopeFactory)
|
||||
public class ExecutorFactory
|
||||
{
|
||||
protected readonly IServiceScopeFactory scopeFactory = scopeFactory;
|
||||
|
||||
public IExecutor CreateExecutor(IClient client, Operation operation)
|
||||
public IExecutor CreateExecutor(IServiceScope scope, IClient client, Operation operation)
|
||||
{
|
||||
switch (operation.Type)
|
||||
{
|
||||
case Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1:
|
||||
return new ExportNsiItemExecutor_15_7_0_1(client, scopeFactory, operation);
|
||||
return new ExportNsiItemExecutor_15_7_0_1(client, scope, operation);
|
||||
}
|
||||
|
||||
throw new NotImplementedException();
|
||||
|
||||
@ -5,13 +5,12 @@ using Hcs.WebApp.Services;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.OperationExecutors.NsiCommon
|
||||
{
|
||||
public class ExportNsiItemExecutor_15_7_0_1(IClient client, IServiceScopeFactory scopeFactory, Operation operation) : ExecutorBase(client, scopeFactory, operation)
|
||||
public class ExportNsiItemExecutor_15_7_0_1(IClient client, IServiceScope scope, Operation operation) : ExecutorBase(client, scope, operation)
|
||||
{
|
||||
public override async Task<string> ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using var scope = scopeFactory.CreateScope();
|
||||
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
|
||||
var registry = await registryService.GetRegistryByOperationId(operation.Id);
|
||||
var registry = await registryService.GetRegistryByOperationIdAsync(operation.Id);
|
||||
return await client.NsiCommon.RequestExportNsiItemAsync(registry.Number, ListGroup.NSI, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user