OnlineUserJob.cs 1.0 KB

123456789101112131415161718192021222324252627
  1. namespace Admin.NET.Core.Job;
  2. /// <summary>
  3. /// 在线用户任务调度
  4. /// </summary>
  5. public class OnlineUserJob : ISpareTimeWorker
  6. {
  7. /// <summary>
  8. /// 服务重启清空在线用户(防止僵尸用户,掉线用户会自动重连)
  9. /// </summary>
  10. [SpareTime(1000, "服务重启清空在线用户", Description = "服务重启清空在线用户", DoOnce = true, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
  11. public void ClearOnlineUser(SpareTimer timer, long count)
  12. {
  13. Scoped.CreateAsync(async (_, scope) =>
  14. {
  15. var services = scope.ServiceProvider;
  16. var rep = services.GetService<SqlSugarRepository<SysOnlineUser>>();
  17. await rep.AsDeleteable().ExecuteCommandAsync();
  18. Console.ForegroundColor = ConsoleColor.Red;
  19. Console.WriteLine("【" + DateTime.Now + "】服务重启清空在线用户");
  20. // 缓存多租户
  21. await services.GetService<SysTenantService>().UpdateTenantCache();
  22. });
  23. }
  24. }