Use IExecutionStrategy
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
using Hcs.WebApp.Data.Hcs;
|
using Hcs.WebApp.Data.Hcs;
|
||||||
using Hcs.WebApp.Services;
|
using Hcs.WebApp.Services;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||||
{
|
{
|
||||||
@ -41,29 +42,33 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
|||||||
|
|
||||||
private async Task DoStartStepAsync()
|
private async Task DoStartStepAsync()
|
||||||
{
|
{
|
||||||
IEnumerable<Operation> operations;
|
IEnumerable<Operation>? operations = null;
|
||||||
|
|
||||||
using var context = HeadquartersService.GetNewContext();
|
using var context = HeadquartersService.GetNewContext();
|
||||||
using var transaction = await context.Database.BeginTransactionAsync();
|
var executionStrategy = context.Database.CreateExecutionStrategy();
|
||||||
try
|
await executionStrategy.ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
await HeadquartersService.SetCampaignStartedAsync(context, campaign.Id);
|
using var transaction = await context.Database.BeginTransactionAsync();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await HeadquartersService.SetCampaignStartedAsync(context, campaign.Id);
|
||||||
|
|
||||||
campaign.Step = (int)Step.ExecutionWait;
|
campaign.Step = (int)Step.ExecutionWait;
|
||||||
await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
|
await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
|
||||||
|
|
||||||
var registryCount = await RegistryService.GetRegistryCountAsync(context, true);
|
var registryCount = await RegistryService.GetRegistryCountAsync(context, true);
|
||||||
operations = await HeadquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1);
|
operations = await HeadquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1);
|
||||||
await RegistryService.SetOperationsToRegistriesAsync(context, true, operations);
|
await RegistryService.SetOperationsToRegistriesAsync(context, true, operations);
|
||||||
|
|
||||||
await transaction.CommitAsync();
|
await transaction.CommitAsync();
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
transaction?.Rollback();
|
transaction?.Rollback();
|
||||||
|
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (operations != null)
|
if (operations != null)
|
||||||
{
|
{
|
||||||
@ -98,22 +103,26 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
|||||||
if (resultWaitState.HasCampaignOperation(campaign.Id)) return;
|
if (resultWaitState.HasCampaignOperation(campaign.Id)) return;
|
||||||
|
|
||||||
using var context = HeadquartersService.GetNewContext();
|
using var context = HeadquartersService.GetNewContext();
|
||||||
using var transaction = await context.Database.BeginTransactionAsync();
|
var executionStrategy = context.Database.CreateExecutionStrategy();
|
||||||
try
|
await executionStrategy.ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
campaign.Step = (int)Step.End;
|
using var transaction = await context.Database.BeginTransactionAsync();
|
||||||
await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
|
try
|
||||||
|
{
|
||||||
|
campaign.Step = (int)Step.End;
|
||||||
|
await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
|
||||||
|
|
||||||
await HeadquartersService.SetCampaignEndedAsync(context, campaign.Id);
|
await HeadquartersService.SetCampaignEndedAsync(context, campaign.Id);
|
||||||
|
|
||||||
await transaction.CommitAsync();
|
await transaction.CommitAsync();
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
transaction?.Rollback();
|
transaction?.Rollback();
|
||||||
|
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
State = IManager.ManagerState.Ended;
|
State = IManager.ManagerState.Ended;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
using Hcs.Broker;
|
using Hcs.Broker;
|
||||||
using Hcs.WebApp.Data.Hcs;
|
using Hcs.WebApp.Data.Hcs;
|
||||||
using Hcs.WebApp.Services;
|
using Hcs.WebApp.Services;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon
|
namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon
|
||||||
@ -21,36 +22,40 @@ namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon
|
|||||||
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
|
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
|
||||||
|
|
||||||
using var context = headquartersService.GetNewContext();
|
using var context = headquartersService.GetNewContext();
|
||||||
using var transaction = await context.Database.BeginTransactionAsync();
|
var executionStrategy = context.Database.CreateExecutionStrategy();
|
||||||
try
|
await executionStrategy.ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
var registry = await registryService.GetRegistryByOperationIdAsync(context, operation.Id, true);
|
using var transaction = await context.Database.BeginTransactionAsync();
|
||||||
registry.SyncedAt = DateTime.UtcNow;
|
try
|
||||||
registry.Elements.Clear();
|
|
||||||
await context.SaveChangesAsync();
|
|
||||||
|
|
||||||
foreach (var element in result.Result!.NsiElement)
|
|
||||||
{
|
{
|
||||||
var registryElement = new RegistryElement()
|
var registry = await registryService.GetRegistryByOperationIdAsync(context, operation.Id, true);
|
||||||
|
registry.SyncedAt = DateTime.UtcNow;
|
||||||
|
registry.Elements.Clear();
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
foreach (var element in result.Result!.NsiElement)
|
||||||
{
|
{
|
||||||
Code = element.Code,
|
var registryElement = new RegistryElement()
|
||||||
GUID = element.GUID,
|
{
|
||||||
Json = JsonConvert.SerializeObject(element)
|
Code = element.Code,
|
||||||
};
|
GUID = element.GUID,
|
||||||
registry.Elements.Add(registryElement);
|
Json = JsonConvert.SerializeObject(element)
|
||||||
|
};
|
||||||
|
registry.Elements.Add(registryElement);
|
||||||
|
}
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
await headquartersService.SetOperationEndedAsync(context, operation.Id);
|
||||||
|
|
||||||
|
await transaction.CommitAsync();
|
||||||
}
|
}
|
||||||
await context.SaveChangesAsync();
|
catch
|
||||||
|
{
|
||||||
|
await transaction.RollbackAsync();
|
||||||
|
|
||||||
await headquartersService.SetOperationEndedAsync(context, operation.Id);
|
throw;
|
||||||
|
}
|
||||||
await transaction.CommitAsync();
|
});
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
transaction?.Rollback();
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,49 +44,54 @@ namespace Hcs.WebApp.Services
|
|||||||
public async Task UpdateUserAsync(string userId, string roleId, string password)
|
public async Task UpdateUserAsync(string userId, string roleId, string password)
|
||||||
{
|
{
|
||||||
using var context = factory.CreateDbContext();
|
using var context = factory.CreateDbContext();
|
||||||
using var transaction = await context.Database.BeginTransactionAsync();
|
var executionStrategy = context.Database.CreateExecutionStrategy();
|
||||||
try
|
await executionStrategy.ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
var curUserRole = await (from userRole in context.UserRoles
|
using var transaction = await context.Database.BeginTransactionAsync();
|
||||||
where userRole.UserId == userId
|
try
|
||||||
select userRole).SingleAsync();
|
|
||||||
var curRole = await (from role in context.Roles
|
|
||||||
where role.Id == curUserRole.RoleId
|
|
||||||
select role).SingleAsync();
|
|
||||||
if (curRole.Id != roleId)
|
|
||||||
{
|
{
|
||||||
context.UserRoles.Remove(curUserRole);
|
var curUserRole = await (from userRole in context.UserRoles
|
||||||
|
where userRole.UserId == userId
|
||||||
var newRole = await (from role in context.Roles
|
select userRole).SingleAsync();
|
||||||
where role.Id == roleId
|
var curRole = await (from role in context.Roles
|
||||||
|
where role.Id == curUserRole.RoleId
|
||||||
select role).SingleAsync();
|
select role).SingleAsync();
|
||||||
await context.UserRoles.AddAsync(new IdentityUserRole<string>()
|
if (curRole.Id != roleId)
|
||||||
{
|
{
|
||||||
UserId = userId,
|
context.UserRoles.Remove(curUserRole);
|
||||||
RoleId = newRole.Id
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var curUser = await (from user in context.Users
|
var newRole = await (from role in context.Roles
|
||||||
where user.Id == userId
|
where role.Id == roleId
|
||||||
select user).SingleAsync();
|
select role).SingleAsync();
|
||||||
var newPasswordHash = passwordHasher.HashPassword(curUser, password);
|
await context.UserRoles.AddAsync(new IdentityUserRole<string>()
|
||||||
if (curUser.PasswordHash != newPasswordHash)
|
{
|
||||||
|
UserId = userId,
|
||||||
|
RoleId = newRole.Id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var curUser = await (from user in context.Users
|
||||||
|
where user.Id == userId
|
||||||
|
select user).SingleAsync();
|
||||||
|
var newPasswordHash = passwordHasher.HashPassword(curUser, password);
|
||||||
|
if (curUser.PasswordHash != newPasswordHash)
|
||||||
|
{
|
||||||
|
curUser.PasswordHash = newPasswordHash;
|
||||||
|
|
||||||
|
context.Users.Update(curUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
{
|
{
|
||||||
curUser.PasswordHash = newPasswordHash;
|
await transaction.RollbackAsync();
|
||||||
|
|
||||||
context.Users.Update(curUser);
|
throw;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
await context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
await transaction.RollbackAsync();
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
await transaction.CommitAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IdentityResult> DeleteUserAsync(string userId)
|
public async Task<IdentityResult> DeleteUserAsync(string userId)
|
||||||
|
|||||||
Reference in New Issue
Block a user