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; + }); + } +}