refactor: 将async void方法改为async Task并优化异步操作
This commit is contained in:
parent
a73b136f3c
commit
aad540a7bc
@ -37,7 +37,7 @@ public class TestService : IDynamicApiController
|
|||||||
/// Redis事件测试 - Payload 🔖
|
/// Redis事件测试 - Payload 🔖
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async void EventTestAsync()
|
public async Task EventTestAsync()
|
||||||
{
|
{
|
||||||
await _eventPublisher.PublishAsync(CommonConst.SendErrorMail, "Admin.NET");
|
await _eventPublisher.PublishAsync(CommonConst.SendErrorMail, "Admin.NET");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -57,8 +57,8 @@ public sealed class RedisEventSourceStorer : IEventSourceStorer, IDisposable
|
|||||||
FullRedis redis = (FullRedis)cacheProvider.Cache;
|
FullRedis redis = (FullRedis)cacheProvider.Cache;
|
||||||
var clusterOpt = App.GetConfig<ClusterOptions>("Cluster", true);
|
var clusterOpt = App.GetConfig<ClusterOptions>("Cluster", true);
|
||||||
_queueBroadcast = redis.GetStream<string>(routeKey + ":broadcast");
|
_queueBroadcast = redis.GetStream<string>(routeKey + ":broadcast");
|
||||||
_queueBroadcast.Group = clusterOpt.ServerId;//根据服务器标识分配到不同的分组里
|
_queueBroadcast.Group = clusterOpt.ServerId; //根据服务器标识分配到不同的分组里
|
||||||
_queueBroadcast.Expire = TimeSpan.FromSeconds(10);//消息10秒过期()
|
_queueBroadcast.Expire = TimeSpan.FromSeconds(10); //消息10秒过期()
|
||||||
_queueBroadcast.ConsumeAsync(OnConsumeBroadcast);
|
_queueBroadcast.ConsumeAsync(OnConsumeBroadcast);
|
||||||
|
|
||||||
// 创建队列消息订阅者,只要有一个服务节点消费了消息即可
|
// 创建队列消息订阅者,只要有一个服务节点消费了消息即可
|
||||||
@ -98,6 +98,7 @@ public sealed class RedisEventSourceStorer : IEventSourceStorer, IDisposable
|
|||||||
Console.WriteLine($"有消息要处理{ces.EventId},{ces.Payload}");
|
Console.WriteLine($"有消息要处理{ces.EventId},{ces.Payload}");
|
||||||
Console.ForegroundColor = oriColor;
|
Console.ForegroundColor = oriColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
await _channel.Writer.WriteAsync(ces, cancel);
|
await _channel.Writer.WriteAsync(ces, cancel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,9 +152,9 @@ public sealed class RedisEventSourceStorer : IEventSourceStorer, IDisposable
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 释放非托管资源
|
/// 释放非托管资源
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
await _eventConsumer.Stop();
|
_eventConsumer.Stop().GetAwaiter().GetResult();
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -344,7 +344,7 @@ public class SysAuthService : IDynamicApiController, ITransient
|
|||||||
/// 退出系统 🔖
|
/// 退出系统 🔖
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DisplayName("退出系统")]
|
[DisplayName("退出系统")]
|
||||||
public async void Logout()
|
public async Task Logout()
|
||||||
{
|
{
|
||||||
var httpContext = _httpContextAccessor.HttpContext ?? throw Oops.Oh(ErrorCodeEnum.D1016);
|
var httpContext = _httpContextAccessor.HttpContext ?? throw Oops.Oh(ErrorCodeEnum.D1016);
|
||||||
|
|
||||||
|
|||||||
@ -23,19 +23,19 @@ public class JobClusterServer : IJobClusterServer
|
|||||||
/// 当前作业调度器启动通知
|
/// 当前作业调度器启动通知
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context">作业集群服务上下文</param>
|
/// <param name="context">作业集群服务上下文</param>
|
||||||
public async void Start(JobClusterContext context)
|
public void Start(JobClusterContext context)
|
||||||
{
|
{
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
using var scope = _serviceScopeFactory.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
||||||
|
|
||||||
// 在作业集群表中,如果 clusterId 不存在,则新增一条(否则更新一条),并设置 status 为 ClusterStatus.Waiting
|
// 在作业集群表中,如果 clusterId 不存在,则新增一条(否则更新一条),并设置 status 为 ClusterStatus.Waiting
|
||||||
if (await db.Queryable<SysJobCluster>().AnyAsync(u => u.ClusterId == context.ClusterId))
|
if (db.Queryable<SysJobCluster>().Any(u => u.ClusterId == context.ClusterId))
|
||||||
{
|
{
|
||||||
await db.Updateable<SysJobCluster>().SetColumns(u => u.Status == ClusterStatus.Waiting).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
|
db.Updateable<SysJobCluster>().SetColumns(u => u.Status == ClusterStatus.Waiting).Where(u => u.ClusterId == context.ClusterId).ExecuteCommand();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await db.Insertable(new SysJobCluster { ClusterId = context.ClusterId, Status = ClusterStatus.Waiting }).ExecuteCommandAsync();
|
db.Insertable(new SysJobCluster { ClusterId = context.ClusterId, Status = ClusterStatus.Waiting }).ExecuteCommand();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +72,9 @@ public class JobClusterServer : IJobClusterServer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch { }
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,24 +82,24 @@ public class JobClusterServer : IJobClusterServer
|
|||||||
/// 当前作业调度器停止通知
|
/// 当前作业调度器停止通知
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context">作业集群服务上下文</param>
|
/// <param name="context">作业集群服务上下文</param>
|
||||||
public async void Stop(JobClusterContext context)
|
public void Stop(JobClusterContext context)
|
||||||
{
|
{
|
||||||
// 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed
|
// 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
using var scope = _serviceScopeFactory.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
||||||
await db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
|
db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前作业调度器宕机
|
/// 当前作业调度器宕机
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context">作业集群服务上下文</param>
|
/// <param name="context">作业集群服务上下文</param>
|
||||||
public async void Crash(JobClusterContext context)
|
public void Crash(JobClusterContext context)
|
||||||
{
|
{
|
||||||
// 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed
|
// 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
using var scope = _serviceScopeFactory.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
||||||
await db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
|
db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user