| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- namespace Admin.NET.Plugin.AiDOP.DataPlatform.Schema;
- /// <summary>
- /// HTTP 已开始监听后再执行历史来源身份回填,避免数据迁移阻塞宿主启动。
- /// </summary>
- public sealed class MdpSourceIdentityBackfillWorker : BackgroundService
- {
- private readonly IServiceScopeFactory _scopeFactory;
- private readonly IHostApplicationLifetime _lifetime;
- private readonly ILogger<MdpSourceIdentityBackfillWorker> _logger;
- public MdpSourceIdentityBackfillWorker(
- IServiceScopeFactory scopeFactory,
- IHostApplicationLifetime lifetime,
- ILogger<MdpSourceIdentityBackfillWorker> logger)
- {
- _scopeFactory = scopeFactory;
- _lifetime = lifetime;
- _logger = logger;
- }
- protected override async Task ExecuteAsync(CancellationToken stoppingToken)
- {
- var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
- using var registration = _lifetime.ApplicationStarted.Register(() => started.TrySetResult());
- try
- {
- await started.Task.WaitAsync(stoppingToken);
- }
- catch (OperationCanceledException)
- {
- return;
- }
- try
- {
- using var scope = _scopeFactory.CreateScope();
- var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>();
- await MdpSchemaAligner.BackfillProvenSourceAsync(db);
- }
- catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
- {
- }
- catch (Exception ex)
- {
- _logger.LogWarning(ex, "中台历史来源身份回填失败;不影响 HTTP 服务");
- }
- }
- }
|