using Furion; using Furion.DependencyInjection; using Furion.TaskScheduler; using Myshipping.Core.Entity; using Myshipping.Core.Service; using Microsoft.Extensions.DependencyInjection; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace Myshipping.Core.Job; /// /// 日志定时任务类 /// public class LogJobWorker : ISpareTimeWorker { // 日志暂存器,待写入 private readonly List _sysLogExs = new(); private readonly List _sysLogOps = new(); private readonly List _sysLogViss = new(); /// /// 定期删除异常日志 /// /// /// [SpareTime("@midnight", "LogExDeletionService", Description = "后台定期删除异常日志,配置项参数:{\"daysAgo\": 30},不填默认为30", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void DoDeleteLogEx(SpareTimer timer, long count) { // 判断是否有异常 if (timer.Exception.Any()) { // todo: 增加任务运行日志 } // 默认值 var daysAgo = 15; var sysCache = App.GetRequiredService(); // 获取写数据库容量阀值,新增定时任务时会将配置项写入缓存 if (sysCache != null) { var parameters = sysCache.Get>("LogDeletionService_Parameters"); // 如果存在相关配置项 if (parameters != null && parameters.ContainsKey("daysAgo") && string.IsNullOrEmpty(parameters["daysAgo"])) daysAgo = int.Parse(parameters["daysAgo"]); } // 生成查询表达式 Expression> expression = ex => ex.ExceptionTime < DateTime.Now.AddDays(-daysAgo); // 执行删除 DoDeleteWork(expression); } /// /// 定期删除操作日志 /// /// /// [SpareTime("@midnight", "LogOpDeletionService", Description = "后台定期删除操作日志,配置项参数:{\"daysAgo\": 7},不填默认为7", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void DoDeleteLogOp(SpareTimer timer, long count) { // 判断是否有异常 if (timer.Exception.Any()) { // todo: 增加任务运行日志 } // 默认值 var daysAgo = 7; var sysCache = App.GetRequiredService(); // 获取写数据库容量阀值,新增定时任务时会将配置项写入缓存 if (sysCache != null) { var parameters = sysCache.Get>("LogOpDeletionService_Parameters"); // 如果存在相关配置项 if (parameters != null && parameters.ContainsKey("daysAgo") && string.IsNullOrEmpty(parameters["daysAgo"])) daysAgo = int.Parse(parameters["daysAgo"]); } // 生成查询表达式 Expression> expression = ex => ex.OpTime < DateTime.Now.AddDays(-daysAgo); // 执行删除 DoDeleteWork(expression); } /// /// 定期删除访问日志 /// /// /// [SpareTime("@midnight", "LogVisDeletionService", Description = "后台定期删除访问日志,配置项参数:{\"daysAgo\": 15},不填默认为15", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void DoDeleteLogVis(SpareTimer timer, long count) { // 判断是否有异常 if (timer.Exception.Any()) { // todo: 增加任务运行日志 } // 默认值 var daysAgo = 15; var sysCache = App.GetRequiredService(); // 获取写数据库容量阀值,新增定时任务时会将配置项写入缓存 if (sysCache != null) { var parameters = sysCache.Get>("LogVisDeletionService_Parameters"); // 如果存在相关配置项 if (parameters != null && parameters.ContainsKey("daysAgo") && string.IsNullOrEmpty(parameters["daysAgo"])) daysAgo = int.Parse(parameters["daysAgo"]); } // 生成查询表达式 Expression> expression = ex => ex.VisTime < DateTime.Now.AddDays(-daysAgo); // 执行删除 DoDeleteWork(expression); } /// /// 后台批量写错误日志 /// /// /// [SpareTime(10000, "LogExWritingService", Description = "后台批量写错误日志,配置项参数:{\"quantity\": 2},不填默认为2", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void DoLogEx(SpareTimer timer, long count) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"DoLogEx: {DateTime.Now}"); return; //DoWork(timer, count, "LogExWritingService_Parameters", _sysLogExs); } /// /// 后台批量写操作日志 /// /// /// [SpareTime(5000, "LogOpWritingService", Description = "后台批量写操作日志,配置项参数:{\"quantity\": 2},不填默认为2", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void DoLogOp(SpareTimer timer, long count) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"DoLogOp: {DateTime.Now}"); return; //DoWork(timer, count, "LogOpWritingService_Parameters", _sysLogOps); } /// /// 后台批量写访问日志 /// /// /// [SpareTime(8000, "LogVisWritingService", Description = "后台批量写访问日志,配置项参数:{\"quantity\": 2},不填默认为2", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void DoLogVis(SpareTimer timer, long count) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"DoLogVis: {DateTime.Now}"); return; //DoWork(timer, count, "LogVisWritingService_Parameters", _sysLogViss); } /// /// 写日志 /// /// /// /// /// /// private void DoWork(SpareTimer timer, long count, string cacheKey, List logs) where T : class, new() { // 判断是否有异常 if (timer.Exception.Any()) { // todo: 增加任务运行日志 } Scoped.Create((_, scope) => { var services = scope.ServiceProvider; var logRep =App.GetService(services); var sysCache = services.GetRequiredService(); // 默认值 var quantity = 2; // 获取写数据库容量阀值,新增定时任务时会将配置项写入缓存 if (sysCache != null) { var parameters = sysCache.Get>(cacheKey); // 如果存在相关配置项 if (parameters != null && parameters.ContainsKey("quantity") && string.IsNullOrEmpty(parameters["quantity"])) quantity = int.Parse(parameters["quantity"]); } // 后台队列中产生了日志,取出写入暂存器 int queue = SimpleQueue.Count(); if (queue > 0) { for (var i = 0; i < queue; i++) { if (SimpleQueue.Try(out T obj)) logs.Add(obj); } } // 达到系统配置的容量则写入数据库 if (logs.Count > quantity) { logRep.Insertable(logs).ExecuteCommand(); logs.Clear(); } }); } /// /// 根据条件删除日志 /// /// /// private void DoDeleteWork(Expression> expression) where T : class, new() { Scoped.Create((_, scope) => { var services = scope.ServiceProvider; var logRep = App.GetService(services); logRep.Deleteable(expression); }); } }