DigitalFactory/Admin.NET/Admin.NET.Core/Job/OnlineUserJob.cs

36 lines
1.4 KiB
C#
Raw Normal View History

2024-03-18 01:36:19 +00:00
// 大名科技(天津)有限公司版权所有 电话18020030720 QQ515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
namespace Admin.NET.Core;
/// <summary>
/// 清理在线用户作业任务
/// </summary>
[JobDetail("job_onlineUser", Description = "清理在线用户", GroupName = "default", Concurrent = false)]
[PeriodSeconds(1, TriggerId = "trigger_onlineUser", Description = "清理在线用户", MaxNumberOfRuns = 1, RunOnStart = true)]
public class OnlineUserJob : IJob
{
private readonly IServiceScopeFactory _scopeFactory;
public OnlineUserJob(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
{
using var serviceScope = _scopeFactory.CreateScope();
var rep = serviceScope.ServiceProvider.GetService<SqlSugarRepository<SysOnlineUser>>();
await rep.CopyNew().AsDeleteable().ExecuteCommandAsync(stoppingToken);
var originColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("【" + DateTime.Now + "】服务重启清空在线用户");
Console.ForegroundColor = originColor;
// 缓存租户列表
await serviceScope.ServiceProvider.GetService<SysTenantService>().CacheTenant();
}
}