改进模拟数据生成器,代码质量优化
This commit is contained in:
@@ -64,7 +64,7 @@ public partial class MySqlDestination : IDisposable, IAsyncDisposable
|
||||
if (_recordCache.Count == 0)
|
||||
return;
|
||||
|
||||
var cmd = _conn.CreateCommand();
|
||||
await using var cmd = _conn.CreateCommand();
|
||||
cmd.CommandTimeout = 0;
|
||||
|
||||
try
|
||||
@@ -255,11 +255,13 @@ public partial class MySqlDestination : IDisposable, IAsyncDisposable
|
||||
{
|
||||
_conn.Close();
|
||||
_conn.Dispose();
|
||||
_recordCache.Clear();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await _conn.CloseAsync();
|
||||
await _conn.DisposeAsync();
|
||||
_recordCache.Clear();
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using ApplicationException = System.ApplicationException;
|
||||
using Serilog;
|
||||
using ApplicationException = System.ApplicationException;
|
||||
using TaskExtensions = MesETL.Shared.Helper.TaskExtensions;
|
||||
|
||||
namespace MesETL.App.Services;
|
||||
@@ -37,6 +38,8 @@ public class TaskManager
|
||||
{
|
||||
var task = Task.Run(async () =>
|
||||
{
|
||||
// Log.Logger.Verbose("[任务管理器] 新的任务已创建");
|
||||
Interlocked.Increment(ref _runningTaskCount);
|
||||
try
|
||||
{
|
||||
await func();
|
||||
@@ -45,13 +48,13 @@ public class TaskManager
|
||||
catch(Exception ex)
|
||||
{
|
||||
OnException?.Invoke(ex);
|
||||
Log.Logger.Error(ex, "[任务管理器] 执行任务时出错");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Interlocked.Decrement(ref _runningTaskCount);
|
||||
}
|
||||
}, cancellationToken);
|
||||
Interlocked.Increment(ref _runningTaskCount);
|
||||
return task;
|
||||
}
|
||||
|
||||
@@ -59,8 +62,10 @@ public class TaskManager
|
||||
{
|
||||
var task = Task.Factory.StartNew(async obj => // 性能考虑,这个lambda中不要捕获任何外部变量!
|
||||
{
|
||||
// Log.Logger.Verbose("[任务管理器] 新的任务已创建");
|
||||
if (obj is not Tuple<Func<object?, Task>, object?> tuple)
|
||||
throw new ApplicationException("这个异常不该出现");
|
||||
Interlocked.Increment(ref _runningTaskCount);
|
||||
try
|
||||
{
|
||||
await tuple.Item1(tuple.Item2);
|
||||
@@ -69,13 +74,13 @@ public class TaskManager
|
||||
catch(Exception ex)
|
||||
{
|
||||
OnException?.Invoke(ex);
|
||||
Log.Logger.Error(ex, "[任务管理器] 执行任务时出错");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Interlocked.Decrement(ref _runningTaskCount);
|
||||
}
|
||||
}, Tuple.Create(func, arg), cancellationToken).Unwrap();
|
||||
Interlocked.Increment(ref _runningTaskCount);
|
||||
return task;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user