From 11a08e43494b6a3ed7daa4eba309ff0b6433ba1e Mon Sep 17 00:00:00 2001 From: rszn <645583145@qq.com> Date: Sat, 1 Jun 2024 23:17:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HybirdFrameworkCore/AutoTask/ITask.cs | 36 +++++++++++++ HybirdFrameworkCore/AutoTask/TaskInfo.cs | 8 +++ HybirdFrameworkCore/AutoTask/TaskInit.cs | 38 ++++++++++++++ WebStarter/Controllers/TaskController.cs | 67 ++++++++++++++++++++++++ WebStarter/Program.cs | 12 +++-- 5 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 HybirdFrameworkCore/AutoTask/ITask.cs create mode 100644 HybirdFrameworkCore/AutoTask/TaskInfo.cs create mode 100644 HybirdFrameworkCore/AutoTask/TaskInit.cs create mode 100644 WebStarter/Controllers/TaskController.cs diff --git a/HybirdFrameworkCore/AutoTask/ITask.cs b/HybirdFrameworkCore/AutoTask/ITask.cs new file mode 100644 index 0000000..74b6fc3 --- /dev/null +++ b/HybirdFrameworkCore/AutoTask/ITask.cs @@ -0,0 +1,36 @@ +namespace HybirdFrameworkCore.AutoTask; + +public interface ITask +{ + public string Name(); + public int Interval(); + public void Handle(); + + public void Start() + { + Stop(); + Thread.Sleep(Interval()); + ResetStop(); + Thread thread = new Thread(Process) + { + Name = Name(), + IsBackground = true + }; + + thread.Start(); + } + + void Process() + { + while (!Stoped()) + { + Thread.Sleep(Interval()); + Handle(); + } + } + + public bool Stoped(); + + public void Stop(); + public void ResetStop(); +} diff --git a/HybirdFrameworkCore/AutoTask/TaskInfo.cs b/HybirdFrameworkCore/AutoTask/TaskInfo.cs new file mode 100644 index 0000000..859e81f --- /dev/null +++ b/HybirdFrameworkCore/AutoTask/TaskInfo.cs @@ -0,0 +1,8 @@ +namespace HybirdFrameworkCore.AutoTask; + +public class TaskInfo +{ + public string Name { get; set; } + public int Interval { get; set; } + public bool Stoped { get; set; } +} diff --git a/HybirdFrameworkCore/AutoTask/TaskInit.cs b/HybirdFrameworkCore/AutoTask/TaskInit.cs new file mode 100644 index 0000000..d896567 --- /dev/null +++ b/HybirdFrameworkCore/AutoTask/TaskInit.cs @@ -0,0 +1,38 @@ +using System.Collections.Concurrent; +using Autofac; +using Autofac.Core; +using HybirdFrameworkCore.Autofac; + +namespace HybirdFrameworkCore.AutoTask; + +public class TaskInit +{ + public static readonly ConcurrentDictionary TaskMap = new(); + + public static void Init() + { + var list = new List(); + foreach (var reg in AppInfo.Container.ComponentRegistry.Registrations) + foreach (var service in reg.Services) + if (service is TypedService ts) + { + if (ts.ServiceType.GetInterface(nameof(ITask)) != null) + { + list.Add(ts.ServiceType); + } + } + + foreach (Type type in list) + { + var resolve = AppInfo.Container.Resolve(type); + ITask task = (ITask)resolve; + if (TaskMap.TryGetValue(task.Name(), out var exist)) + { + throw new ArgumentException($"same task name already added {exist.GetType()}"); + } + + TaskMap.TryAdd(task.Name(), task); + task.Start(); + } + } +} diff --git a/WebStarter/Controllers/TaskController.cs b/WebStarter/Controllers/TaskController.cs new file mode 100644 index 0000000..fd7b5d6 --- /dev/null +++ b/WebStarter/Controllers/TaskController.cs @@ -0,0 +1,67 @@ +using HybirdFrameworkCore.Entity; +using HybirdFrameworkCore.AutoTask; +using Microsoft.AspNetCore.Mvc; + +namespace WebStarter.Controllers.System; + +[Produces("application/json")] +[ApiController] +[Route("api/[controller]")] +public class TaskController +{ + /// + /// 获取任务列表 + /// + /// + [HttpGet("/GetAll")] + public Result> GetAll() + { + List result = new(); + foreach (var (key, value) in TaskInit.TaskMap) + { + result.Add(new TaskInfo() + { + Name = key, + Interval = value.Interval(), + Stoped = value.Stoped() + }); + } + + return Result>.Success(result); + } + + /// + /// 停止任务 + /// + /// + /// + [HttpGet("/stop/{taskName}")] + public Result Stop(string taskName) + { + if (TaskInit.TaskMap.TryGetValue(taskName, out var task)) + { + task.Stop(); + return Result.Success(task.Stoped()); + } + + return Result.Fail("任务不存在"); + } + + + /// + /// 启动任务 + /// + /// + /// + [HttpGet("/start/{taskName}")] + public Result Start(string taskName) + { + if (TaskInit.TaskMap.TryGetValue(taskName, out var task)) + { + task.Start(); + return Result.Success(task.Stoped()); + } + + return Result.Fail("任务不存在"); + } +} diff --git a/WebStarter/Program.cs b/WebStarter/Program.cs index 8b30de2..3fb6595 100644 --- a/WebStarter/Program.cs +++ b/WebStarter/Program.cs @@ -1,6 +1,7 @@ using Autofac; using Autofac.Extensions.DependencyInjection; using HybirdFrameworkCore.Autofac; +using HybirdFrameworkCore.AutoTask; using HybirdFrameworkCore.Configuration; using HybirdFrameworkCore.Entity; using HybirdFrameworkCore.Redis; @@ -53,8 +54,8 @@ builder.Services.AddSwaggerGen(c => c.IncludeXmlComments(Path.Combine(basePath, "WebStarter.xml"), true); c.IncludeXmlComments(Path.Combine(basePath, "Entity.xml"), true); c.IncludeXmlComments(Path.Combine(basePath, "HybirdFrameworkCore.xml"), true); - - + + }); builder.Services.AddControllers().AddJsonOptions(configure => @@ -63,7 +64,7 @@ builder.Services.AddControllers().AddJsonOptions(configure => }); //redis var redisConnectionString = AppSettingsHelper.GetContent("Redis", "Connection"); -var instanceName = AppSettingsHelper.GetContent("Redis", "InstanceName");//默认数据库 +var instanceName = AppSettingsHelper.GetContent("Redis", "InstanceName");//默认数据库 var defaultDb = int.Parse(AppSettingsHelper.GetContent("Redis", "DefaultDB") ?? "0"); if (redisConnectionString != null && instanceName != null) builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, defaultDb)); @@ -98,4 +99,7 @@ AppInfo.Container = app.Services.GetAutofacRoot(); var listen = builder.Configuration.GetSection("Listen"); CarServerMgr.InitServer(int.Parse(listen.GetSection("Port").Value ?? "5588")); -app.Run(); \ No newline at end of file + +TaskInit.Init(); + +app.Run();