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;
using System.IO;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Furion.Logging;
namespace Myshipping.Core.Job;
///
/// 日志定时任务类
///
public class LogJobWorker : ISpareTimeWorker, IDynamicApiController, ITransient
{
// 日志暂存器,待写入
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 = 30;
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"]);
}
Log.Information($"准备清理天{daysAgo}之前的日志LogEx");
// 生成查询表达式
Expression> expression = ex => ex.ExceptionTime < DateTime.Now.AddDays(-daysAgo);
// 执行删除
DoDeleteWork(expression);
}
///
/// 定期删除操作日志
///
///
///
[SpareTime("@midnight", "LogOpDeletionService", Description = "后台定期删除操作日志,配置项参数:{\"daysAgo\": 30},不填默认为30",
DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
public void DoDeleteLogOp(SpareTimer timer, long count)
{
// 判断是否有异常
if (timer.Exception.Any())
{
// todo: 增加任务运行日志
}
// 默认值
var daysAgo = 30;
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"]);
}
Log.Information($"准备清理天{daysAgo}之前的日志LogOp");
//// 生成查询表达式
//Expression> expression = ex => ex.OpTime < DateTime.Now.AddDays(-daysAgo);
//// 执行删除
//DoDeleteWork(expression);
var repLogOp = App.GetService>();
repLogOp.Delete(x => x.OpTime < DateTime.Today.AddDays(-daysAgo));
}
///
/// 定期删除访问日志
///
///
///
[SpareTime("@midnight", "LogVisDeletionService", Description = "后台定期删除访问日志,配置项参数:{\"daysAgo\": 30},不填默认为30",
DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
public void DoDeleteLogVis(SpareTimer timer, long count)
{
// 判断是否有异常
if (timer.Exception.Any())
{
// todo: 增加任务运行日志
}
// 默认值
var daysAgo = 30;
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"]);
}
Log.Information($"准备清理天{daysAgo}之前的日志LogVis");
// 生成查询表达式
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).ExecuteCommand();
});
}
///
/// 后台批量删除临时文件
///
[SpareTime(120000, "TemporaryDocuments", Description = "后台批量删除临时文件,配置项参数:{\"quantity\": 2},不填默认为2",
DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
public void TemporaryDocuments(SpareTimer timer, long count)
{
var opt = App.GetOptions();
var dirAbs = opt.Path;
var RemainHours = Convert.ToDouble(opt.RemainHours);
var fileFullPath = Path.Combine(App.WebHostEnvironment.WebRootPath, dirAbs);
string[] Files = Directory.GetFiles(fileFullPath); //当前目录下的文件:
foreach (string it in Files)
{
FileInfo fi = new FileInfo(it);
var date = fi.CreationTime.AddHours(RemainHours);
if (date < DateTime.Now)
{
File.Delete(it);
}
}
string[] _Files = Directory.GetDirectories(fileFullPath); //当前目录下的文件夹:
if (_Files.Length > 0)
{
foreach (string it in _Files)
{
DirectoryInfo fi = new DirectoryInfo(it);
var date = fi.CreationTime.AddHours(RemainHours);
if (date < DateTime.Now)
{
fi.Delete(true);
}
else
{
TemporaryDirectories(it);
}
}
}
}
public void TemporaryDirectories(string path)
{
var opt = App.GetOptions();
var RemainHours = Convert.ToDouble(opt.RemainHours);
string[] Files = Directory.GetFiles(path); //当前目录下的文件:
foreach (string it in Files)
{
FileInfo fi = new FileInfo(it);
var date = fi.CreationTime.AddHours(RemainHours);
if (date < DateTime.Now)
{
File.Delete(it);
}
}
string[] _Files = Directory.GetDirectories(path); //当前目录下的文件夹:
if (_Files.Length > 0)
{
foreach (string it in _Files)
{
TemporaryDirectories(it);
}
}
else
{
DirectoryInfo fi = new DirectoryInfo(path);
fi.Delete(true);
}
}
}