fix(centralui): run long/concurrent page actions in their own DI scope (UI-2, UI-3, UI-8, WP5 task 5.2)
- Deployments: every LoadDataAsync flight reads through its own scope, so overlapping flights (push reload, debounce, page/tile click) no longer collide on the circuit DbContext; the generation guard still orders results. - Sites: Deploy Artifacts (per site and to all sites) resolves ArtifactDeploymentService from a fresh scope, so the post-fan-out record, audit rows and revision stamp neither share the circuit context nor die with the circuit. - Topology: Diff and Enable/Disable/Delete resolve DeploymentService from their own scope (like Deploy/Deploy all), so they cannot overlap the 15 s live tick; drops the unused AreaService/InstanceService injections. - New ScopedServiceExtensions.InNewScopeAsync helper. Tests: DeploymentsPerFlightScopeTests, SitesPageTests DeployToAllSites_RunsInItsOwnScope_DisposedAfterTheAction, TopologyPageTests UI8_InstanceActions_RunOnTheirOwnScope (all fail on the pre-fix pages).
This commit is contained in:
@@ -12,7 +12,8 @@
|
||||
@attribute [Authorize(Policy = AuthorizationPolicies.RequireAdmin)]
|
||||
@inject ISiteRepository SiteRepository
|
||||
@inject IPendingUpdatesCache PendingUpdates
|
||||
@inject ArtifactDeploymentService ArtifactDeploymentService
|
||||
@inject Microsoft.Extensions.DependencyInjection.IServiceScopeFactory ScopeFactory
|
||||
@using Microsoft.Extensions.DependencyInjection
|
||||
@inject CommunicationService CommunicationService
|
||||
@inject AuthenticationStateProvider AuthStateProvider
|
||||
@inject NavigationManager NavigationManager
|
||||
@@ -314,8 +315,11 @@
|
||||
try
|
||||
{
|
||||
var user = await GetCurrentUserAsync();
|
||||
var result = await ArtifactDeploymentService.RetryForSiteAsync(
|
||||
site.Id, site.SiteIdentifier, user);
|
||||
// Own DI scope (UI-3): the deploy's record, audit rows and revision stamp are all
|
||||
// written after the site fan-out, so it must neither share the circuit's context
|
||||
// with whatever the operator does next nor die with the circuit if the tab closes.
|
||||
var result = await ScopeFactory.InNewScopeAsync(
|
||||
(ArtifactDeploymentService svc) => svc.RetryForSiteAsync(site.Id, site.SiteIdentifier, user));
|
||||
|
||||
succeeded = result.IsSuccess;
|
||||
if (succeeded)
|
||||
@@ -349,7 +353,9 @@
|
||||
try
|
||||
{
|
||||
var user = await GetCurrentUserAsync();
|
||||
var result = await ArtifactDeploymentService.DeployToAllSitesAsync(user);
|
||||
// Own DI scope (UI-3) — see DeployArtifacts.
|
||||
var result = await ScopeFactory.InNewScopeAsync(
|
||||
(ArtifactDeploymentService svc) => svc.DeployToAllSitesAsync(user));
|
||||
|
||||
succeeded = result.IsSuccess;
|
||||
if (succeeded)
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
@using ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment
|
||||
@using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums
|
||||
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDeployment)]
|
||||
@inject IDeploymentManagerRepository DeploymentManagerRepository
|
||||
@inject Microsoft.Extensions.DependencyInjection.IServiceScopeFactory ScopeFactory
|
||||
@using Microsoft.Extensions.DependencyInjection
|
||||
@inject ZB.MOM.WW.ScadaBridge.CentralUI.Auth.SiteScopeService SiteScope
|
||||
@inject ZB.MOM.WW.ScadaBridge.DeploymentManager.IDeploymentStatusNotifier DeploymentStatusNotifier
|
||||
@implements IDisposable
|
||||
@@ -615,13 +616,20 @@
|
||||
SiteIdScope: siteScope,
|
||||
SearchMode: _searchContains ? DeploymentSearchMode.Contains : DeploymentSearchMode.Prefix);
|
||||
|
||||
var page = await DeploymentManagerRepository
|
||||
// Each flight reads through its OWN DI scope (UI-2): flights overlap by design
|
||||
// (push reload, search debounce, page/tile click), and two of them on the one
|
||||
// circuit-scoped DbContext would throw "A second operation was started on this
|
||||
// context instance". The generation guard still decides whose results apply.
|
||||
await using var readScope = ScopeFactory.CreateAsyncScope();
|
||||
var deployments = readScope.ServiceProvider.GetRequiredService<IDeploymentManagerRepository>();
|
||||
|
||||
var page = await deployments
|
||||
.QueryDeploymentListPageAsync(filter, pageToQuery, PageSize);
|
||||
|
||||
// The tile counts deliberately ignore the status filter (see
|
||||
// GetDeploymentStatusCountsAsync) so each tile keeps showing its own
|
||||
// total while it is the one selected.
|
||||
var counts = await DeploymentManagerRepository.GetDeploymentStatusCountsAsync(filter);
|
||||
var counts = await deployments.GetDeploymentStatusCountsAsync(filter);
|
||||
|
||||
// A newer flight has started since this one began (its generation is
|
||||
// now the current one) — this flight's results are stale. Discard
|
||||
@@ -646,7 +654,7 @@
|
||||
if (_currentPage > _totalPages && _totalPages >= 1)
|
||||
{
|
||||
_currentPage = _totalPages;
|
||||
var reread = await DeploymentManagerRepository
|
||||
var reread = await deployments
|
||||
.QueryDeploymentListPageAsync(filter, _currentPage, PageSize);
|
||||
|
||||
if (generation != _requestGeneration)
|
||||
|
||||
@@ -18,9 +18,6 @@
|
||||
@inject ITemplateEngineRepository TemplateEngineRepository
|
||||
@inject ISiteRepository SiteRepository
|
||||
@inject IDeploymentManagerRepository DeploymentManagerRepository
|
||||
@inject DeploymentService DeploymentService
|
||||
@inject AreaService AreaService
|
||||
@inject InstanceService InstanceService
|
||||
@inject AuthenticationStateProvider AuthStateProvider
|
||||
@inject ZB.MOM.WW.ScadaBridge.CentralUI.Auth.SiteScopeService SiteScope
|
||||
@inject NavigationManager NavigationManager
|
||||
@@ -977,7 +974,9 @@
|
||||
{
|
||||
var user = await GetCurrentUserAsync(inst.Id);
|
||||
if (user is null) return;
|
||||
var result = await DeploymentService.DeleteInstanceAsync(inst.Id, user);
|
||||
// Own DI scope (UI-8): the lifecycle command must not share the circuit's context
|
||||
// with the 15 s live tick.
|
||||
var result = await ScopeFactory.InNewScopeAsync((DeploymentService svc) => svc.DeleteInstanceAsync(inst.Id, user));
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
_toast.ShowSuccess($"Instance '{inst.UniqueName}' deleted.");
|
||||
@@ -1003,7 +1002,7 @@
|
||||
{
|
||||
var user = await GetCurrentUserAsync(inst.Id);
|
||||
if (user is null) return;
|
||||
var result = await DeploymentService.EnableInstanceAsync(inst.Id, user);
|
||||
var result = await ScopeFactory.InNewScopeAsync((DeploymentService svc) => svc.EnableInstanceAsync(inst.Id, user));
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
_toast.ShowSuccess($"Instance '{inst.UniqueName}' enabled.");
|
||||
@@ -1035,7 +1034,7 @@
|
||||
{
|
||||
var user = await GetCurrentUserAsync(inst.Id);
|
||||
if (user is null) return;
|
||||
var result = await DeploymentService.DisableInstanceAsync(inst.Id, user);
|
||||
var result = await ScopeFactory.InNewScopeAsync((DeploymentService svc) => svc.DisableInstanceAsync(inst.Id, user));
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
_toast.ShowSuccess($"Instance '{inst.UniqueName}' disabled.");
|
||||
@@ -1175,7 +1174,9 @@
|
||||
string? diffError = null;
|
||||
try
|
||||
{
|
||||
var result = await DeploymentService.GetDeploymentComparisonAsync(inst.Id);
|
||||
// Own DI scope (UI-8): the diff re-flattens the instance (a multi-query read) and
|
||||
// must not overlap the live tick on the circuit's context.
|
||||
var result = await ScopeFactory.InNewScopeAsync((DeploymentService svc) => svc.GetDeploymentComparisonAsync(inst.Id));
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
diffResult = result.Value;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Runs one page action against a service resolved from its own DI scope (and therefore its
|
||||
/// own <c>ScadaBridgeDbContext</c>), instead of the circuit-lifetime scope the page lives in.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Use this for every page action that <b>mutates</b> configuration through a scoped service
|
||||
/// (template / folder / shared-script edits, deploys, lifecycle commands, bundle import) and for
|
||||
/// long or concurrent reads that could overlap another query on the circuit context
|
||||
/// (code review 2026-12-02, UI-1/UI-2/UI-3/UI-8).
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The circuit's own context runs with a no-tracking default
|
||||
/// (<c>CircuitDbContextTrackingHandler</c>), so a load-modify-save service run on it would
|
||||
/// silently persist nothing. A fresh scope gets an ordinary tracking context that starts empty,
|
||||
/// so the service always reads the current row and its write lands; the scope is disposed when
|
||||
/// the action ends, so nothing stale is left behind for the next action, and the action does
|
||||
/// not depend on (or die with) the circuit's scope.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static class ScopedServiceExtensions
|
||||
{
|
||||
/// <summary>Resolves <typeparamref name="TService"/> from a fresh scope and runs <paramref name="action"/> on it.</summary>
|
||||
/// <typeparam name="TService">The scoped service to resolve.</typeparam>
|
||||
/// <typeparam name="TResult">The action's result type.</typeparam>
|
||||
/// <param name="scopes">The root scope factory.</param>
|
||||
/// <param name="action">The action to run; it must finish with the scope (do not return deferred work).</param>
|
||||
/// <returns>The action's result.</returns>
|
||||
public static async Task<TResult> InNewScopeAsync<TService, TResult>(
|
||||
this IServiceScopeFactory scopes, Func<TService, Task<TResult>> action)
|
||||
where TService : notnull
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(scopes);
|
||||
ArgumentNullException.ThrowIfNull(action);
|
||||
await using var scope = scopes.CreateAsyncScope();
|
||||
return await action(scope.ServiceProvider.GetRequiredService<TService>());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user