v3.4.6
This commit is contained in:
@@ -12,12 +12,13 @@ using JNPF.DependencyInjection;
|
||||
using JNPF.DynamicApiController;
|
||||
using JNPF.FriendlyException;
|
||||
using JNPF.LinqBuilder;
|
||||
using JNPF.Schedule;
|
||||
using JNPF.Systems.Interfaces.System;
|
||||
using JNPF.TaskScheduler.Entitys;
|
||||
using JNPF.TaskScheduler.Entitys.Dto.TaskScheduler;
|
||||
using JNPF.TaskScheduler.Entitys.Entity;
|
||||
using JNPF.TaskScheduler.Entitys.Enum;
|
||||
using JNPF.TaskScheduler.Entitys.Model;
|
||||
using JNPF.TaskScheduler.Interfaces.TaskScheduler;
|
||||
using JNPF.TimeCrontab;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
@@ -38,6 +39,8 @@ public class TimeTaskService : ITimeTaskService, IDynamicApiController, ITransie
|
||||
private readonly IDataInterfaceService _dataInterfaceService;
|
||||
private readonly IUserManager _userManager;
|
||||
private readonly ICacheManager _cacheManager;
|
||||
//private readonly ISchedulerFactory _schedulerFactory;
|
||||
private readonly IDataBaseManager _dataBaseManager;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化一个<see cref="TimeTaskService"/>类型的新实例.
|
||||
@@ -46,12 +49,16 @@ public class TimeTaskService : ITimeTaskService, IDynamicApiController, ITransie
|
||||
ISqlSugarRepository<TimeTaskEntity> repository,
|
||||
IUserManager userManager,
|
||||
IDataInterfaceService dataInterfaceService,
|
||||
ICacheManager cacheManager)
|
||||
ICacheManager cacheManager,
|
||||
//ISchedulerFactory schedulerFactory,
|
||||
IDataBaseManager dataBaseManager)
|
||||
{
|
||||
_repository = repository;
|
||||
_userManager = userManager;
|
||||
_dataInterfaceService = dataInterfaceService;
|
||||
_cacheManager = cacheManager;
|
||||
_dataBaseManager = dataBaseManager;
|
||||
//_schedulerFactory = schedulerFactory;
|
||||
}
|
||||
|
||||
#region Get
|
||||
@@ -124,6 +131,7 @@ public class TimeTaskService : ITimeTaskService, IDynamicApiController, ITransie
|
||||
{
|
||||
return await GetTaskMethods();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Post
|
||||
@@ -131,7 +139,7 @@ public class TimeTaskService : ITimeTaskService, IDynamicApiController, ITransie
|
||||
/// <summary>
|
||||
/// 新建.
|
||||
/// </summary>
|
||||
/// <param name="input">实体对象</param>
|
||||
/// <param name="input">实体对象.</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("")]
|
||||
public async Task Create([FromBody] TimeTaskCrInput input)
|
||||
@@ -147,10 +155,14 @@ public class TimeTaskService : ITimeTaskService, IDynamicApiController, ITransie
|
||||
entity.ExecuteContent = comtentModel.ToJsonString();
|
||||
entity.ExecuteCycleJson = comtentModel.cron;
|
||||
var result = await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
|
||||
// var job = _repository.AsTenant().GetConnection("JNPF-Job");
|
||||
// var jobDetail = new JobDetail() { };
|
||||
// job.Insertable()
|
||||
_ = result ?? throw Oops.Oh(ErrorCode.COM1000);
|
||||
|
||||
// 添加到任务调度里
|
||||
AddTimerJob(result);
|
||||
//await AddJob(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -271,7 +283,7 @@ public class TimeTaskService : ITimeTaskService, IDynamicApiController, ITransie
|
||||
// 非多租户模式启动自启任务
|
||||
if (!KeyVariable.MultiTenancy)
|
||||
{
|
||||
_repository.AsQueryable().Where(x => x.DeleteMark == null && x.EnabledMark == 1).ToList().ForEach(AddTimerJob);
|
||||
//_repository.AsQueryable().Where(x => x.DeleteMark == null && x.EnabledMark == 1).ToList().ForEach(AddTimerJob);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,12 +336,11 @@ public class TimeTaskService : ITimeTaskService, IDynamicApiController, ITransie
|
||||
Action<SpareTimer, long>? action = null;
|
||||
ContentModel? comtentModel = input.ExecuteContent.ToObject<ContentModel>();
|
||||
input.ExecuteCycleJson = comtentModel.cron;
|
||||
TaskMethodInfo? taskMethod = null;
|
||||
switch (input.ExecuteType)
|
||||
{
|
||||
case "3":
|
||||
// 查询符合条件的任务方法
|
||||
taskMethod = GetTaskMethods()?.Result.FirstOrDefault(m => m.id == comtentModel.localHostTaskId);
|
||||
TaskMethodInfo? taskMethod = GetTaskMethods()?.Result.FirstOrDefault(m => m.id == comtentModel.localHostTaskId);
|
||||
if (taskMethod == null) break;
|
||||
|
||||
// 创建任务对象
|
||||
@@ -347,7 +358,8 @@ public class TimeTaskService : ITimeTaskService, IDynamicApiController, ITransie
|
||||
}
|
||||
|
||||
if (action == null) return;
|
||||
//SpareTime.Do(comtentModel.cron, action, input.Id, comtentModel.ConnectionConfig.ToJsonString(), true, executeType: SpareTimeExecuteTypes.Parallel);
|
||||
|
||||
// SpareTime.Do(comtentModel.cron, action, input.Id, comtentModel.ConnectionConfig.ToJsonString(), true, executeType: SpareTimeExecuteTypes.Parallel);
|
||||
var starTime = comtentModel.startTime?.TimeStampToDateTime();
|
||||
var endTime = comtentModel.endTime?.TimeStampToDateTime();
|
||||
var interval = 1;
|
||||
@@ -355,65 +367,86 @@ public class TimeTaskService : ITimeTaskService, IDynamicApiController, ITransie
|
||||
{
|
||||
interval = (starTime.ParseToDateTime() - DateTime.Now).TotalMilliseconds.ParseToInt();
|
||||
}
|
||||
|
||||
if (taskMethod.StartNow) //modifyby zhoukeda 20230516
|
||||
{
|
||||
SpareTime.DoOnce(interval, action, "Once_" + input.Id);
|
||||
}
|
||||
|
||||
Func<DateTimeOffset?> nextHandle = null;
|
||||
var isRun = comtentModel.endTime.IsNullOrEmpty() ? DateTime.Now >= starTime : DateTime.Now >= starTime && DateTime.Now < endTime;
|
||||
if (isRun)
|
||||
{
|
||||
nextHandle = ()=>SpareTime.GetCronNextOccurrence(comtentModel.cron);
|
||||
}
|
||||
SpareTime.Do(nextHandle
|
||||
,
|
||||
SpareTime.DoOnce(interval, action, "Once_" + input.Id);
|
||||
SpareTime.Do(
|
||||
() =>
|
||||
{
|
||||
var isRun = comtentModel.endTime.IsNullOrEmpty() ? DateTime.Now >= starTime : DateTime.Now >= starTime && DateTime.Now < endTime;
|
||||
if (isRun)
|
||||
{
|
||||
return SpareTime.GetCronNextOccurrence(comtentModel.cron);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
},
|
||||
action, input.Id, comtentModel.ConnectionConfig.ToJsonString(), true, executeType: SpareTimeExecuteTypes.Parallel, cancelInNoneNextTime: false);
|
||||
}
|
||||
|
||||
private async Task AddJob(TimeTaskEntity input)
|
||||
{
|
||||
//ContentModel? comtentModel = input.ExecuteContent.ToObject<ContentModel>();
|
||||
//var starTime = comtentModel.startTime?.TimeStampToDateTime();
|
||||
//var endTime = comtentModel.endTime?.TimeStampToDateTime();
|
||||
//var jobBuilder = JobBuilder.Create(async (context, stoppingToken) =>
|
||||
//{
|
||||
// Console.WriteLine(string.Format("{0}在执行任务,执行时间{1},触发时间:{1}", context.JobId, context.ExecutingTime, context.OccurrenceTime));
|
||||
// if (!input.ExecuteType.Equals("3"))
|
||||
// {
|
||||
// var msg = await PerformJob(input);
|
||||
// }
|
||||
//});
|
||||
//jobBuilder.SetJobId(input.Id);
|
||||
//jobBuilder.SetGroupName(input.FullName);
|
||||
//var triggerBuilder = TriggerBuilder.Create<CronTrigger>(input.ExecuteCycleJson, CronStringFormat.WithSeconds);
|
||||
//triggerBuilder.SetStartTime(starTime);
|
||||
//triggerBuilder.SetEndTime(endTime);
|
||||
////triggerBuilder.SetRunOnStart(true);
|
||||
//_schedulerFactory.AddJob(jobBuilder, triggerBuilder);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有本地任务.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task<List<TaskMethodInfo>> GetTaskMethods()
|
||||
{
|
||||
// var taskMethods = await _cacheManager.GetAsync<List<TaskMethodInfo>>(CommonConst.CACHEKEYTIMERJOB);
|
||||
// if (taskMethods != null) return taskMethods;
|
||||
//var taskMethods = await _cacheManager.GetAsync<List<TaskMethodInfo>>(CommonConst.CACHEKEYTIMERJOB);
|
||||
//if (taskMethods != null) return taskMethods;
|
||||
|
||||
List<TaskMethodInfo> taskMethods = null;
|
||||
|
||||
// 获取所有本地任务方法,必须有spareTimeAttribute特性
|
||||
taskMethods = App.EffectiveTypes
|
||||
.Where(u => u.IsClass && !u.IsInterface && !u.IsAbstract && typeof(ISpareTimeWorker).IsAssignableFrom(u))
|
||||
.SelectMany(u => u.GetMethods(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(m => m.GetCustomAttributes(typeof(SpareTimeAttribute), false).ToString().Contains("SpareTime") &&
|
||||
m.GetParameters().Length == 2 &&
|
||||
m.GetParameters()[0].ParameterType == typeof(SpareTimer) &&
|
||||
m.GetParameters()[1].ParameterType == typeof(long) && m.ReturnType == typeof(void))
|
||||
.Select(m =>
|
||||
{
|
||||
// 默认获取第一条任务特性
|
||||
var spareTimeAttribute = m.GetCustomAttribute<SpareTimeAttribute>();
|
||||
return new TaskMethodInfo
|
||||
{
|
||||
id = $"{m.DeclaringType.Name}/{m.Name}",
|
||||
fullName = spareTimeAttribute.WorkerName,
|
||||
RequestUrl = $"{m.DeclaringType.Name}/{m.Name}",
|
||||
cron = spareTimeAttribute.CronExpression,
|
||||
DoOnce = spareTimeAttribute.DoOnce,
|
||||
ExecuteType = spareTimeAttribute.ExecuteType,
|
||||
Interval = (int)spareTimeAttribute.Interval / 1000,
|
||||
StartNow = spareTimeAttribute.StartNow,
|
||||
RequestType = RequestTypeEnum.Run,
|
||||
Remark = spareTimeAttribute.Description,
|
||||
TimerType = string.IsNullOrEmpty(spareTimeAttribute.CronExpression) ? SpareTimeTypes.Interval : SpareTimeTypes.Cron,
|
||||
MethodName = m.Name,
|
||||
DeclaringType = m.DeclaringType
|
||||
};
|
||||
})).ToList();
|
||||
await _cacheManager.SetAsync(CommonConst.CACHEKEYTIMERJOB, taskMethods);
|
||||
return taskMethods;
|
||||
//// 获取所有本地任务方法,必须有spareTimeAttribute特性
|
||||
//taskMethods = App.EffectiveTypes
|
||||
// .Where(u => u.IsClass && !u.IsInterface && !u.IsAbstract && typeof(ISpareTimeWorker).IsAssignableFrom(u))
|
||||
// .SelectMany(u => u.GetMethods(BindingFlags.Public | BindingFlags.Instance)
|
||||
// .Where(m => m.IsDefined(typeof(SpareTimeAttribute), false) &&
|
||||
// m.GetParameters().Length == 2 &&
|
||||
// m.GetParameters()[0].ParameterType == typeof(SpareTimer) &&
|
||||
// m.GetParameters()[1].ParameterType == typeof(long) && m.ReturnType == typeof(void))
|
||||
// .Select(m =>
|
||||
// {
|
||||
// // 默认获取第一条任务特性
|
||||
// var spareTimeAttribute = m.GetCustomAttribute<SpareTimeAttribute>();
|
||||
// return new TaskMethodInfo
|
||||
// {
|
||||
// id = $"{m.DeclaringType.Name}/{m.Name}",
|
||||
// fullName = spareTimeAttribute.WorkerName,
|
||||
// RequestUrl = $"{m.DeclaringType.Name}/{m.Name}",
|
||||
// cron = spareTimeAttribute.CronExpression,
|
||||
// DoOnce = spareTimeAttribute.DoOnce,
|
||||
// ExecuteType = spareTimeAttribute.ExecuteType,
|
||||
// Interval = (int)spareTimeAttribute.Interval / 1000,
|
||||
// StartNow = spareTimeAttribute.StartNow,
|
||||
// RequestType = RequestTypeEnum.BuiltIn,
|
||||
// Remark = spareTimeAttribute.Description,
|
||||
// TimerType = string.IsNullOrEmpty(spareTimeAttribute.CronExpression) ? SpareTimeTypes.Interval : SpareTimeTypes.Cron,
|
||||
// MethodName = m.Name,
|
||||
// DeclaringType = m.DeclaringType
|
||||
// };
|
||||
// })).ToList();
|
||||
//await _cacheManager.SetAsync(CommonConst.CACHEKEYTIMERJOB, taskMethods);
|
||||
//return taskMethods;
|
||||
return new List<TaskMethodInfo>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user