自动任务

master
rszn 6 months ago
parent 51aabd63a2
commit 11a08e4349

@ -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();
}

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

@ -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<string, ITask> TaskMap = new();
public static void Init()
{
var list = new List<Type>();
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();
}
}
}

@ -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
{
/// <summary>
/// 获取任务列表
/// </summary>
/// <returns></returns>
[HttpGet("/GetAll")]
public Result<List<TaskInfo>> GetAll()
{
List<TaskInfo> result = new();
foreach (var (key, value) in TaskInit.TaskMap)
{
result.Add(new TaskInfo()
{
Name = key,
Interval = value.Interval(),
Stoped = value.Stoped()
});
}
return Result<List<TaskInfo>>.Success(result);
}
/// <summary>
/// 停止任务
/// </summary>
/// <param name="taskName"></param>
/// <returns></returns>
[HttpGet("/stop/{taskName}")]
public Result<bool> Stop(string taskName)
{
if (TaskInit.TaskMap.TryGetValue(taskName, out var task))
{
task.Stop();
return Result<bool>.Success(task.Stoped());
}
return Result<bool>.Fail("任务不存在");
}
/// <summary>
/// 启动任务
/// </summary>
/// <param name="taskName"></param>
/// <returns></returns>
[HttpGet("/start/{taskName}")]
public Result<bool> Start(string taskName)
{
if (TaskInit.TaskMap.TryGetValue(taskName, out var task))
{
task.Start();
return Result<bool>.Success(task.Stoped());
}
return Result<bool>.Fail("任务不存在");
}
}

@ -1,6 +1,7 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.AutoTask;
using HybirdFrameworkCore.Configuration; using HybirdFrameworkCore.Configuration;
using HybirdFrameworkCore.Entity; using HybirdFrameworkCore.Entity;
using HybirdFrameworkCore.Redis; using HybirdFrameworkCore.Redis;
@ -98,4 +99,7 @@ AppInfo.Container = app.Services.GetAutofacRoot();
var listen = builder.Configuration.GetSection("Listen"); var listen = builder.Configuration.GetSection("Listen");
CarServerMgr.InitServer(int.Parse(listen.GetSection("Port").Value ?? "5588")); CarServerMgr.InitServer(int.Parse(listen.GetSection("Port").Value ?? "5588"));
TaskInit.Init();
app.Run(); app.Run();
Loading…
Cancel
Save