diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs index e9b5b4c9..8ba81bed 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs @@ -1,7 +1,9 @@ using Bootstrap.DataAccess; +using Longbow; using Longbow.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System; using System.Collections.Generic; using System.Linq; @@ -44,5 +46,52 @@ namespace Bootstrap.Admin.Controllers.Api } return true; } + + /// + /// 保存任务方法 + /// + /// + [HttpPost] + public bool Post([FromBody]TaskWidget widget) + { + // 判断 Cron 表达式 + if (string.IsNullOrEmpty(widget.CronExpression)) return false; + + // 加载任务执行体 + // 此处可以扩展为任意 DLL 中的任意继承 ITask 接口的实体类 + var taskExecutor = LgbActivator.CreateInstance("Bootstrap.Admin", widget.TaskExecutorName); + 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 _tasks = new string[] { + "单次任务", + "周期任务", + "Cron 任务", + "超时任务", + "取消任务", + "禁用任务", + "SQL日志" + }; + /// + /// 删除任务方法 + /// + /// + /// + [HttpDelete] + public bool Delete([FromBody]IEnumerable 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; + } } } diff --git a/src/admin/Bootstrap.Admin/Models/TaskModel.cs b/src/admin/Bootstrap.Admin/Models/TaskModel.cs index e821bdab..ef362b62 100644 --- a/src/admin/Bootstrap.Admin/Models/TaskModel.cs +++ b/src/admin/Bootstrap.Admin/Models/TaskModel.cs @@ -4,22 +4,39 @@ using System.Collections.Generic; namespace Bootstrap.Admin.Models { /// - /// + /// 任务管理页面 Model 类 /// public class TaskModel : NavigatorBarModel { /// - /// + /// 构造函数 /// /// public TaskModel(ControllerBase controller) : base(controller) { - Tasks = new string[] { "测试任务" }; + // 此处为演示代码,具体生产环境可以从数据库配置获得 + // Key 为任务名称 Value 为任务执行体 FullName + TaskExecutors = new Dictionary + { + { "测试任务", "Bootstrap.Admin.DefaultTaskExecutor" } + }; + + TaskTriggers = new Dictionary + { + { "每 5 秒钟执行一次", Longbow.Tasks.Cron.Secondly(5) }, + { "每 1 分钟执行一次", Longbow.Tasks.Cron.Minutely(1) } + }; } /// - /// 获得 系统配置的所有任务 + /// 获得 系统内置的所有任务 /// - public IEnumerable Tasks { get; } + public IDictionary TaskExecutors { get; } + + /// + /// 获得 系统内置触发器集合 + /// + /// + public IDictionary TaskTriggers { get; } } } diff --git a/src/admin/Bootstrap.Admin/Tasks/DefaultTaskExecutor.cs b/src/admin/Bootstrap.Admin/Tasks/DefaultTaskExecutor.cs new file mode 100644 index 00000000..2846bf80 --- /dev/null +++ b/src/admin/Bootstrap.Admin/Tasks/DefaultTaskExecutor.cs @@ -0,0 +1,19 @@ +using System.Threading; +using System.Threading.Tasks; +using Longbow.Tasks; + +namespace Bootstrap.Admin +{ + /// + /// 默认任务执行体实体类 + /// + public class DefaultTaskExecutor : ITask + { + /// + /// 任务执行方法 + /// + /// + /// + public Task Execute(CancellationToken cancellationToken) => Task.Delay(1000, cancellationToken); + } +} diff --git a/src/admin/Bootstrap.Admin/Tasks/TaskWidget.cs b/src/admin/Bootstrap.Admin/Tasks/TaskWidget.cs new file mode 100644 index 00000000..c4e3a8dc --- /dev/null +++ b/src/admin/Bootstrap.Admin/Tasks/TaskWidget.cs @@ -0,0 +1,23 @@ +namespace Bootstrap.Admin +{ + /// + /// 任务描述类 + /// + public class TaskWidget + { + /// + /// 获得/设置 任务名称 + /// + public string Name { get; set; } = ""; + + /// + /// 获得/设置 任务执行体名称 + /// + public string TaskExecutorName { get; set; } = ""; + + /// + /// 获得/设置 Cron 任务表达式 + /// + public string CronExpression { get; set; } = ""; + } +} diff --git a/src/admin/Bootstrap.Admin/Views/Admin/Tasks.cshtml b/src/admin/Bootstrap.Admin/Views/Admin/Tasks.cshtml index 26e5eb8c..7dfc35c8 100644 --- a/src/admin/Bootstrap.Admin/Views/Admin/Tasks.cshtml +++ b/src/admin/Bootstrap.Admin/Views/Admin/Tasks.cshtml @@ -1,86 +1,61 @@ @model TaskModel @{ ViewBag.Title = "任务管理"; - Layout = "_Admin"; + Layout = "_Default"; } @section css { - - - - - - } @section javascript { - - - - - - - - - - - - - - - - - - - } +@section tableButtons { + + + +} @section modal { -