From 1ba16e0e4baaa8217a84af03ca8e9fdfb5ac6327 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Wed, 19 Feb 2020 15:24:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/TasksController.cs | 29 +++++++++++++++++-- src/admin/Bootstrap.Admin/Models/TaskModel.cs | 18 ++++++++++-- .../Bootstrap.Admin/Views/Admin/Tasks.cshtml | 17 ++++++++--- src/admin/Bootstrap.Admin/wwwroot/js/tasks.js | 1 + 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs index 608c2e46..8ba81bed 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/TasksController.cs @@ -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 _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 148417a1..ef362b62 100644 --- a/src/admin/Bootstrap.Admin/Models/TaskModel.cs +++ b/src/admin/Bootstrap.Admin/Models/TaskModel.cs @@ -16,15 +16,27 @@ namespace Bootstrap.Admin.Models { // 此处为演示代码,具体生产环境可以从数据库配置获得 // Key 为任务名称 Value 为任务执行体 FullName - Tasks = new Dictionary + TaskExecutors = new Dictionary { - {"测试任务", "Bootstrap.Admin.DefaultTaskExecutor"} + { "测试任务", "Bootstrap.Admin.DefaultTaskExecutor" } + }; + + TaskTriggers = new Dictionary + { + { "每 5 秒钟执行一次", Longbow.Tasks.Cron.Secondly(5) }, + { "每 1 分钟执行一次", Longbow.Tasks.Cron.Minutely(1) } }; } /// /// 获得 系统内置的所有任务 /// - public IDictionary Tasks { get; } + public IDictionary TaskExecutors { get; } + + /// + /// 获得 系统内置触发器集合 + /// + /// + public IDictionary TaskTriggers { get; } } } diff --git a/src/admin/Bootstrap.Admin/Views/Admin/Tasks.cshtml b/src/admin/Bootstrap.Admin/Views/Admin/Tasks.cshtml index f63d2a67..7dfc35c8 100644 --- a/src/admin/Bootstrap.Admin/Views/Admin/Tasks.cshtml +++ b/src/admin/Bootstrap.Admin/Views/Admin/Tasks.cshtml @@ -22,6 +22,9 @@