158 lines
5.5 KiB
C#
158 lines
5.5 KiB
C#
using Microsoft.Practices.Unity;
|
||
using MyCode.Project.Domain.Message.Response.Queue;
|
||
using MyCode.Project.Infrastructure.Cache;
|
||
using MyCode.Project.Infrastructure.Common;
|
||
using MyCode.Project.Infrastructure.Constant;
|
||
using MyCode.Project.Infrastructure.Extensions;
|
||
using MyCode.Project.Infrastructure.UnityExtensions;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace MyCode.Project.Services.Implementation
|
||
{
|
||
public class AnsyDataProcessService : ServiceBase, IAnsyDataProcessService
|
||
{
|
||
#region 初始化
|
||
private readonly IMyCodeCacheService _myCodeCacheService;
|
||
|
||
public AnsyDataProcessService(IMyCodeCacheService myCodeCacheService)
|
||
{
|
||
_myCodeCacheService = myCodeCacheService;
|
||
}
|
||
#endregion
|
||
|
||
#region AddQueue(添加队列)
|
||
|
||
/// <summary>
|
||
/// 添加队列
|
||
/// </summary>
|
||
/// <param name="methodName">方法名</param>
|
||
/// <param name="entity">参数信息</param>
|
||
public void AddQueue<T>(string methodName, DateTime? expireTime, object entity = null)
|
||
{
|
||
AddQueue(typeof(T), methodName, expireTime, entity);
|
||
}
|
||
#endregion
|
||
|
||
#region AddQueue(添加队列)
|
||
/// <summary>
|
||
/// 添加队列
|
||
/// </summary>
|
||
/// <param name="type">执行类</param>
|
||
/// <param name="methodName">方法名</param>
|
||
/// <param name="entity">参数信息</param>
|
||
public void AddQueue(Type type, string methodName, DateTime? expireTime, object entity = null)
|
||
{
|
||
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.ToString()
|
||
: JsonConvert.SerializeObject(entity);
|
||
AddQueue(typePath, methodName, expireTime, paramInfo);
|
||
}
|
||
#endregion
|
||
|
||
#region AddQueue(添加队列)
|
||
/// <summary>
|
||
/// 添加调度任务
|
||
/// </summary>
|
||
/// <param name="typePath">类型路径,如:Lxm.IServices.IWorkProcessService, Lxm.Services</param>
|
||
/// <param name="methodName">方法名</param>
|
||
/// <param name="paramInfo">参数信息</param>
|
||
public void AddQueue(string typePath, string methodName, DateTime? expireTime, string paramInfo = "")
|
||
{
|
||
var model = new QueueProcess()
|
||
{
|
||
MethodName = methodName,
|
||
Parameter = paramInfo,
|
||
TypePath = typePath,
|
||
CreateTime = DateTime.Now,
|
||
ExpireTime = expireTime
|
||
};
|
||
|
||
//这里判断下,队列中是否已经存在
|
||
_myCodeCacheService.Push(CacheKey.AnsyDataQueueCacheKey, model);
|
||
}
|
||
#endregion
|
||
|
||
#region GetLen(返回队列长度)
|
||
/// <summary>
|
||
/// 返回队列长度
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public long GetLen()
|
||
{
|
||
return _myCodeCacheService.ListLen(CacheKey.AnsyDataQueueCacheKey);
|
||
}
|
||
#endregion
|
||
|
||
#region Execute(调度执行,普通调度)
|
||
|
||
/// <summary>
|
||
/// 涉及到资源释放问题,每次开启一个线程去执行
|
||
/// </summary>
|
||
public void Execute()
|
||
{
|
||
//数据同步有异常发生,请等待处理
|
||
if (_myCodeCacheService.Exists(CacheKey.AnsyDataErrorCacheKey))
|
||
{
|
||
Console.WriteLine("数据同步有异常发生,请等待处理,当前同步停止");
|
||
return;
|
||
}
|
||
|
||
var queueLen = _myCodeCacheService.ListLen(CacheKey.AnsyDataQueueCacheKey);
|
||
|
||
Console.WriteLine($"目前同步数据队列长度:{queueLen}");
|
||
|
||
var objProcess = _myCodeCacheService.Popup(CacheKey.AnsyDataQueueCacheKey);
|
||
|
||
if (objProcess == null) { return; }
|
||
|
||
var process = (QueueProcess)objProcess;
|
||
|
||
_myCodeCacheService.Set(CacheKey.AnsyDataErrorCacheKey, objProcess, new TimeSpan(3600, 0, 0));
|
||
|
||
//这里对AOP的事务或者缓存也会生效
|
||
var type = UnityHelper.GetUnityContainer().Resolve(Type.GetType(process.TypePath));
|
||
|
||
Console.WriteLine($"执行方法:{process.TypePath}.{process.MethodName}");
|
||
|
||
Console.WriteLine($"参数:{process.Parameter}");
|
||
|
||
MethodInfo method = type.GetType().GetMethod(process.MethodName);
|
||
|
||
try
|
||
{
|
||
if (!string.IsNullOrEmpty(process.Parameter))
|
||
{
|
||
method.Invoke(type, new object[] { process.Parameter });
|
||
}
|
||
else
|
||
{
|
||
method.Invoke(type, new object[] { });
|
||
}
|
||
_myCodeCacheService.Delete(CacheKey.AnsyDataErrorCacheKey);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Error($"执行方法:{process.TypePath}.{process.MethodName},传参:{process.Parameter}");
|
||
LogHelper.Error(ex);
|
||
LogHelper.Error("完整JSON:" + JsonHelper.ToJson(process) );
|
||
|
||
//DingDingHelper.SendMsg($"调度QueueProcess异常:{ex}");
|
||
}
|
||
|
||
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|