Skip to content

Commit

Permalink
[fix] 任务作业优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xianhc committed May 10, 2024
1 parent 77476d6 commit b678066
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 212 deletions.
67 changes: 57 additions & 10 deletions Ape.Volo.Api/Controllers/System/QuartzNetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Ape.Volo.Api.Controllers.Base;
using Ape.Volo.Common.Extention;
using Ape.Volo.Common.Global;
using Ape.Volo.Common.Helper;
using Ape.Volo.Common.Model;
using Ape.Volo.Entity.System;
Expand All @@ -12,6 +13,7 @@
using Ape.Volo.QuartzNetService.service;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Quartz;

namespace Ape.Volo.Api.Controllers.System;

Expand Down Expand Up @@ -63,6 +65,26 @@ public async Task<ActionResult<object>> Create(
return Error(actionError);
}

if (createUpdateQuartzNetDto.TriggerType == (int)TriggerMode.Cron)
{
if (createUpdateQuartzNetDto.Cron.IsNullOrEmpty())
{
return Error("cron模式下请设置作业执行cron表达式");
}

if (!CronExpression.IsValidExpression(createUpdateQuartzNetDto.Cron))
{
return Error("cron模式下请设置正确得cron表达式");
}
}
else if (createUpdateQuartzNetDto.TriggerType == (int)TriggerMode.Simple)
{
if (createUpdateQuartzNetDto.IntervalSecond <= 5)
{
return Error("simple模式下请设置作业间隔执行秒数");
}
}

