Wms/Model/ModelVm/JobVm.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Wms/Utility/Entity/LogModel.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Wms/Utility/Entity/ScheduleEntity.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Wms/Utility/Job/Constant.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Wms/Utility/Job/HttpJob.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Wms/Utility/Job/InternalMethodJob.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Wms/Utility/Job/JobBase.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Wms/Utility/Job/SchedulerCenter.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Wms/Utility/Utility.csproj | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Wms/Wms/Controllers/JobController.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
Wms/Model/ModelVm/JobVm.cs
New file @@ -0,0 +1,19 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text; namespace Model.ModelVm { public class JobVm { /// <summary> /// 任务名称 /// </summary> public string Name { get; set; } /// <summary> /// 任务组 /// </summary> public string Group { get; set; } } } Wms/Utility/Entity/LogModel.cs
@@ -4,7 +4,7 @@ namespace Utility.Entity { public abstract class LogModel public class LogModel { /// <summary> /// 开始执行时间 Wms/Utility/Entity/ScheduleEntity.cs
@@ -68,6 +68,20 @@ public RequestTypeEnum RequestType { get; set; } = RequestTypeEnum.Post; #endregion #region 内置方法 /// <summary> /// 类名 /// </summary> public string ClassName { get; set; } /// <summary> /// 方法名 /// </summary> public string MethodName { get; set; } /// <summary> /// 参数 /// </summary> public string MethodParameters { get; set; } #endregion } public class ModifyJobInput Wms/Utility/Job/Constant.cs
@@ -57,5 +57,9 @@ public static string Payload = "Payload"; public static string BuiltIn = "BuiltIn"; public const string CLASSNAME = "ClassName"; public const string METHODNAME = "MethodName"; public const string METHODPARAMETERS = "MethodParameters"; } } Wms/Utility/Job/HttpJob.cs
@@ -15,7 +15,7 @@ { public class HttpJob : JobBase<LogUrlModel>, IJob { public HttpJob(ILogger logger) : base(new LogUrlModel(), logger) public HttpJob() : base(new LogUrlModel()) { } public override async Task NextExecute(IJobExecutionContext context) Wms/Utility/Job/InternalMethodJob.cs
New file @@ -0,0 +1,93 @@ using Autofac; using AutoMapper; using Newtonsoft.Json; using Quartz; using Serilog; using System; using System.Collections.Generic; using System.Reflection; using System.Text; using System.Threading.Tasks; using Utility.Entity; namespace Utility.Job { public class InternalMethodJob : JobBase<LogModel>, IJob { public InternalMethodJob() : base(new LogModel()) { } public override async Task NextExecute(IJobExecutionContext context) { // 获取相关参数 var className = context.JobDetail.JobDataMap.GetString(Constant.CLASSNAME)?.Trim(); var methodName = context.JobDetail.JobDataMap.GetString(Constant.METHODNAME)?.Trim(); var methodParameters = context.JobDetail.JobDataMap.GetString(Constant.METHODPARAMETERS); try { /// 加载 WMS.BLL 类库 Assembly assembly = Assembly.LoadFrom("bin\\Debug\\netcoreapp3.1\\WMS.BLL.dll"); // 获取 WMS.BLL的类型 Type classType = assembly.GetType("WMS.BLL." + className); if (classType == null) { throw new TypeLoadException($"找不到该类型{className}"); } #region 容器创建示例 有报错搞不定抽空再弄 //var builder = new ContainerBuilder(); //// 注册需要的服务和类型 //builder.RegisterAssemblyTypes(Assembly.Load("WMS.IDAL"), Assembly.Load("WMS.DAL"), Assembly.Load("WMS.IBLL"), // Assembly.Load("WMS.BLL")); //builder.RegisterType<Mapper>().As<IMapper>(); //// 使用 Autofac 容器来解析类的实例 //var scope = builder.Build(); //var instance = scope.Resolve(classType); #endregion // 创建实例(如果方法是实例方法) 无注入 必须有空参数的构造函数 object instance = Activator.CreateInstance(classType); // 获取方法信息 MethodInfo methodInfo = classType.GetMethod(methodName); if (methodInfo == null) { throw new ArgumentException($"找不到该方法{methodName}"); } // 调用方法 object result = null; if (methodInfo.GetParameters().Length == 0) { // 无参方法 result = methodInfo.Invoke(instance, null); } else { // 有参方法 var parameters = JsonConvert.DeserializeObject<object[]>(methodParameters); // 解析方法参数 result = await (Task<object>)methodInfo.Invoke(instance, parameters); } // 处理方法执行结果 LogInfo.Result = JsonConvert.SerializeObject(result); } catch (Exception ex) { LogInfo.ErrorMsg = ex.Message; context.JobDetail.JobDataMap[Constant.EXCEPTION] = $"<div class='err-time'>{LogInfo.BeginTime}</div>{JsonConvert.SerializeObject(LogInfo)}"; } //_logger.Error(ex, $"执行方法时出错 {methodName}: {ex.Message}"); } } } Wms/Utility/Job/JobBase.cs
@@ -19,11 +19,9 @@ protected Stopwatch stopwatch = new Stopwatch(); protected T LogInfo { get; private set; } private ILogger _logger; public JobBase(T logInfo,ILogger logger) public JobBase(T logInfo) { LogInfo = logInfo; _logger = logger; } public async Task Execute(IJobExecutionContext context) @@ -36,7 +34,15 @@ return; } //记录执行次数 var runNumber = context.JobDetail.JobDataMap.GetLong(Constant.RUNNUMBER); long runNumber = 0; try { runNumber = context.JobDetail.JobDataMap.GetLong(Constant.RUNNUMBER); } catch (Exception) { } context.JobDetail.JobDataMap[Constant.RUNNUMBER] = ++runNumber; var logs = context.JobDetail.JobDataMap[Constant.LOGLIST] as List<string> ?? new List<string>(); @@ -75,19 +81,19 @@ public abstract Task NextExecute(IJobExecutionContext context); public async Task WarningAsync(string title, string msg) { _logger.Warning(msg); } //public async Task WarningAsync(string title, string msg) //{ // _logger.Warning(msg); //} public async Task InformationAsync(string title, string msg) { _logger.Information(msg); } //public async Task InformationAsync(string title, string msg) //{ // _logger.Information(msg); //} public async Task ErrorAsync(string title, Exception ex, string msg) { _logger.Error(ex, msg); } //public async Task ErrorAsync(string title, Exception ex, string msg) //{ // _logger.Error(ex, msg); //} } } Wms/Utility/Job/SchedulerCenter.cs
@@ -143,6 +143,13 @@ httpDir.Add(Constant.REQUESTPARAMETERS, entity.RequestParameters); httpDir.Add(Constant.REQUESTTYPE, ((int)entity.RequestType).ToString()); } else if (entity.JobType == JobTypeEnum.BuiltIn) { jobConfigurator = JobBuilder.Create<InternalMethodJob>(); httpDir.Add(Constant.CLASSNAME, entity.ClassName); httpDir.Add(Constant.METHODNAME, entity.MethodName); httpDir.Add(Constant.METHODPARAMETERS, entity.MethodParameters); } // 定义这个工作,并将其绑定到我们的IJob实现类 IJobDetail job = jobConfigurator Wms/Utility/Utility.csproj
@@ -14,6 +14,7 @@ <ItemGroup> <PackageReference Include="Autofac" Version="8.0.0" /> <PackageReference Include="AutoMapper" Version="12.0.1" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.32" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" /> Wms/Wms/Controllers/JobController.cs
@@ -6,6 +6,7 @@ using System.Threading.Tasks; using Utility.Entity; using Utility; using Model.ModelVm; namespace Wms.Controllers { @@ -50,7 +51,7 @@ /// </summary> /// <returns></returns> [HttpPost] public async Task<string> StopJob([FromBody] JobKey job) public async Task<string> StopJob(JobVm job) { return await scheduler.StopOrDelScheduleJobAsync(job.Group, job.Name); } @@ -60,7 +61,7 @@ /// </summary> /// <returns></returns> [HttpPost] public async Task<string> RemoveJob([FromBody] JobKey job) public async Task<string> RemoveJob(JobVm job) { return await scheduler.StopOrDelScheduleJobAsync(job.Group, job.Name, true); } @@ -70,7 +71,7 @@ /// </summary> /// <returns></returns> [HttpPost] public async Task<string> ResumeJob([FromBody] JobKey job) public async Task<string> ResumeJob(JobVm job) { return await scheduler.ResumeJobAsync(job.Group, job.Name); } @@ -80,7 +81,7 @@ /// </summary> /// <returns></returns> [HttpPost] public async Task<ScheduleEntity> QueryJob([FromBody] JobKey job) public async Task<ScheduleEntity> QueryJob(JobVm job) { return await scheduler.QueryJobAsync(job.Group, job.Name); } @@ -106,9 +107,10 @@ /// <param name="job"></param> /// <returns></returns> [HttpPost] public async Task<bool> TriggerJob([FromBody] JobKey job) public async Task<bool> TriggerJob(JobVm job) { await scheduler.TriggerJobAsync(job); JobKey modKey = new JobKey(job.Name, job.Group); await scheduler.TriggerJobAsync(modKey); return true; } @@ -118,9 +120,10 @@ /// <param name="jobKey"></param> /// <returns></returns> [HttpPost] public async Task<List<string>> GetJobLogs([FromBody] JobKey jobKey) public async Task<List<string>> GetJobLogs(JobVm job) { var logs = await scheduler.GetJobLogsAsync(jobKey); JobKey modKey = new JobKey(job.Name, job.Group); var logs = await scheduler.GetJobLogsAsync(modKey); logs?.Reverse(); return logs; }