OnlineUserJob.cs 1.0 KB

12345678910111213141516171819202122232425
  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.Create(async (_, scope) =>
  14. {
  15. var services = scope.ServiceProvider;
  16. var rep = services.GetService<SqlSugarRepository<SysOnlineUser>>();
  17. if (rep == null) return;
  18. await rep.AsDeleteable().ExecuteCommandAsync();
  19. Console.ForegroundColor = ConsoleColor.Blue;
  20. Console.WriteLine("【" + DateTime.Now + "——清空在线用户】\r\n服务重启触发清空在线用户");
  21. });
  22. }
  23. }