var quartzNet = await _quartzNetService.CreateAsync(createUpdateQuartzNetDto);
if (quartzNet.IsNotNull())
{
Expand Down Expand Up @@ -95,20 +117,45 @@ public async Task<ActionResult<object>> Update(
return Error(actionError);
}

if (await _quartzNetService.UpdateAsync(createUpdateQuartzNetDto))
if (createUpdateQuartzNetDto.TriggerType == (int)TriggerMode.Cron)
{
var quartzNet = _mapper.Map<QuartzNet>(createUpdateQuartzNetDto);
if (quartzNet.IsEnable)
if (createUpdateQuartzNetDto.Cron.IsNullOrEmpty())
{
await _schedulerCenterService.StopScheduleJobAsync(quartzNet);
await _schedulerCenterService.AddScheduleJobAsync(quartzNet);
return Error("cron模式下请设置作业执行cron表达式");
}
else

if (!CronExpression.IsValidExpression(createUpdateQuartzNetDto.Cron))
{
await _schedulerCenterService.StopScheduleJobAsync(quartzNet);
return Error("cron模式下请设置正确得cron表达式");
}
}
else if (createUpdateQuartzNetDto.TriggerType == (int)TriggerMode.Simple)
{
if (createUpdateQuartzNetDto.IntervalSecond <= 5)
{
return Error("simple模式下请设置作业间隔执行秒数");
}
}


return NoContent();
if (await _quartzNetService.UpdateAsync(createUpdateQuartzNetDto))
{
var quartzNet = _mapper.Map<QuartzNet>(createUpdateQuartzNetDto);
var flag = await _schedulerCenterService.DeleteScheduleJobAsync(quartzNet);
if (flag)
{
if (quartzNet.IsEnable)
{
if (await _schedulerCenterService.AddScheduleJobAsync(quartzNet))
{
return NoContent();
}
}
else
{
return NoContent();
}
}
}

return Error();
Expand All @@ -135,7 +182,7 @@ public async Task<ActionResult<object>> Delete([FromBody] IdCollection idCollect
{
foreach (var item in quartzList)
{
await _schedulerCenterService.StopScheduleJobAsync(item);
await _schedulerCenterService.DeleteScheduleJobAsync(item);
}

return Success();
Expand Down Expand Up @@ -251,7 +298,7 @@ public async Task<ActionResult<object>> Pause(long id)
}

var triggerStatus = await _schedulerCenterService.GetTriggerStatus(_mapper.Map<QuartzNetDto>(quartzNet));
if (triggerStatus == "正常")
if (triggerStatus == "运行中")
{
//检查任务在内存状态
var isTrue = await _schedulerCenterService.IsExistScheduleJobAsync(quartzNet);
Expand Down
10 changes: 10 additions & 0 deletions Ape.Volo.Api/wwwroot/resources/db/sys_dict.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,15 @@
"UpdateBy": null,
"UpdateTime": null,
"IsDeleted": false
},
{
"Id": "1787853620566298624",
"Name": "task_trigger_type",
"Description": "作业触发器类型",
"CreateBy": "apevolo",
"CreateTime": "/Date(1609430400000+0800)/",
"UpdateBy": null,
"UpdateTime": null,
"IsDeleted": false
}
]
28 changes: 26 additions & 2 deletions Ape.Volo.Api/wwwroot/resources/db/sys_dict_detail.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"DictSort": "1",
"CreateBy": "apevolo",
"CreateTime": "/Date(1609430400000+0800)/",
"UpdateBy": "admin",
"UpdateBy": "",
"UpdateTime": null,
"IsDeleted": false
},
Expand All @@ -103,7 +103,7 @@
"DictSort": "1",
"CreateBy": "apevolo",
"CreateTime": "/Date(1609430400000+0800)/",
"UpdateBy": "admin",
"UpdateBy": "",
"UpdateTime": null,
"IsDeleted": false
},
Expand All @@ -118,5 +118,29 @@
"UpdateBy": null,
"UpdateTime": null,
"IsDeleted": false
},
{
"Id": "1787853921478250496",
"DictId": "1787853620566298624",
"Label": "cron",
"Value": "1",
"DictSort": "1",
"CreateBy": "apevolo",
"CreateTime": "/Date(1609430400000+0800)/",
"UpdateBy": "",
"UpdateTime": null,
"IsDeleted": false
},
{
"Id": "1787853979401588736",
"DictId": "1787853620566298624",
"Label": "simple",
"Value": "0",
"DictSort": "2",
"CreateBy": "apevolo",
"CreateTime": "/Date(1609430400000+0800)/",
"UpdateBy": null,
"UpdateTime": null,
"IsDeleted": false
}
]
10 changes: 5 additions & 5 deletions Ape.Volo.Api/wwwroot/resources/db/sys_quartz_job.tsv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"Id": "1378942344006471680",
"TaskName": "TestConsole",
"TaskName": "Console_Write",
"TaskGroup": "TestConsole",
"Cron": "*/5 * * * * ?",
"AssemblyName": "Ape.Volo.QuartzNetService",
Expand All @@ -10,7 +10,7 @@
"Principal": "apevolo",
"AlertEmail": null,
"PauseAfterFailure": 0,
"RunTimes": 2904,
"RunTimes": 0,
"StartTime": null,
"EndTime": null,
"TriggerType": 1,
Expand All @@ -26,16 +26,16 @@
},
{
"Id": "1380932818854481920",
"TaskName": "邮件队列",
"TaskName": "Email_Send",
"TaskGroup": "邮件作业",
"Cron": "*/5 * * * * ?",
"AssemblyName": "Ape.Volo.QuartzNetService",
"ClassName": "SendEmailJobService",
"Description": "执行邮件队列发送",
"Description": "执行邮件发送",
"Principal": "apevolo",
"AlertEmail": null,
"PauseAfterFailure": 0,
"RunTimes": 2387,
"RunTimes": 0,
"StartTime": null,
"EndTime": null,
"TriggerType": 1,
Expand Down
35 changes: 20 additions & 15 deletions Ape.Volo.Business/Monitor/ServerResourcesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ namespace Ape.Volo.Business.Monitor;

public class ServerResourcesService : IServerResourcesService
{
private const int B = 1;
private const int Kb = 1024 * B;
private const int Mb = 1024 * Kb;
private const int Gb = 1024 * Mb;

public async Task<ServerResourcesInfo> Query()
{
var os = new OsInfoTo();

ServerResourcesInfo osInfo = new ServerResourcesInfo
{
Time = DateTime.Now.ToString("HH:mm;ss"),
Time = DateTime.Now.ToString("HH:mm:ss"),
Sys = new Sys
{
Os = os.OsDescription,
Expand All @@ -35,21 +40,21 @@ public async Task<ServerResourcesInfo> Query()
},
Memory = new Memory
{
Total = Math.Round(Convert.ToDecimal(os.TotalPhysicalMemory) / 1024 / 1024 / 1024, 2)
Total = Math.Round(Convert.ToDecimal(os.TotalPhysicalMemory) / Gb, 2)
.ToString("0.00") + " GiB",
Used = Math.Round(Convert.ToDecimal(os.TotalPhysicalMemory - os.FreePhysicalMemory) / 1073741824, 2)
.ToString("0.00") + " GiB",
Available = Math.Round(Convert.ToDecimal(os.FreePhysicalMemory) / 1024 / 1024 / 1024, 1)
Available = Math.Round(Convert.ToDecimal(os.FreePhysicalMemory) / Gb, 1)
.ToString("0.00") + " GiB",
UsageRate = Math.Round(100 / Math.Round(Convert.ToDecimal(os.TotalPhysicalMemory) / 1073741824, 2) *
Math.Round(
Convert.ToDecimal(os.TotalPhysicalMemory - os.FreePhysicalMemory) /
1024 / 1024 / 1024, 2), 2).ToString("0.00") + " GiB"
Gb, 2), 2).ToString("0.00")
},
Swap = new Swap(),
Disk = new Disk()
};
if (os.LogicalDisk != null && os.LogicalDisk.Count > 0)
if (os.LogicalDisk is { Count: > 0 })
{
long size = 0;
long freeSpace = 0;
Expand All @@ -59,29 +64,29 @@ public async Task<ServerResourcesInfo> Query()
freeSpace += item.FreeSpace;
}

osInfo.Disk.Total = Math.Round(Convert.ToDecimal(size) / 1024 / 1024 / 1024, 2).ToString("0.00") +
osInfo.Disk.Total = Math.Round(Convert.ToDecimal(size) / Gb, 2).ToString("0.00") +
" GiB";
osInfo.Disk.Used =
Math.Round(Convert.ToDecimal(size - freeSpace) / 1024 / 1024 / 1024, 2).ToString("0.00") + " GiB";
Math.Round(Convert.ToDecimal(size - freeSpace) / Gb, 2).ToString("0.00") + " GiB";
osInfo.Disk.Available =
Math.Round(Convert.ToDecimal(freeSpace) / 1024 / 1024 / 1024, 2).ToString("0.00") + " GiB";
osInfo.Disk.UsageRate = Math.Round(100 / Math.Round(Convert.ToDecimal(size) / 1024 / 1024 / 1024, 2) *
Math.Round(Convert.ToDecimal(size - freeSpace) / 1024 / 1024 / 1024,
Math.Round(Convert.ToDecimal(freeSpace) / Gb, 2).ToString("0.00") + " GiB";
osInfo.Disk.UsageRate = Math.Round(100 / Math.Round(Convert.ToDecimal(size) / Gb, 2) *
Math.Round(Convert.ToDecimal(size - freeSpace) / Gb,
2), 2)
.ToString("0.00") + " GiB";
.ToString("0.00");
}

if (os.SwapTotal > 0)
{
osInfo.Swap.Total =
Math.Round(Convert.ToDecimal(os.SwapTotal / 1024 / 1024 / 1024), 2).ToString("0.00") + " GiB";
osInfo.Swap.Used = Math.Round(Convert.ToDecimal((os.SwapTotal - os.SwapFree) / 1024 / 1024 / 1024), 2)
Math.Round(Convert.ToDecimal(os.SwapTotal / Gb), 2).ToString("0.00") + " GiB";
osInfo.Swap.Used = Math.Round(Convert.ToDecimal((os.SwapTotal - os.SwapFree) / Gb), 2)
.ToString("0.00") + " GiB";
osInfo.Swap.Available =
Math.Round(Convert.ToDecimal(os.SwapFree / 1024 / 1024 / 1024), 2).ToString("0.00") + " GiB";
Math.Round(Convert.ToDecimal(os.SwapFree / Gb), 2).ToString("0.00") + " GiB";
osInfo.Swap.UsageRate =
Math.Round(Convert.ToDecimal(100 / os.SwapTotal * (os.SwapTotal - os.SwapFree)), 2)
.ToString("0.00") + " GiB";
.ToString("0.00");
}

await Task.CompletedTask;
Expand Down
25 changes: 25 additions & 0 deletions Ape.Volo.Business/System/QuartzNetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Ape.Volo.Business.Base;
using Ape.Volo.Common.AttributeExt;
using Ape.Volo.Common.Exception;
using Ape.Volo.Common.Extention;
using Ape.Volo.Common.Global;
using Ape.Volo.Common.Model;
Expand Down Expand Up @@ -47,12 +48,36 @@ public async Task<List<QuartzNet>> QueryAllAsync()

public async Task<QuartzNet> CreateAsync(CreateUpdateQuartzNetDto createUpdateQuartzNetDto)
{
if (await TableWhere(q =>
q.AssemblyName == createUpdateQuartzNetDto.AssemblyName &&
q.ClassName == createUpdateQuartzNetDto.ClassName).AnyAsync())
{
throw new BadRequestException(
$"作业执行目录=>{createUpdateQuartzNetDto.AssemblyName + "_" + createUpdateQuartzNetDto.ClassName}=>已存在!");
}

var quartzNet = ApeContext.Mapper.Map<QuartzNet>(createUpdateQuartzNetDto);
return await SugarRepository.AddReturnEntityAsync(quartzNet);
}

public async Task<bool> UpdateAsync(CreateUpdateQuartzNetDto createUpdateQuartzNetDto)
{
var oldQuartzNet =
await TableWhere(x => x.Id == createUpdateQuartzNetDto.Id).FirstAsync();
if (oldQuartzNet.IsNull())
{
throw new BadRequestException("数据不存在!");
}

if ((oldQuartzNet.AssemblyName != createUpdateQuartzNetDto.AssemblyName ||
oldQuartzNet.ClassName != createUpdateQuartzNetDto.ClassName) && await TableWhere(q =>
q.AssemblyName == createUpdateQuartzNetDto.AssemblyName &&
q.ClassName == createUpdateQuartzNetDto.ClassName).AnyAsync())
{
throw new BadRequestException(
$"作业执行目录=>{createUpdateQuartzNetDto.AssemblyName + "_" + createUpdateQuartzNetDto.ClassName}=>已存在!");
}

var quartzNet = ApeContext.Mapper.Map<QuartzNet>(createUpdateQuartzNetDto);
return await UpdateEntityAsync(quartzNet);
}
Expand Down
14 changes: 14 additions & 0 deletions Ape.Volo.Common/Global/TriggerMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Ape.Volo.Common.Global;

public enum TriggerMode
{
/// <summary>
/// 表达式
/// </summary>
Cron = 1,

/// <summary>
/// 简单的
/// </summary>
Simple = 0
}
3 changes: 1 addition & 2 deletions Ape.Volo.IBusiness/Dto/System/CreateUpdateQuartzNetDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class CreateUpdateQuartzNetDto : BaseEntityDto<long>
/// <summary>
/// cron 表达式
/// </summary>
[Required]
public string Cron { get; set; }

/// <summary>
Expand Down Expand Up @@ -81,7 +80,7 @@ public class CreateUpdateQuartzNetDto : BaseEntityDto<long>
/// <summary>
/// 触发器类型(0、simple 1、cron)
/// </summary>
public int TriggerType => Cron.IsNullOrEmpty() ? 0 : 1;
public int TriggerType { get; set; }

/// <summary>
/// 执行间隔时间, 秒为单位
Expand Down
5 changes: 0 additions & 5 deletions Ape.Volo.IBusiness/Dto/System/QuartzNetDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,4 @@ public class QuartzNetDto : BaseEntityDto<long>
/// 触发器状态
/// </summary>
public string TriggerStatus { get; set; }

/// <summary>
/// 触发器模式
/// </summary>
public string TriggerTypeStr => TriggerType == 1 ? "cron" : "simple";
}
Loading

0 comments on commit b678066

Please sign in to comment.