feat: 添加定时任务及演示模块的更新
This commit is contained in:
@@ -14,13 +14,13 @@ namespace Yi.Framework.Rbac.Application.Contracts.Dtos.Task
|
||||
|
||||
public JobTypeEnum Type { get; set; }
|
||||
|
||||
public string Cron { get; set; }
|
||||
public string? Cron { get; set; }
|
||||
|
||||
public int Millisecond { get; set; }
|
||||
public int? Millisecond { get; set; }
|
||||
|
||||
public bool Concurrent { get; set; }
|
||||
|
||||
//public Dictionary<string, object>? Properties { get; set; }
|
||||
// public Dictionary<string, object>? Properties { get; set; }
|
||||
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Yi.Framework.Rbac.Application.Contracts.Dtos.Task
|
||||
{
|
||||
public class TaskGetListInput : PagedAllResultRequestDto
|
||||
{
|
||||
public string JobId { get; set; }
|
||||
public string GroupName { get; set; }
|
||||
public string? JobId { get; set; }
|
||||
public string? GroupName { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
/// 作业更新时间
|
||||
/// </summary>
|
||||
|
||||
public DateTime? UpdatedTime { get; internal set; }
|
||||
public DateTime? LastRunTime { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 标记其他作业正在执行
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace Yi.Framework.Rbac.Application.Contracts.Dtos.Task
|
||||
using Yi.Framework.Rbac.Domain.Shared.Enums;
|
||||
|
||||
namespace Yi.Framework.Rbac.Application.Contracts.Dtos.Task
|
||||
{
|
||||
public class TaskGetOutput
|
||||
{
|
||||
@@ -68,5 +70,15 @@
|
||||
public DateTime? LastRunTime { get; set; }
|
||||
|
||||
public long NumberOfRuns { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
public string Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发器类型
|
||||
/// </summary>
|
||||
public JobTypeEnum Type { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,18 +7,18 @@ namespace Yi.Framework.Rbac.Application.Contracts.Dtos.Task
|
||||
public string AssemblyName { get; set; }
|
||||
|
||||
public string JobType { get; set; }
|
||||
|
||||
public string JobId { get; set; }
|
||||
public string? GroupName { get; set; }
|
||||
|
||||
public JobTypeEnum Type { get; set; }
|
||||
|
||||
public string? Cron { get; set; }
|
||||
|
||||
public int Millisecond { get; set; }
|
||||
public int? Millisecond { get; set; }
|
||||
|
||||
public bool Concurrent { get; set; }
|
||||
|
||||
// public Dictionary<string, object>? Properties { get; set; }
|
||||
// public Dictionary<string, object>? Properties { get; set; }
|
||||
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Quartz;
|
||||
using Quartz.Impl.Matchers;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Volo.Abp.Timing;
|
||||
using Yi.Framework.Rbac.Application.Contracts.Dtos.Task;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Enums;
|
||||
@@ -14,8 +16,10 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
public class TaskService : ApplicationService, ITaskService
|
||||
{
|
||||
private readonly ISchedulerFactory _schedulerFactory;
|
||||
public TaskService(ISchedulerFactory schedulerFactory)
|
||||
private readonly IClock _clock;
|
||||
public TaskService(ISchedulerFactory schedulerFactory, IClock clock)
|
||||
{
|
||||
_clock=clock;
|
||||
_schedulerFactory = schedulerFactory;
|
||||
}
|
||||
|
||||
@@ -25,8 +29,8 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
/// </summary>
|
||||
/// <param name="jobId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{jobId}")]
|
||||
public async Task<TaskGetOutput> GetByIdAsync([FromRoute] string jobId)
|
||||
[HttpGet("task/{jobId}")]
|
||||
public async Task<TaskGetOutput> GetAsync([FromRoute] string jobId)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
|
||||
@@ -34,6 +38,8 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
var trigger = (await scheduler.GetTriggersOfJob(new JobKey(jobId))).First();
|
||||
//状态
|
||||
var state = await scheduler.GetTriggerState(trigger.Key);
|
||||
|
||||
|
||||
var output = new TaskGetOutput
|
||||
{
|
||||
JobId = jobDetail.Key.Name,
|
||||
@@ -42,17 +48,21 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
Properties = Newtonsoft.Json.JsonConvert.SerializeObject(jobDetail.JobDataMap),
|
||||
Concurrent = !jobDetail.ConcurrentExecutionDisallowed,
|
||||
Description = jobDetail.Description,
|
||||
LastRunTime = trigger.GetPreviousFireTimeUtc()?.DateTime,
|
||||
LastRunTime = _clock.Normalize( trigger.GetPreviousFireTimeUtc()?.DateTime??DateTime.MinValue),
|
||||
NextRunTime = _clock.Normalize(trigger.GetNextFireTimeUtc()?.DateTime ?? DateTime.MinValue),
|
||||
AssemblyName = jobDetail.JobType.Assembly.GetName().Name,
|
||||
Status = state.ToString()
|
||||
};
|
||||
|
||||
if (trigger is ISimpleTrigger simple)
|
||||
{
|
||||
output.TriggerArgs = simple.RepeatInterval.TotalMilliseconds.ToString() + "毫秒";
|
||||
|
||||
output.TriggerArgs =Math.Round(simple.RepeatInterval.TotalMinutes,2) .ToString() + "分钟";
|
||||
output.Type = JobTypeEnum.Millisecond;
|
||||
}
|
||||
else if (trigger is ICronTrigger cron)
|
||||
{
|
||||
output.TriggerArgs = cron.CronExpressionString!;
|
||||
output.Type = JobTypeEnum.Cron;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
@@ -61,8 +71,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
/// 多查job
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("")]
|
||||
public async Task<PagedResultDto<TaskGetListOutput>> GetList([FromQuery] TaskGetListInput input)
|
||||
public async Task<PagedResultDto<TaskGetListOutput>> GetListAsync([FromQuery] TaskGetListInput input)
|
||||
{
|
||||
var items = new List<TaskGetOutput>();
|
||||
|
||||
@@ -77,7 +86,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
string jobName = jobKey.Name;
|
||||
string jobGroup = jobKey.Group;
|
||||
var triggers = (await scheduler.GetTriggersOfJob(jobKey)).First();
|
||||
items.Add(await GetByIdAsync(jobName));
|
||||
items.Add(await GetAsync(jobName));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +102,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Create(TaskCreateInput input)
|
||||
public async Task CreateAsync(TaskCreateInput input)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
|
||||
@@ -101,7 +110,12 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
|
||||
|
||||
//jobBuilder
|
||||
var jobClassType = Assembly.LoadFrom(input.AssemblyName).GetType(input.JobType);
|
||||
var jobClassType = Assembly.Load(input.AssemblyName).GetTypes().Where(x => x.Name == input.JobType).FirstOrDefault();
|
||||
|
||||
if (jobClassType is null)
|
||||
{
|
||||
throw new UserFriendlyException($"程序集:{input.AssemblyName},{input.JobType} 不存在");
|
||||
}
|
||||
|
||||
var jobBuilder = JobBuilder.Create(jobClassType).WithIdentity(new JobKey(input.JobId, input.GroupName))
|
||||
.WithDescription(input.Description);
|
||||
@@ -115,8 +129,8 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
switch (input.Type)
|
||||
{
|
||||
case JobTypeEnum.Cron:
|
||||
triggerBuilder =
|
||||
TriggerBuilder.Create().StartNow()
|
||||
triggerBuilder =
|
||||
TriggerBuilder.Create()
|
||||
.WithCronSchedule(input.Cron);
|
||||
|
||||
|
||||
@@ -127,7 +141,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
triggerBuilder =
|
||||
TriggerBuilder.Create().StartNow()
|
||||
.WithSimpleSchedule(x => x
|
||||
.WithInterval(TimeSpan.FromMilliseconds(input.Millisecond))
|
||||
.WithInterval(TimeSpan.FromMilliseconds(input.Millisecond ?? 10000))
|
||||
.RepeatForever()
|
||||
);
|
||||
break;
|
||||
@@ -140,12 +154,12 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
/// <summary>
|
||||
/// 移除job
|
||||
/// </summary>
|
||||
/// <param name="jobId"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Remove(string jobId)
|
||||
{
|
||||
public async Task DeleteAsync(IEnumerable<string> id)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
await scheduler.ResumeJob(new JobKey(jobId));
|
||||
await scheduler.DeleteJobs(id.Select(x => new JobKey(x)).ToList());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -154,7 +168,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
/// <param name="jobId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut]
|
||||
public async Task Pause(string jobId)
|
||||
public async Task PauseAsync(string jobId)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
await scheduler.PauseJob(new JobKey(jobId));
|
||||
@@ -166,7 +180,7 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
/// <param name="jobId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut]
|
||||
public async Task Start(string jobId)
|
||||
public async Task StartAsync(string jobId)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
await scheduler.ResumeJob(new JobKey(jobId));
|
||||
@@ -175,28 +189,30 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
/// <summary>
|
||||
/// 更新job
|
||||
/// </summary>
|
||||
/// <param name="jobId"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Update(string jobId, TaskUpdateInput input)
|
||||
public async Task UpdateAsync(string id, TaskUpdateInput input)
|
||||
{
|
||||
await Remove(jobId);
|
||||
await Create(input.Adapt<TaskCreateInput>());
|
||||
await DeleteAsync(new List<string>() { id });
|
||||
await CreateAsync(input.Adapt<TaskCreateInput>());
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task RunOnce(string jobId)
|
||||
[HttpPost("task/run-once/{id}")]
|
||||
public async Task RunOnceAsync([FromRoute] string id)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
var jobDetail = await scheduler.GetJobDetail(new JobKey(jobId));
|
||||
var jobDetail = await scheduler.GetJobDetail(new JobKey(id));
|
||||
|
||||
var jobBuilder = JobBuilder.Create(jobDetail.JobType).WithIdentity(new JobKey(Guid.NewGuid().ToString()));
|
||||
//设置启动时执行一次,然后最大只执行一次
|
||||
var trigger = TriggerBuilder.Create().WithIdentity(Guid.NewGuid().ToString()).StartNow()
|
||||
.WithSimpleSchedule(x => x
|
||||
.WithIntervalInHours(1)
|
||||
.WithRepeatCount(1))
|
||||
.Build();
|
||||
|
||||
await scheduler.ScheduleJob(jobDetail, trigger);
|
||||
await scheduler.ScheduleJob(jobBuilder.Build(), trigger);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
56
Yi.Abp.Net8/src/Yi.Abp.Application/Jobs/DemoResetJob.cs
Normal file
56
Yi.Abp.Net8/src/Yi.Abp.Application/Jobs/DemoResetJob.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
using Quartz.Logging;
|
||||
using Volo.Abp.BackgroundWorkers.Quartz;
|
||||
using Volo.Abp.Data;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Abp.Application.Jobs
|
||||
{
|
||||
public class DemoResetJob : QuartzBackgroundWorkerBase
|
||||
{
|
||||
private ISqlSugarDbContext _dbContext;
|
||||
private ILogger<DemoResetJob> _logger => LoggerFactory.CreateLogger<DemoResetJob>();
|
||||
private IDataSeeder _dataSeeder;
|
||||
private IConfiguration _configuration;
|
||||
public DemoResetJob(ISqlSugarDbContext dbContext, IDataSeeder dataSeeder, IConfiguration configuration)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
JobDetail = JobBuilder.Create<DemoResetJob>().WithIdentity(nameof(DemoResetJob)).Build();
|
||||
|
||||
//每天01点与13点,演示环境进行重置
|
||||
Trigger = TriggerBuilder.Create().WithIdentity(nameof(DemoResetJob)).WithCronSchedule("0 0 1,13 * * ? ").Build();
|
||||
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(DemoResetJob)).WithSimpleSchedule(x=>x.WithIntervalInSeconds(10)).Build();
|
||||
_dataSeeder = dataSeeder;
|
||||
_configuration = configuration;
|
||||
}
|
||||
public override async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
//开启演示环境重置功能
|
||||
if (_configuration["EnableDemoReset"] == "true")
|
||||
{
|
||||
//定时任务,非常简单
|
||||
_logger.LogWarning("演示环境正在还原!");
|
||||
var db = _dbContext.SqlSugarClient.CopyNew();
|
||||
db.DbMaintenance.TruncateTable<UserEntity>();
|
||||
db.DbMaintenance.TruncateTable<UserRoleEntity>();
|
||||
db.DbMaintenance.TruncateTable<RoleEntity>();
|
||||
db.DbMaintenance.TruncateTable<RoleMenuEntity>();
|
||||
db.DbMaintenance.TruncateTable<MenuEntity>();
|
||||
db.DbMaintenance.TruncateTable<RoleDeptEntity>();
|
||||
db.DbMaintenance.TruncateTable<DeptEntity>();
|
||||
db.DbMaintenance.TruncateTable<PostEntity>();
|
||||
db.DbMaintenance.TruncateTable<UserPostEntity>();
|
||||
|
||||
//删除种子数据
|
||||
await _dataSeeder.SeedAsync();
|
||||
_logger.LogWarning("演示环境还原成功!");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Jobs\" />
|
||||
<Folder Include="EventHandlers\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -36,10 +36,11 @@ export function updateJob(jobId,data) {
|
||||
}
|
||||
|
||||
// 删除定时任务调度
|
||||
export function delJob(jobId) {
|
||||
export function delJob(ids) {
|
||||
return request({
|
||||
url: '/task/' + jobId,
|
||||
method: 'delete'
|
||||
url: '/task',
|
||||
method: 'delete',
|
||||
params:{id:ids}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@
|
||||
<el-table-column label="Job参数" align="center" prop="properties" :show-overflow-tooltip="true" />
|
||||
|
||||
<el-table-column label="是否并行" align="center" prop="concurrent" :show-overflow-tooltip="true" />
|
||||
<el-table-column label="最后更新时间" align="center" prop="updatedTime" :show-overflow-tooltip="true" />
|
||||
<el-table-column label="最后执行时间" align="center" prop="lastRunTime" :show-overflow-tooltip="true" />
|
||||
|
||||
<el-table-column label="状态" align="center" prop="status" :show-overflow-tooltip="true" />
|
||||
<el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true" />
|
||||
@@ -151,14 +151,14 @@
|
||||
v-hasPermi="['monitor:job:query']"
|
||||
></el-button>
|
||||
</el-tooltip>
|
||||
<el-tooltip content="调度日志" placement="top">
|
||||
<!-- <el-tooltip content="调度日志" placement="top">
|
||||
<el-button
|
||||
type="text"
|
||||
icon="Operation"
|
||||
@click="handleJobLog(scope.row)"
|
||||
v-hasPermi="['monitor:job:query']"
|
||||
></el-button>
|
||||
</el-tooltip>
|
||||
</el-tooltip> -->
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
@@ -248,7 +248,7 @@
|
||||
|
||||
|
||||
</el-col>
|
||||
<el-col v-show="form.type==0" :span="24">
|
||||
<el-col v-show="form.type=='Cron'" :span="24">
|
||||
<el-form-item label="cron表达式" prop="cron">
|
||||
<el-input v-model="form.cron" placeholder="请输入cron执行表达式">
|
||||
<template #append>
|
||||
@@ -260,7 +260,7 @@
|
||||
</el-input>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col v-show="form.type==1" :span="24">
|
||||
<el-col v-show="form.type=='Millisecond'" :span="24">
|
||||
<el-form-item label="定时毫秒间隔" prop="millisecond">
|
||||
<el-input v-model="form.millisecond" placeholder="请输入毫秒间隔">
|
||||
</el-input>
|
||||
@@ -269,8 +269,8 @@
|
||||
<el-col :span="24">
|
||||
<el-form-item label="执行策略" prop="type">
|
||||
<el-radio-group v-model="form.type">
|
||||
<el-radio-button :label=0>Cron表达式</el-radio-button>
|
||||
<el-radio-button :label=1>简单毫秒间隔</el-radio-button>
|
||||
<el-radio-button label="Cron">Cron表达式</el-radio-button>
|
||||
<el-radio-button label="Millisecond">简单毫秒间隔</el-radio-button>
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
@@ -293,7 +293,7 @@
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
</el-col> -->
|
||||
|
||||
|
||||
|
||||
<el-col :span="24">
|
||||
<el-form-item label="描述" prop="description">
|
||||
@@ -335,10 +335,7 @@
|
||||
<el-form-item label="已执行次数:">{{ form.numberOfRuns }}</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="12">
|
||||
<el-form-item label="任务状态:">
|
||||
<div v-if="form.status == 0">正常</div>
|
||||
<div v-else-if="form.status == 1">失败</div>
|
||||
</el-form-item>
|
||||
<el-form-item label="任务状态:">{{ form.status }}</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="12">
|
||||
<el-form-item label="是否并发:">
|
||||
@@ -427,6 +424,7 @@ function cancel() {
|
||||
/** 表单重置 */
|
||||
function reset() {
|
||||
form.value = {
|
||||
oldJobId:undefined,
|
||||
jobId: undefined,
|
||||
groupName: undefined,
|
||||
type: undefined,
|
||||
@@ -525,8 +523,10 @@ function handleUpdate(row) {
|
||||
reset();
|
||||
IsAdd.value=false;
|
||||
const jobId = row.jobId || ids.value;
|
||||
|
||||
getJob(jobId).then(response => {
|
||||
form.value = response.data;
|
||||
form.value.oldJobId=jobId;
|
||||
open.value = true;
|
||||
title.value = "修改任务";
|
||||
});
|
||||
@@ -545,7 +545,7 @@ function submitForm() {
|
||||
});
|
||||
} else {
|
||||
|
||||
updateJob(form.value.jobId,form.value).then(response => {
|
||||
updateJob(form.value.oldJobId,form.value).then(response => {
|
||||
proxy.$modal.msgSuccess("修改成功");
|
||||
open.value = false;
|
||||
getList();
|
||||
|
||||
Reference in New Issue
Block a user