😎恢复种子数据执行单线程(保证应用层重写系统种子数据最后执行)
This commit is contained in:
parent
a95b29757d
commit
acb465ce57
@ -7,7 +7,7 @@
|
||||
namespace Admin.NET.Application;
|
||||
|
||||
/// <summary>
|
||||
/// 系统菜单表种子数据
|
||||
/// 系统菜单表种子数据(执行顺序大于0且Id与框架一致,则代表重写系统种子数据)
|
||||
/// </summary>
|
||||
[SeedData(500)]
|
||||
[IncreSeed]
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
namespace Admin.NET.Application;
|
||||
|
||||
/// <summary>
|
||||
/// 系统租户表种子数据
|
||||
/// 系统租户表种子数据(执行顺序大于0且Id与框架一致,则代表重写系统种子数据)
|
||||
/// </summary>
|
||||
[SeedData(500)]
|
||||
public class SysTenantSeedData : ISqlSugarEntitySeedData<SysTenant>
|
||||
|
||||
@ -22,11 +22,6 @@ public class CommonConst
|
||||
/// </summary>
|
||||
public const string SysLogCategoryName = "System.Logging.LoggingMonitor";
|
||||
|
||||
/// <summary>
|
||||
/// 最大并发数
|
||||
/// </summary>
|
||||
public const int MaxConcurrent = 8;
|
||||
|
||||
/// <summary>
|
||||
/// 事件-增加异常日志
|
||||
/// </summary>
|
||||
|
||||
@ -391,26 +391,17 @@ public static class SqlSugarSetup
|
||||
else
|
||||
entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
|
||||
|
||||
var semaphore = new SemaphoreSlim(CommonConst.MaxConcurrent); // 并发限制数量
|
||||
int taskIndex = 0, size = entityTypes.Count;
|
||||
var taskList = entityTypes.Select(entityType => Task.Run(() =>
|
||||
{
|
||||
semaphore.Wait(); // 获取信号量许可
|
||||
try
|
||||
{
|
||||
var stopWatch = Stopwatch.StartNew(); // 开始计时
|
||||
var stopWatch = Stopwatch.StartNew(); // 开始计时
|
||||
|
||||
dbProvider.InitTable(entityType); // 同步表结构
|
||||
dbProvider.InitTable(entityType); // 初始化表结构
|
||||
|
||||
stopWatch.Stop(); // 停止计时
|
||||
stopWatch.Stop(); // 停止计时
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"初始化表 {entityType,-64} ({config.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003}) 耗时:{stopWatch.ElapsedMilliseconds} ms");
|
||||
}
|
||||
finally
|
||||
{
|
||||
semaphore.Release(); // 释放信号量许可
|
||||
}
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"初始化表 {entityType,-64} ({config.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003}) 耗时:{stopWatch.ElapsedMilliseconds} ms");
|
||||
}));
|
||||
Task.WaitAll(taskList.ToArray());
|
||||
}
|
||||
@ -525,30 +516,21 @@ public static class SqlSugarSetup
|
||||
.WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false))
|
||||
.OrderBy(u => u.GetCustomAttributes(typeof(SeedDataAttribute), false).Length > 0 ? ((SeedDataAttribute)u.GetCustomAttributes(typeof(SeedDataAttribute), false)[0]).Order : 0).ToList();
|
||||
|
||||
var semaphore = new SemaphoreSlim(CommonConst.MaxConcurrent); // 并发限制数量
|
||||
// 由于种子数据在应用层存在重写,必须保证应用层种子最后执行(多线程顺序会乱)
|
||||
int taskIndex = 0, size = seedDataTypes.Count;
|
||||
var taskList = seedDataTypes.Select(seedType => Task.Run(() =>
|
||||
foreach (var seedType in seedDataTypes)
|
||||
{
|
||||
semaphore.Wait(); // 获取信号量许可
|
||||
try
|
||||
{
|
||||
var stopWatch = Stopwatch.StartNew(); // 开始计时
|
||||
var stopWatch = Stopwatch.StartNew(); // 开始计时
|
||||
|
||||
// 种子数据初始化
|
||||
var tuple = db.InitTableSeedData(seedType);
|
||||
if (tuple == null) return;
|
||||
// 初始化种子数据
|
||||
var tuple = db.InitTableSeedData(seedType);
|
||||
if (tuple == null) return;
|
||||
|
||||
stopWatch.Stop(); // 停止计时
|
||||
stopWatch.Stop(); // 停止计时
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"初始化种子数据 {seedType.FullName,-58} ({config.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},数据量:{tuple.Value.Item1:D003},新增 {tuple.Value.Item2:D003} 条记录,更新 {tuple.Value.Item3:D003} 条记录,耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
|
||||
}
|
||||
finally
|
||||
{
|
||||
semaphore.Release(); // 释放信号量许可
|
||||
}
|
||||
}));
|
||||
Task.WaitAll(taskList.ToArray());
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"初始化种子数据 {seedType.FullName,-58} ({config.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},数据量:{tuple.Value.Item1:D003},新增 {tuple.Value.Item2:D003} 条记录,更新 {tuple.Value.Item3:D003} 条记录,耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user