From fcb50ffda42e38b8f0b902fe427f0250fd7eac33 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 25 Jul 2019 10:03:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(#IZOW4):=20=E5=A2=9E=E5=8A=A0TasksExtension?= =?UTF-8?q?s=E6=89=A9=E5=B1=95=E4=BB=BB=E5=8A=A1=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Comment comment #IZOW4 增加 TasksExtensions 扩展方法,调用 services.AddHostedService() #Issue close https://gitee.com/LongbowEnterprise/dashboard/issues?id=IZOW4 --- .../Controllers/Api/TasksController.cs | 5 --- Bootstrap.Admin/Startup.cs | 2 +- Bootstrap.Admin/Tasks/TasksExtensions.cs | 45 +++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 Bootstrap.Admin/Tasks/TasksExtensions.cs diff --git a/Bootstrap.Admin/Controllers/Api/TasksController.cs b/Bootstrap.Admin/Controllers/Api/TasksController.cs index 50530465..58e0e4c2 100644 --- a/Bootstrap.Admin/Controllers/Api/TasksController.cs +++ b/Bootstrap.Admin/Controllers/Api/TasksController.cs @@ -21,11 +21,6 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public IEnumerable Get() { - 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/Startup.cs b/Bootstrap.Admin/Startup.cs index 0e04d56f..4f931167 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -63,7 +63,7 @@ namespace Bootstrap.Admin services.AddBootstrapAdminAuthentication(); services.AddSwagger(); services.AddButtonAuthorization(); - services.AddTaskServices(); + services.AddDemoTask(); services.AddMvc(options => { options.Filters.Add(); diff --git a/Bootstrap.Admin/Tasks/TasksExtensions.cs b/Bootstrap.Admin/Tasks/TasksExtensions.cs new file mode 100644 index 00000000..0b35919b --- /dev/null +++ b/Bootstrap.Admin/Tasks/TasksExtensions.cs @@ -0,0 +1,45 @@ +using Longbow.Tasks; +using Microsoft.Extensions.Hosting; +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.Extensions.DependencyInjection +{ + /// + /// 后台任务扩展方法 + /// + internal static class TasksExtensions + { + /// + /// 添加示例后台任务 + /// + /// + public static IServiceCollection AddDemoTask(this IServiceCollection services) + { + services.AddHostedService(); + return services.AddTaskServices(); + } + } + + /// + /// 后台任务服务类 + /// + internal class TaskBackgroundServices : BackgroundService + { + /// + /// 运行任务 + /// + /// + /// + protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(() => + { + 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; + }); + } +}