All checks were successful
Build and Deploy / deploy (push) Successful in 1m0s
预报计费失败不再把订单留在处理中,轨迹、面单和自动触发在创单提交后各自入队。 Co-authored-by: Cursor <cursoragent@cursor.com>
50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Fake.UnitOfWork;
|
|
|
|
namespace Oms.Application.Messaging;
|
|
|
|
/// <summary>
|
|
/// 把创单/预报之后的副作用延后到当前事务提交成功再执行,每一步单独提交。
|
|
/// </summary>
|
|
internal static class OmsAfterCommit
|
|
{
|
|
public static async Task ScheduleAsync(IUnitOfWorkManager unitOfWorkManager, Func<Task> action)
|
|
{
|
|
var current = unitOfWorkManager.Current;
|
|
if (current != null && !current.IsCompleted)
|
|
{
|
|
current.OnCompleted(action);
|
|
return;
|
|
}
|
|
|
|
await action();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新开独立事务执行一步。失败只记日志,不抛回调用方。
|
|
/// </summary>
|
|
public static async Task<bool> RunStepAsync(
|
|
IServiceScopeFactory scopeFactory,
|
|
ILogger logger,
|
|
string step,
|
|
Func<IServiceProvider, Task> work)
|
|
{
|
|
try
|
|
{
|
|
using var scope = scopeFactory.CreateScope();
|
|
var unitOfWorkManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
|
|
using var unitOfWork = unitOfWorkManager.Begin(OmsOutboxUnitOfWork.Independent());
|
|
await OmsIntegrationEventPublisher.EnsureOutboxDbContextAsync(unitOfWorkManager);
|
|
await work(scope.ServiceProvider);
|
|
await unitOfWork.CompleteAsync();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "订单后置步骤失败 Step={Step}", step);
|
|
return false;
|
|
}
|
|
}
|
|
}
|