feat: 增加任务编辑功能
This commit is contained in:
parent
34d1f87a51
commit
1ba16e0e4b
|
@ -3,6 +3,7 @@ using Longbow;
|
||||||
using Longbow.Tasks;
|
using Longbow.Tasks;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
@ -55,8 +56,6 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
{
|
{
|
||||||
// 判断 Cron 表达式
|
// 判断 Cron 表达式
|
||||||
if (string.IsNullOrEmpty(widget.CronExpression)) return false;
|
if (string.IsNullOrEmpty(widget.CronExpression)) return false;
|
||||||
// 判断 任务是否已经存在
|
|
||||||
if (TaskServicesManager.Get(widget.Name) != null) return false;
|
|
||||||
|
|
||||||
// 加载任务执行体
|
// 加载任务执行体
|
||||||
// 此处可以扩展为任意 DLL 中的任意继承 ITask 接口的实体类
|
// 此处可以扩展为任意 DLL 中的任意继承 ITask 接口的实体类
|
||||||
|
@ -64,9 +63,35 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
if (taskExecutor == null) return false;
|
if (taskExecutor == null) return false;
|
||||||
|
|
||||||
// 此处未存储到数据库中,直接送入任务中心
|
// 此处未存储到数据库中,直接送入任务中心
|
||||||
|
TaskServicesManager.Remove(widget.Name);
|
||||||
var expression = Longbow.Tasks.Cron.ParseCronExpression(widget.CronExpression);
|
var expression = Longbow.Tasks.Cron.ParseCronExpression(widget.CronExpression);
|
||||||
TaskServicesManager.GetOrAdd(widget.Name, token => taskExecutor.Execute(token), TriggerBuilder.Build(widget.CronExpression));
|
TaskServicesManager.GetOrAdd(widget.Name, token => taskExecutor.Execute(token), TriggerBuilder.Build(widget.CronExpression));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<string> _tasks = new string[] {
|
||||||
|
"单次任务",
|
||||||
|
"周期任务",
|
||||||
|
"Cron 任务",
|
||||||
|
"超时任务",
|
||||||
|
"取消任务",
|
||||||
|
"禁用任务",
|
||||||
|
"SQL日志"
|
||||||
|
};
|
||||||
|
/// <summary>
|
||||||
|
/// 删除任务方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ids"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpDelete]
|
||||||
|
public bool Delete([FromBody]IEnumerable<string> ids)
|
||||||
|
{
|
||||||
|
// 演示模式下禁止删除内置任务
|
||||||
|
if (DictHelper.RetrieveSystemModel() && _tasks.Any(t => ids.Any(id => id.Equals(t, StringComparison.OrdinalIgnoreCase)))) return false;
|
||||||
|
|
||||||
|
// 循环删除任务
|
||||||
|
ids.ToList().ForEach(id => TaskServicesManager.Remove(id));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,15 +16,27 @@ namespace Bootstrap.Admin.Models
|
||||||
{
|
{
|
||||||
// 此处为演示代码,具体生产环境可以从数据库配置获得
|
// 此处为演示代码,具体生产环境可以从数据库配置获得
|
||||||
// Key 为任务名称 Value 为任务执行体 FullName
|
// Key 为任务名称 Value 为任务执行体 FullName
|
||||||
Tasks = new Dictionary<string, string>
|
TaskExecutors = new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
{"测试任务", "Bootstrap.Admin.DefaultTaskExecutor"}
|
{ "测试任务", "Bootstrap.Admin.DefaultTaskExecutor" }
|
||||||
|
};
|
||||||
|
|
||||||
|
TaskTriggers = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "每 5 秒钟执行一次", Longbow.Tasks.Cron.Secondly(5) },
|
||||||
|
{ "每 1 分钟执行一次", Longbow.Tasks.Cron.Minutely(1) }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获得 系统内置的所有任务
|
/// 获得 系统内置的所有任务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IDictionary<string, string> Tasks { get; }
|
public IDictionary<string, string> TaskExecutors { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获得 系统内置触发器集合
|
||||||
|
/// </summary>
|
||||||
|
/// <value></value>
|
||||||
|
public IDictionary<string, string> TaskTriggers { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,9 @@
|
||||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
|
<div class="alert alert-danger" role="alert" asp-condition="@Model.IsDemo">
|
||||||
|
<span>演示系统禁止修改内置后台任务</span>
|
||||||
|
</div>
|
||||||
<form class="form-inline">
|
<form class="form-inline">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="form-group col-12">
|
<div class="form-group col-12">
|
||||||
|
@ -30,13 +33,19 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group col-12">
|
<div class="form-group col-12">
|
||||||
<label class="control-label" for="taskCron">Cron表达式</label>
|
<label class="control-label" for="taskCron">Cron表达式</label>
|
||||||
<input type="text" class="form-control flex-sm-fill" id="taskCron" placeholder="不可为空,2000字以内" maxlength="2000" data-valid="true" />
|
<input class="form-control" data-toggle="lgbSelect" />
|
||||||
|
<select data-toggle="lgbSelect" class="d-none" id="taskCron" data-default-val="*/5 * * * * *">
|
||||||
|
@foreach (var task in Model.TaskTriggers)
|
||||||
|
{
|
||||||
|
<option value="@task.Value">@task.Key</option>
|
||||||
|
}
|
||||||
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group col-12">
|
<div class="form-group col-12">
|
||||||
<label class="control-label" for="taskContent">内置任务</label>
|
<label class="control-label" for="taskContent">内置任务</label>
|
||||||
<input class="form-control" data-toggle="lgbSelect" data-default-val="Bootstrap.Admin.DefaultTaskExecutor" />
|
<input class="form-control" data-toggle="lgbSelect" />
|
||||||
<select data-toggle="lgbSelect" class="d-none" id="taskExecutor">
|
<select data-toggle="lgbSelect" class="d-none" id="taskExecutor" data-default-val="Bootstrap.Admin.DefaultTaskExecutor">
|
||||||
@foreach (var task in Model.Tasks)
|
@foreach (var task in Model.TaskExecutors)
|
||||||
{
|
{
|
||||||
<option value="@task.Value">@task.Key</option>
|
<option value="@task.Value">@task.Key</option>
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
smartTable: {
|
smartTable: {
|
||||||
|
idField: 'Name',
|
||||||
sidePagination: "client",
|
sidePagination: "client",
|
||||||
sortName: 'Name',
|
sortName: 'Name',
|
||||||
sortOrder: 'asc',
|
sortOrder: 'asc',
|
||||||
|
|
Loading…
Reference in New Issue