feat: 增加任务编辑功能

This commit is contained in:
Argo Zhang 2020-02-19 15:24:26 +08:00
parent 34d1f87a51
commit 1ba16e0e4b
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
4 changed files with 56 additions and 9 deletions

View File

@ -3,6 +3,7 @@ using Longbow;
using Longbow.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
@ -55,8 +56,6 @@ namespace Bootstrap.Admin.Controllers.Api
{
// 判断 Cron 表达式
if (string.IsNullOrEmpty(widget.CronExpression)) return false;
// 判断 任务是否已经存在
if (TaskServicesManager.Get(widget.Name) != null) return false;
// 加载任务执行体
// 此处可以扩展为任意 DLL 中的任意继承 ITask 接口的实体类
@ -64,9 +63,35 @@ namespace Bootstrap.Admin.Controllers.Api
if (taskExecutor == null) return false;
// 此处未存储到数据库中,直接送入任务中心
TaskServicesManager.Remove(widget.Name);
var expression = Longbow.Tasks.Cron.ParseCronExpression(widget.CronExpression);
TaskServicesManager.GetOrAdd(widget.Name, token => taskExecutor.Execute(token), TriggerBuilder.Build(widget.CronExpression));
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;
}
}
}

View File

@ -16,15 +16,27 @@ namespace Bootstrap.Admin.Models
{
// 此处为演示代码,具体生产环境可以从数据库配置获得
// 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>
public IDictionary<string, string> Tasks { get; }
public IDictionary<string, string> TaskExecutors { get; }
/// <summary>
/// 获得 系统内置触发器集合
/// </summary>
/// <value></value>
public IDictionary<string, string> TaskTriggers { get; }
}
}

View File

@ -22,6 +22,9 @@
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
</div>
<div class="modal-body">
<div class="alert alert-danger" role="alert" asp-condition="@Model.IsDemo">
<span>演示系统禁止修改内置后台任务</span>
</div>
<form class="form-inline">
<div class="row">
<div class="form-group col-12">
@ -30,13 +33,19 @@
</div>
<div class="form-group col-12">
<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 class="form-group col-12">
<label class="control-label" for="taskContent">内置任务</label>
<input class="form-control" data-toggle="lgbSelect" data-default-val="Bootstrap.Admin.DefaultTaskExecutor" />
<select data-toggle="lgbSelect" class="d-none" id="taskExecutor">
@foreach (var task in Model.Tasks)
<input class="form-control" data-toggle="lgbSelect" />
<select data-toggle="lgbSelect" class="d-none" id="taskExecutor" data-default-val="Bootstrap.Admin.DefaultTaskExecutor">
@foreach (var task in Model.TaskExecutors)
{
<option value="@task.Value">@task.Key</option>
}

View File

@ -43,6 +43,7 @@
}
},
smartTable: {
idField: 'Name',
sidePagination: "client",
sortName: 'Name',
sortOrder: 'asc',