|
@@ -17,6 +17,8 @@ using JLHHJSvr.Excutor.APP;
|
|
|
using JLHHJSvr.LJLib.HttpServer;
|
|
|
using LJLib;
|
|
|
using LJLib.TextLog;
|
|
|
+using JLHHJSvr.Tools;
|
|
|
+using JLHHJSvr.Helper;
|
|
|
|
|
|
namespace JLHHJSvr
|
|
|
{
|
|
@@ -277,6 +279,71 @@ namespace JLHHJSvr
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 定时任务
|
|
|
+ /// </summary>
|
|
|
+ public static void StartTimer()
|
|
|
+ {
|
|
|
+ var manager = new DailySchedulerManager();
|
|
|
+ manager.AddTask("ClearExpireLock", new TimeSpan(2, 0, 0),() => ClearExpireLock());
|
|
|
+ manager.AddTask("ClearOldLogs", new TimeSpan(2, 0, 0),() => CleanupOldLogs());
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 清理7天前的请求日志文件
|
|
|
+ /// </summary>
|
|
|
+ public static void CleanupOldLogs()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var files = Directory.GetFiles(Path.Combine(App_Data, "log"), "REQUEST_*.log");
|
|
|
+ DateTime threshold = DateTime.Now.AddDays(-7);
|
|
|
+
|
|
|
+ foreach (var file in files)
|
|
|
+ {
|
|
|
+ string fileName = Path.GetFileNameWithoutExtension(file);
|
|
|
+ // 格式: REQUEST_yyyyMMdd
|
|
|
+ if (fileName.Length == "REQUEST_yyyyMMdd".Length &&
|
|
|
+ DateTime.TryParseExact(fileName.Replace("REQUEST_", ""),
|
|
|
+ "yyyyMMdd",
|
|
|
+ null,
|
|
|
+ System.Globalization.DateTimeStyles.None,
|
|
|
+ out DateTime logDate))
|
|
|
+ {
|
|
|
+ if (logDate < threshold.Date)
|
|
|
+ {
|
|
|
+ File.Delete(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch(Exception ex)
|
|
|
+ {
|
|
|
+ Trace.Write(ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 清理过期锁
|
|
|
+ /// </summary>
|
|
|
+ public static void ClearExpireLock()
|
|
|
+ {
|
|
|
+ using (SqlConnection con = new SqlConnection(GlobalVar.ConnectionString))
|
|
|
+ using (SqlCommand cmd = con.CreateCommand())
|
|
|
+ {
|
|
|
+ con.Open();
|
|
|
+ using (cmd.Transaction = con.BeginTransaction())
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ LockHelper.ClearExpireLock(cmd);
|
|
|
+ cmd.Transaction.Commit();
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ cmd.Transaction?.Rollback();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
public static string ConnectionString { get; set; }
|
|
|
|