From 3ebfe8cd5619d2a6fe48b29dc2c52150543f63f6 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 4 Jan 2022 08:05:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=20Task=20=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Pages/Admin/Tasks.razor | 17 +++- .../Pages/Admin/Tasks.razor.cs | 86 +++++++++++++++++-- 2 files changed, 92 insertions(+), 11 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Tasks.razor b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Tasks.razor index d73159ef..20c893fe 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Tasks.razor +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Tasks.razor @@ -29,8 +29,14 @@ - - + + + + @@ -46,11 +52,14 @@ @FormatStatus(v.Value) - + + + + - +
\ No newline at end of file diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Tasks.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Tasks.razor.cs index 416b3935..ba8077c6 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Tasks.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Tasks.razor.cs @@ -1,4 +1,4 @@ -using BootstrapAdmin.Web.Components; +using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Extensions; using BootstrapAdmin.Web.Models; using Longbow.Tasks; @@ -7,18 +7,76 @@ namespace BootstrapAdmin.Web.Pages.Admin; public partial class Tasks { - [NotNull] - private AdminTable? TaskTable { get; set; } + private List SelectedRows { get; set; } = new List(); - private static Task> OnQueryAsync(QueryPageOptions options) + private static IEnumerable Jobs => new string[] { - var tasks = TaskServicesManager.ToList().ToTasksModelList(); + "单次任务", + "周期任务", + "Cron 任务", + "超时任务", + "取消任务", + "禁用任务", + "SQL日志", + "健康检查" + }; + + [Inject] + [NotNull] + private IDict? DictService { get; set; } + + private bool IsDemo { get; set; } + + protected override void OnInitialized() + { + base.OnInitialized(); + + IsDemo = DictService.IsDemo(); + } + + private Task> OnQueryAsync(QueryPageOptions options) + { + var tasks = TaskServicesManager.ToList().OrderBy(i => i.Name).ToTasksModelList(); + var model = tasks.FirstOrDefault(i => i.Name == SelectedRows.FirstOrDefault()?.Name); + SelectedRows.Clear(); + if (model != null) + { + SelectedRows.Add(model); + } return Task.FromResult(new QueryData() { Items = tasks }); } + private static Task OnSaveAsync(TasksModel model, ItemChangedType changedType) + { + var taskExecutor = new DefaultTaskExecutor(); + TaskServicesManager.Remove(model.Name); + TaskServicesManager.GetOrAdd(model.Name, token => taskExecutor.Execute(token), TriggerBuilder.Build(model.Trigger)); + return Task.FromResult(true); + } + + private Task OnDeleteAsync(IEnumerable models) + { + // 演示模式下禁止删除内置任务 + if (IsDemo) + { + var m = models.ToList(); + m.RemoveAll(m => Jobs.Any(i => i == m.Name)); + models = m; + } + + // 循环删除任务 + foreach (var model in models) + { + TaskServicesManager.Remove(model.Name); + } + return Task.FromResult(true); + } + + private bool OnShowButtonCallback(TasksModel model) => !IsDemo || !Jobs.Any(i => i == model.Name); + private static Color GetResultColor(TriggerResult result) => result switch { TriggerResult.Success => Color.Success, @@ -60,23 +118,27 @@ public partial class Tasks _ => "未知状态" }; - private static Task OnPause(TasksModel model) + private Task OnPause(TasksModel model) { var task = TaskServicesManager.ToList().FirstOrDefault(i => i.Name == model.Name); if (task != null) { task.Status = SchedulerStatus.Ready; } + SelectedRows.Clear(); + SelectedRows.Add(model); return Task.CompletedTask; } - private static Task OnRun(TasksModel model) + private Task OnRun(TasksModel model) { var task = TaskServicesManager.ToList().FirstOrDefault(i => i.Name == model.Name); if (task != null) { task.Status = SchedulerStatus.Running; } + SelectedRows.Clear(); + SelectedRows.Add(model); return Task.CompletedTask; } @@ -86,4 +148,14 @@ public partial class Tasks } private static bool OnCheckTaskStatus(TasksModel model) => model.Status != SchedulerStatus.Disabled; + + class DefaultTaskExecutor : ITask + { + /// + /// 任务执行方法 + /// + /// + /// + public Task Execute(CancellationToken cancellationToken) => Task.Delay(1000, cancellationToken); + } }