From 7388a54f20bf511e409177cad4f8f69e7514d371 Mon Sep 17 00:00:00 2001 From: Argo Date: Mon, 15 Jul 2019 15:53:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=A0=B9=E6=8D=AE=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E7=9A=84Tasks=E7=BB=84=E4=BB=B6=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Bootstrap.Admin.csproj | 2 +- .../Controllers/Api/TasksController.cs | 9 ++- .../Controllers/Api/TasksLogController.cs | 8 +-- Bootstrap.Admin/Views/Admin/Tasks.cshtml | 6 +- Bootstrap.Admin/wwwroot/js/tasks.js | 61 +++++++++++++------ .../Bootstrap.DataAccess.MongoDB.csproj | 2 +- .../Bootstrap.DataAccess.csproj | 2 +- UnitTest/Bootstrap.Admin/Api/TasksTest.cs | 2 +- 8 files changed, 63 insertions(+), 29 deletions(-) diff --git a/Bootstrap.Admin/Bootstrap.Admin.csproj b/Bootstrap.Admin/Bootstrap.Admin.csproj index eee53a49..2b84cace 100644 --- a/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -14,7 +14,7 @@ - + diff --git a/Bootstrap.Admin/Controllers/Api/TasksController.cs b/Bootstrap.Admin/Controllers/Api/TasksController.cs index 3ad24a8f..50530465 100644 --- a/Bootstrap.Admin/Controllers/Api/TasksController.cs +++ b/Bootstrap.Admin/Controllers/Api/TasksController.cs @@ -1,5 +1,6 @@ using Longbow.Tasks; using Microsoft.AspNetCore.Mvc; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -20,8 +21,12 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public IEnumerable Get() { - TaskServicesManager.GetOrAdd("测试任务", token => Task.Delay(1000), TriggerBuilder.WithCronExpression("*/5 * * * * *")); - return TaskServicesManager.ToList().Select(s => new { s.Name, s.Enabled, s.Status, s.LastRuntime, s.CreatedTime, s.NextRuntime, Triggers = s.Triggers.Count, s.LastRunResult, TriggerExpression = s.Triggers.FirstOrDefault().ToString() }); + TaskServicesManager.GetOrAdd("单次任务", token => Task.Delay(1000)); + TaskServicesManager.GetOrAdd("周期任务", token => Task.Delay(1000), TriggerBuilder.Build(Cron.Secondly(5))); + TaskServicesManager.GetOrAdd("超时任务", token => Task.Delay(2000), TriggerBuilder.Default.WithTimeout(1000).WithInterval(1000).WithRepeatCount(2).Build()); + TaskServicesManager.GetOrAdd("故障任务", token => throw new Exception("故障任务")); + TaskServicesManager.GetOrAdd("取消任务", token => Task.Delay(1000)).Triggers.First().Enabled = false; + return TaskServicesManager.ToList().Select(s => new { s.Name, Status = s.Status.ToString(), s.LastRuntime, s.CreatedTime, s.NextRuntime, LastRunResult = s.Triggers.First().LastResult.ToString(), TriggerExpression = s.Triggers.FirstOrDefault().ToString() }).OrderBy(s => s.Name); } } } diff --git a/Bootstrap.Admin/Controllers/Api/TasksLogController.cs b/Bootstrap.Admin/Controllers/Api/TasksLogController.cs index 5c504dda..400724c3 100644 --- a/Bootstrap.Admin/Controllers/Api/TasksLogController.cs +++ b/Bootstrap.Admin/Controllers/Api/TasksLogController.cs @@ -2,6 +2,7 @@ using Longbow.Web.SignalR; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; +using System.Linq; namespace Bootstrap.Admin.Controllers.Api { @@ -22,12 +23,11 @@ namespace Bootstrap.Admin.Controllers.Api public bool Get([FromQuery]string name, [FromServices]IHubContext hub) { var sche = TaskServicesManager.GetOrAdd(name); - sche.Triggers[0].RegisterPulseCallback(async t => + sche.Triggers.First().PulseCallback = async t => { - var success = t.Cancelled ? "Cancelled" : "Success"; - var result = $"{t.Scheduler.LastRuntime.Value.DateTime}: Trigger({t.GetType().Name}) Run({success}) NextRuntime: {t.NextRuntime.Value.DateTime} Elapsed: {t.LastRunElapsedTime.Seconds}s"; + var result = $"{{\"name\": \"{name}\", \"msg\": \"{sche.LastRuntime}: Trigger({t.GetType().Name}) Run({t.LastResult}) NextRuntime: {sche.NextRuntime} Elapsed: {t.LastRunElapsedTime.Seconds}s\"}}"; await SignalRManager.SendTaskLog(hub.Clients.All, result); - }); + }; return true; } } diff --git a/Bootstrap.Admin/Views/Admin/Tasks.cshtml b/Bootstrap.Admin/Views/Admin/Tasks.cshtml index d2efcd1c..fb143b80 100644 --- a/Bootstrap.Admin/Views/Admin/Tasks.cshtml +++ b/Bootstrap.Admin/Views/Admin/Tasks.cshtml @@ -33,14 +33,14 @@

var trigger = TriggerBuilder.Default.WithInterval(TimeSpan.FromSeconds(5)).WithRepeatCount(2).WithStartTime(DateTimeOffset.Now.AddMinutes(1)).Build(); -
+
TaskServicesManager.GetOrAdd("测试任务", token => Task.Delay(1000), trigger);

3. Cron表达式任务 (间隔 5 秒循环执行任务)

- TaskServicesManager.GetOrAdd("Cron表达式任务", token => Task.Delay(1000), TriggerBuilder.WithCronExpression("*/5 * * * * *")); + TaskServicesManager.GetOrAdd("Cron表达式任务", token => Task.Delay(1000), TriggerBuilder.Build("*/5 * * * * *"));

@@ -86,9 +86,11 @@