refactor: SQL日志任务增加暂停逻辑

#Comment
防止任务暂停期间日志占用大量内存
This commit is contained in:
Argo Zhang 2019-11-15 11:27:35 +08:00
parent 624f584072
commit 9a8df6679a
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
2 changed files with 23 additions and 3 deletions

View File

@ -1,4 +1,5 @@
using Longbow.Tasks;
using Bootstrap.DataAccess;
using Longbow.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
@ -34,6 +35,13 @@ namespace Bootstrap.Admin.Controllers.Api
{
var sche = TaskServicesManager.Get(scheName);
if (sche != null) sche.Status = operType == "pause" ? SchedulerStatus.Disabled : SchedulerStatus.Running;
// SQL 日志任务特殊处理
if (scheName == "SQL日志")
{
if (operType == "pause") LogHelper.Pause();
else LogHelper.Run();
}
return true;
}
}

View File

@ -49,12 +49,23 @@ namespace Bootstrap.DataAccess
/// <param name="log"></param>
public static System.Threading.Tasks.Task AddDBLog(DBLog log) => System.Threading.Tasks.Task.Run(() =>
{
if (!_messageQueue.IsAddingCompleted)
if (!_messageQueue.IsAddingCompleted && !_pause)
{
_messageQueue.Add(log);
}
});
private static bool _pause;
/// <summary>
/// 暂停接收脚本执行日志
/// </summary>
public static void Pause() => _pause = true;
/// <summary>
/// 开始接收脚本执行日志
/// </summary>
public static void Run() => _pause = false;
/// <summary>
/// 查询所有SQL日志信息
/// </summary>
@ -80,7 +91,8 @@ namespace Bootstrap.DataAccess
var logs = new List<DBLog>();
while (_messageQueue.TryTake(out var log))
{
logs.Add(log);
if (log != null) logs.Add(log);
if (logs.Count >= 100) break;
}
if (logs.Any())
{