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 @@