2025-07-04 09:50:02 +08:00
|
|
|
|
using Microsoft.Practices.Unity;
|
|
|
|
|
|
using MyCode.Project.Domain.Config;
|
|
|
|
|
|
using MyCode.Project.Domain.Model;
|
|
|
|
|
|
using MyCode.Project.Domain.Repositories;
|
|
|
|
|
|
using MyCode.Project.Infrastructure.Common;
|
|
|
|
|
|
using MyCode.Project.Infrastructure.Enumeration;
|
|
|
|
|
|
using MyCode.Project.Infrastructure.Exceptions;
|
|
|
|
|
|
using MyCode.Project.Infrastructure.Extensions;
|
|
|
|
|
|
using MyCode.Project.Infrastructure.UnityExtensions;
|
|
|
|
|
|
using MyCode.Project.Repositories.Common;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MyCode.Project.Services.Implementation
|
|
|
|
|
|
{
|
|
|
|
|
|
public class WorkProcessService : ServiceBase, IWorkProcessService
|
|
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
private ISysWorkProcessRepository _SysWorkProcessRepository;
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
private static object locker = new object(); //创建锁
|
|
|
|
|
|
public WorkProcessService(ISysWorkProcessRepository SysWorkProcessRepository)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
_SysWorkProcessRepository = SysWorkProcessRepository;
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
2025-07-07 13:54:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Add(添加调度)
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加调度
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">执行类</typeparam>
|
|
|
|
|
|
/// <param name="merchantId">商家ID</param>
|
|
|
|
|
|
/// <param name="methodName">方法名</param>
|
|
|
|
|
|
/// <param name="remark">备注</param>
|
|
|
|
|
|
/// <param name="entity">参数信息</param>
|
|
|
|
|
|
/// <param name="funcType">执行类型</param>
|
|
|
|
|
|
public void Add<T>(Guid merchantId, string methodName, string remark = "", object entity = null, int priority = 5, FuncType funcType = FuncType.Method) where T : class
|
|
|
|
|
|
{
|
|
|
|
|
|
Add(merchantId, typeof(T), methodName, remark, entity, funcType, priority);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加调度
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="merchantId">商家ID</param>
|
|
|
|
|
|
/// <param name="type">执行类</param>
|
|
|
|
|
|
/// <param name="methodName">方法名</param>
|
|
|
|
|
|
/// <param name="remark">备注</param>
|
|
|
|
|
|
/// <param name="entity">参数信息</param>
|
|
|
|
|
|
/// <param name="funcType">执行类型</param>
|
|
|
|
|
|
public void Add(Guid merchantId, Type type, string methodName, string remark = "", object entity = null, FuncType funcType = FuncType.Method, int priority = 5)
|
|
|
|
|
|
{
|
|
|
|
|
|
string typePath = string.Format("{0}, {1}", type.FullName, type.Assembly.GetName().Name);
|
|
|
|
|
|
string paramInfo = entity == null
|
|
|
|
|
|
? ""
|
|
|
|
|
|
: entity is string || entity is Guid || entity is int || entity is long || entity is decimal ||
|
|
|
|
|
|
entity is float || entity is double
|
|
|
|
|
|
? entity.SafeString()
|
|
|
|
|
|
: JsonHelper.ToJson(entity);
|
|
|
|
|
|
Add(merchantId, typePath, methodName, remark, paramInfo, funcType, priority);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加调度任务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="merchantId">商家ID</param>
|
|
|
|
|
|
/// <param name="typePath">类型路径,如:Lxm.IServices.IWorkProcessService, Lxm.Services</param>
|
|
|
|
|
|
/// <param name="methodName">方法名</param>
|
|
|
|
|
|
/// <param name="remark">备注</param>
|
|
|
|
|
|
/// <param name="paramInfo">参数信息</param>
|
|
|
|
|
|
/// <param name="funcType">执行类型</param>
|
|
|
|
|
|
public void Add(Guid merchantId, string typePath, string methodName, string remark = "", string paramInfo = "",
|
|
|
|
|
|
FuncType funcType = FuncType.Method, int priority = 5)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (this._SysWorkProcessRepository.Exists(funcType, merchantId, typePath, methodName, paramInfo) && methodName != "RunWechatVSKingDee")
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
SysWorkProcess entity = new SysWorkProcess();
|
|
|
|
|
|
entity.ID = Guid.NewGuid();
|
|
|
|
|
|
entity.MerchantID = merchantId;
|
|
|
|
|
|
entity.FuncType = funcType.Value();
|
|
|
|
|
|
entity.FuncClass = typePath;
|
|
|
|
|
|
entity.FuncMethod = methodName;
|
|
|
|
|
|
entity.ParamInfo = paramInfo;
|
|
|
|
|
|
entity.FuncStatus = FuncStatus.Waiting.Value();
|
|
|
|
|
|
entity.Remark = remark;
|
|
|
|
|
|
entity.RetryCount = 0;
|
|
|
|
|
|
entity.Creater = "系统调度";
|
|
|
|
|
|
entity.CreateTime = DateTime.Now;
|
|
|
|
|
|
entity.Editor = "系统调度";
|
|
|
|
|
|
entity.EditTime = DateTime.Now;
|
|
|
|
|
|
entity.Priority = priority;
|
|
|
|
|
|
this._SysWorkProcessRepository.Add(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-04 09:50:02 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
|
2025-07-04 09:50:02 +08:00
|
|
|
|
#region SelectInitWorkProcess(返回前几十条数据)
|
2025-07-07 13:54:21 +08:00
|
|
|
|
public List<SysWorkProcess> SelectInitWorkProcess(int top)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
|
|
|
|
|
|
return _SysWorkProcessRepository.Queryable()
|
|
|
|
|
|
.Where(p => p.FuncStatus == (int)WorkProcessStatus.Waiting && (p.Priority == 1 || p.Priority == null))
|
2025-07-04 09:50:02 +08:00
|
|
|
|
.Take(top)
|
|
|
|
|
|
.OrderBy(p => p.Priority)
|
2025-07-07 13:54:21 +08:00
|
|
|
|
.OrderBy(p => p.CreateTime).ToList();
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Add(批量添加)
|
2025-07-07 13:54:21 +08:00
|
|
|
|
public void Add(List<SysWorkProcess> workProcess)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
_SysWorkProcessRepository.Add(workProcess);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region RestratStopProcess(重新启用所有暂停了的调度,这里不需要事务,因为修改失败也不影响)
|
|
|
|
|
|
public void RestratStopProcess()
|
|
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
var list = _SysWorkProcessRepository.SelectList(p => p.FuncStatus == (int)WorkProcessStatus.Pause);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
list.ForEach(x => {
|
2025-07-07 13:54:21 +08:00
|
|
|
|
x.FuncStatus = (int)WorkProcessStatus.Running;
|
2025-07-04 09:50:02 +08:00
|
|
|
|
x.EditTime = DateTime.Now;
|
|
|
|
|
|
});
|
2025-07-07 13:54:21 +08:00
|
|
|
|
_SysWorkProcessRepository.Update(list);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region RestartStopProcess(重新启用某个暂停了的调度)
|
|
|
|
|
|
[TransactionCallHandler]
|
2025-07-07 13:54:21 +08:00
|
|
|
|
public void RestartStopProcess(Guid workprocessId)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
var workprocess = _SysWorkProcessRepository.SelectFirst(p => p.ID == workprocessId);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
if (workprocess.FuncStatus != (int)WorkProcessStatus.Pause) { throw new BaseException("当前进程状态不是停止"); }
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
workprocess.FuncStatus = (int)WorkProcessStatus.Running;
|
2025-07-04 09:50:02 +08:00
|
|
|
|
workprocess.EditTime = DateTime.Now;
|
2025-07-07 13:54:21 +08:00
|
|
|
|
_SysWorkProcessRepository.Update(workprocess);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 清空历史数据
|
|
|
|
|
|
public void ClearHistoryTask()
|
|
|
|
|
|
{
|
|
|
|
|
|
var historyDate = DateTime.Now.AddDays(-7);
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
_SysWorkProcessRepository.Delete(p => p.FuncStatus == (int)WorkProcessStatus.Complete && p.EditTime < historyDate);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region ExecuteSingle(执行单个)
|
2025-07-07 13:54:21 +08:00
|
|
|
|
public void ExecuteSingle(SysWorkProcess process)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
var type = UnityHelper.GetUnityContainer().Resolve(Type.GetType(process.FuncClass));
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
MethodInfo method = type.GetType().GetMethod(process.FuncMethod);
|
2025-07-25 22:23:48 +08:00
|
|
|
|
object result = new object();
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(process.ParamInfo))
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-25 22:23:48 +08:00
|
|
|
|
result= method.Invoke(type, new object[] { process.ParamInfo });
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-25 22:23:48 +08:00
|
|
|
|
result= method.Invoke(type, new object[] { });
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
2025-07-25 22:23:48 +08:00
|
|
|
|
if (result == null)
|
|
|
|
|
|
result = "";
|
2025-07-07 13:54:21 +08:00
|
|
|
|
process.FuncStatus = (int)WorkProcessStatus.Complete;
|
|
|
|
|
|
process.ExecuteTime = DateTime.Now;
|
|
|
|
|
|
process.ExceptionInfo = string.Empty;
|
2025-08-11 13:30:29 +08:00
|
|
|
|
process.EditTime = DateTime.Now;
|
2025-07-29 10:12:36 +08:00
|
|
|
|
process.Result=JsonHelper.ToJson(result);
|
2025-07-07 13:54:21 +08:00
|
|
|
|
_SysWorkProcessRepository.Update(process);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Execute(调度执行)
|
|
|
|
|
|
|
|
|
|
|
|
public void Execute()
|
|
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
var client = UnityHelper.GetService<MyCodeSqlSugarClient>();
|
|
|
|
|
|
|
|
|
|
|
|
var list = SelectInitWorkProcess(5);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
//先将这10个任务改成运行中
|
|
|
|
|
|
if (list != null && list.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var updateList = list.Select(p => new SysWorkProcess { ID = p.ID, FuncStatus = (int)WorkProcessStatus.Running });
|
|
|
|
|
|
|
|
|
|
|
|
_SysWorkProcessRepository.Update(updateList.ToList(), it => new { it.FuncStatus });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var process in list)
|
|
|
|
|
|
{
|
|
|
|
|
|
//这里开启事务,同时才开启
|
|
|
|
|
|
client.Ado.BeginTran();
|
|
|
|
|
|
|
|
|
|
|
|
#region 执行一个任务
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
ExecuteSingle(process);
|
|
|
|
|
|
|
|
|
|
|
|
client.Ado.CommitTran();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
client.Ado.RollbackTran();
|
|
|
|
|
|
|
|
|
|
|
|
while (ex.InnerException != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ex = ex.InnerException;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
process.FuncStatus = (int)WorkProcessStatus.ExceptionStop;
|
|
|
|
|
|
process.EditTime = DateTime.Now;
|
|
|
|
|
|
process.ExecuteTime = DateTime.Now;
|
|
|
|
|
|
if (ex is BaseException)
|
|
|
|
|
|
{
|
|
|
|
|
|
process.ExceptionInfo = ex.Message;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
process.ExceptionInfo = ex.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_SysWorkProcessRepository.Update(process);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region SelectOtherInitWorkProcess(返回优先级大于2的前几十条数据)
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取优先级3,4,5的调度
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="top"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public List<SysWorkProcess> SelectOtherInitWorkProcess(int top)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
return _SysWorkProcessRepository.Queryable()
|
|
|
|
|
|
.Where(p => p.FuncStatus == (int)WorkProcessStatus.Waiting && p.Priority > 2 && p.Priority <= 5)
|
|
|
|
|
|
.Take(top)
|
|
|
|
|
|
.OrderBy(p => p.Priority)
|
|
|
|
|
|
.OrderBy(p => p.CreateTime).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region ExecuteOther(调度执行优先级比较低的任务)
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 调度执行优先级比较低的任务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ExecuteOther()
|
|
|
|
|
|
{
|
2025-07-04 09:50:02 +08:00
|
|
|
|
var client = UnityHelper.GetService<MyCodeSqlSugarClient>();
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
var list = SelectOtherInitWorkProcess(20);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
//先将这10个任务改成运行中
|
2025-07-04 09:50:02 +08:00
|
|
|
|
if (list != null && list.Count > 0)
|
|
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
var updateList = list.Select(p => new SysWorkProcess { ID = p.ID, FuncStatus = (int)WorkProcessStatus.Running });
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
_SysWorkProcessRepository.Update(updateList.ToList(), it => new { it.FuncStatus });
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var process in list)
|
|
|
|
|
|
{
|
|
|
|
|
|
//这里开启事务,同时才开启
|
|
|
|
|
|
client.Ado.BeginTran();
|
|
|
|
|
|
|
|
|
|
|
|
#region 执行一个任务
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
ExecuteSingle(process);
|
|
|
|
|
|
|
|
|
|
|
|
client.Ado.CommitTran();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
client.Ado.RollbackTran();
|
|
|
|
|
|
|
|
|
|
|
|
while (ex.InnerException != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ex = ex.InnerException;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
process.FuncStatus = (int)WorkProcessStatus.ExceptionStop;
|
2025-07-04 09:50:02 +08:00
|
|
|
|
process.EditTime = DateTime.Now;
|
2025-07-07 13:54:21 +08:00
|
|
|
|
process.ExecuteTime = DateTime.Now;
|
2025-07-04 09:50:02 +08:00
|
|
|
|
if (ex is BaseException)
|
|
|
|
|
|
{
|
|
|
|
|
|
process.ExceptionInfo = ex.Message;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
process.ExceptionInfo = ex.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
_SysWorkProcessRepository.Update(process);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
#region SelectPriority2InitWorkProcess(返回优先级等于2的前几十条数据)
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回优先级等于2的前几十条数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="top"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public List<SysWorkProcess> SelectPriority2InitWorkProcess(int top)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
return _SysWorkProcessRepository.Queryable()
|
|
|
|
|
|
.Where(p => p.FuncStatus == (int)WorkProcessStatus.Waiting && p.Priority == 2)
|
|
|
|
|
|
.Take(top)
|
|
|
|
|
|
.OrderBy(p => p.Priority)
|
|
|
|
|
|
.OrderBy(p => p.CreateTime).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
#region ExecutePriority2Work(调度执行优先级等于2的任务)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
/// <summary>
|
2025-07-07 13:54:21 +08:00
|
|
|
|
/// 调度执行优先级等于2的任务
|
2025-07-04 09:50:02 +08:00
|
|
|
|
/// </summary>
|
2025-07-07 13:54:21 +08:00
|
|
|
|
public void ExecutePriority2Work()
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
var client = UnityHelper.GetService<MyCodeSqlSugarClient>();
|
|
|
|
|
|
|
|
|
|
|
|
var list = SelectPriority2InitWorkProcess(10);
|
|
|
|
|
|
|
|
|
|
|
|
//先将这10个任务改成运行中
|
|
|
|
|
|
if (list != null && list.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var updateList = list.Select(p => new SysWorkProcess { ID = p.ID, FuncStatus = (int)WorkProcessStatus.Running });
|
|
|
|
|
|
|
|
|
|
|
|
_SysWorkProcessRepository.Update(updateList.ToList(), it => new { it.FuncStatus });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var process in list)
|
|
|
|
|
|
{
|
|
|
|
|
|
//这里开启事务,同时才开启
|
|
|
|
|
|
client.Ado.BeginTran();
|
|
|
|
|
|
|
|
|
|
|
|
#region 执行一个任务
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
ExecuteSingle(process);
|
|
|
|
|
|
|
|
|
|
|
|
client.Ado.CommitTran();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
client.Ado.RollbackTran();
|
|
|
|
|
|
|
|
|
|
|
|
while (ex.InnerException != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ex = ex.InnerException;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
process.FuncStatus = (int)WorkProcessStatus.ExceptionStop;
|
|
|
|
|
|
process.EditTime = DateTime.Now;
|
|
|
|
|
|
process.ExecuteTime = DateTime.Now;
|
|
|
|
|
|
if (ex is BaseException)
|
|
|
|
|
|
{
|
|
|
|
|
|
process.ExceptionInfo = ex.Message;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
process.ExceptionInfo = ex.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_SysWorkProcessRepository.Update(process);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
//Thread.Sleep(300);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
#region SelectSmsInitWorkProcess(获取优先级6的短信调度)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
/// <summary>
|
2025-07-07 13:54:21 +08:00
|
|
|
|
/// 获取优先级6的短信调度
|
2025-07-04 09:50:02 +08:00
|
|
|
|
/// </summary>
|
2025-07-07 13:54:21 +08:00
|
|
|
|
/// <param name="top"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public List<SysWorkProcess> SelectPriority6WorkProcess(int top)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
DateTime days = DateTime.Now.Date;
|
|
|
|
|
|
return _SysWorkProcessRepository.Queryable()
|
|
|
|
|
|
.Where(p => p.FuncStatus == (int)WorkProcessStatus.Waiting && p.Priority == 6)
|
|
|
|
|
|
.Take(top)
|
|
|
|
|
|
.OrderBy(p => p.Priority)
|
|
|
|
|
|
.OrderBy(p => p.CreateTime).ToList();
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
#region ExecutePriority6(调度执行优先级=6的任务)
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 调度执行优先级=6的任务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ExecutePriority6()
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
var client = UnityHelper.GetService<MyCodeSqlSugarClient>();
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
var list = SelectPriority6WorkProcess(20);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
//先将这10个任务改成运行中
|
|
|
|
|
|
if (list != null && list.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var updateList = list.Select(p => new SysWorkProcess { ID = p.ID, FuncStatus = (int)WorkProcessStatus.Running });
|
2025-07-04 09:50:02 +08:00
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
_SysWorkProcessRepository.Update(updateList.ToList(), it => new { it.FuncStatus });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var process in list)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
//这里开启事务,同时才开启
|
|
|
|
|
|
client.Ado.BeginTran();
|
|
|
|
|
|
|
|
|
|
|
|
#region 执行一个任务
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
ExecuteSingle(process);
|
|
|
|
|
|
|
|
|
|
|
|
client.Ado.CommitTran();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
client.Ado.RollbackTran();
|
|
|
|
|
|
|
|
|
|
|
|
while (ex.InnerException != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ex = ex.InnerException;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
process.FuncStatus = (int)WorkProcessStatus.ExceptionStop;
|
|
|
|
|
|
process.EditTime = DateTime.Now;
|
|
|
|
|
|
process.ExecuteTime = DateTime.Now;
|
|
|
|
|
|
if (ex is BaseException)
|
|
|
|
|
|
{
|
|
|
|
|
|
process.ExceptionInfo = ex.Message;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
process.ExceptionInfo = ex.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_SysWorkProcessRepository.Update(process);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2025-07-07 13:54:21 +08:00
|
|
|
|
#region RetryTask(重试失败的任务)
|
2025-07-04 09:50:02 +08:00
|
|
|
|
/// <summary>
|
2025-07-07 13:54:21 +08:00
|
|
|
|
/// 重试失败的任务
|
2025-07-04 09:50:02 +08:00
|
|
|
|
/// </summary>
|
2025-07-07 13:54:21 +08:00
|
|
|
|
public void RetryTask()
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
DateTime today = DateTime.Now.Date.AddDays(-1);
|
|
|
|
|
|
var list = _SysWorkProcessRepository.Queryable().Where(t => t.FuncStatus == 4 && t.RetryCount <= 10
|
|
|
|
|
|
&& t.CreateTime >= today).OrderBy(t => t.EditTime).Take(20).ToList();
|
|
|
|
|
|
list.ForEach(t =>
|
2025-07-04 09:50:02 +08:00
|
|
|
|
{
|
2025-07-07 13:54:21 +08:00
|
|
|
|
t.RetryCount = t.RetryCount + 1;
|
|
|
|
|
|
t.FuncStatus = 0;
|
|
|
|
|
|
});
|
|
|
|
|
|
_SysWorkProcessRepository.Update(list);
|
2025-07-04 09:50:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|