OnlineUserJob.cs 990 B

123456789101112131415161718192021222324
  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. await rep.AsDeleteable().ExecuteCommandAsync();
  18. Console.ForegroundColor = ConsoleColor.Blue;
  19. Console.WriteLine("【" + DateTime.Now + "——清空在线人员】\r\n服务重启触发清空在线人员");
  20. });
  21. }
  22. }