任务框架

zw
rszn 6 months ago
parent 6728923020
commit ce34b813c1

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

@ -1,42 +1,66 @@
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.AutoTask;
using log4net;
using Repository.Station;
using Service.BusinessTask;
namespace Service.Cloud.Client.MyTask;
[Scope("SingleInstance")]
public class ChargeOrderUploadTask : AbstractTaskHandler
[Scope]
public class ChargeOrderUploadTask : ITask
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ChargeOrderUploadTask));
private volatile bool _stop;
public ChargeOrderRepository _chargeOrderRepository { get; set; }
protected override int Interval()
public int Interval()
{
return 1000 * 10;
}
protected override void Handle()
public void Handle()
{
List<ChargeOrder>? chargeOrders = _chargeOrderRepository.QueryListByClause(it => it.CloudReportStatus ==0 && it.CanUpload == 1);
Log.Info($"there are {chargeOrders?.Count ?? 0} to upload");
if (chargeOrders is { Count: > 0 })
try
{
List<ChargeOrder>? chargeOrders =
_chargeOrderRepository.QueryListByClause(it => it.CloudReportStatus == 0 && it.CanUpload == 1);
var group = chargeOrders.GroupBy(it => it.CloudChargeOrder);
foreach (IGrouping<string,ChargeOrder> grouping in group)
Log.Info($"there are {chargeOrders?.Count ?? 0} to upload");
if (chargeOrders is { Count: > 0 })
{
List<ChargeOrder> orders = grouping.ToList();
CloudClientMgr.CloudClient?.PublishChargeOrder(orders, 1);
var group = chargeOrders.GroupBy(it => it.CloudChargeOrder);
foreach (IGrouping<string, ChargeOrder> grouping in group)
{
List<ChargeOrder> orders = grouping.ToList();
CloudClientMgr.CloudClient?.PublishChargeOrder(orders, 1);
}
}
}
catch (Exception e)
{
Log.Error(e);
}
}
protected override string Name()
public string Name()
{
return "ChargeOrderUploadTask";
}
public bool Stoped()
{
return _stop;
}
public void Stop()
{
_stop = true;
}
public void ResetStop()
{
_stop = false;
}
}

@ -1,8 +1,5 @@
using Autofac;
using HybirdFrameworkCore.Autofac;
using Service.BusinessTask;
using Service.BusinessTask;
using Service.BusinessTask.MyTask;
using Service.Cloud.Client.MyTask;
namespace Service.Execute
{
@ -16,7 +13,6 @@ namespace Service.Execute
private static readonly AbstractTaskHandler SwapOrderReportCloudTask = new SwapOrderReportCloudTask();
private static readonly AbstractTaskHandler BatteryMoveTask = new BatteryMoveTask();
private static readonly AbstractTaskHandler ChargeOrderUploadTask = AppInfo.Container.Resolve<ChargeOrderUploadTask>();
#region Task
@ -28,7 +24,6 @@ namespace Service.Execute
{
SwapOrderReportCloudTask.Start();
BatteryMoveTask.Start();
ChargeOrderUploadTask.Start();
}
/// <summary>

@ -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("任务不存在");
}
}

@ -3,6 +3,7 @@ using Autofac;
using Autofac.Extensions.DependencyInjection;
using Entity.Dto.Resp;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.AutoTask;
using HybirdFrameworkCore.Configuration;
using HybirdFrameworkCore.Entity;
using HybirdFrameworkCore.Redis;
@ -168,5 +169,7 @@ if (AppSettingsHelper.GetBool("task", "enable"))
StationSoftMgr.StartTasks();
}
TaskInit.Init();
app.Run();
Loading…
Cancel
Save