/// <summary> /// 处理步骤连线信息 /// </summary> /// <param name="db"></param> /// <param name="versionDTO"></param> /// <param name="model"></param> /// <param name="version"></param> /// <param name="ruleStepMap"></param> private void HandleHop(SqlSugarClient db, ModelVersionAddDTO versionDTO, DmeModel model, DmeModelVersion version, IDictionary <string, DmeRuleStep> ruleStepMap) { IList <RuleStepHopDTO> ruleStepHopDTOs = versionDTO.Hops; if (ruleStepHopDTOs?.Count == 0) { return; } List <DmeRuleStepHop> hops = new List <DmeRuleStepHop>(); foreach (var hopDTO in ruleStepHopDTOs) { DmeRuleStepHop hop = new DmeRuleStepHop { ModelId = model.Id, VersionId = version.Id, StepFromId = ruleStepMap[hopDTO.StepFromName].Id, StepToId = ruleStepMap[hopDTO.StepToName].Id, Enabled = hopDTO.Enabled, Name = hopDTO.Name }; hops.Add(hop); } db.Insertable <DmeRuleStepHop>(hops).ExecuteCommand(); }
public async Task <bool> RestoreModelAsync(string modelCode) { return(await Task.Run <Boolean>(() => { var db = Repository.GetDbContext(); return db.Ado.UseTran <Boolean>(() => { DmeModel model = db.Queryable <DmeModel>().Single(m => m.SysCode == modelCode); if (null == model) { throw new BusinessException((int)EnumSystemStatusCode.DME_FAIL, $"模型[{modelCode}]不存在"); } model.Status = 1; db.Updateable <DmeModel>(model).UpdateColumns(m => m.Status).ExecuteCommand(); // 级联逻辑恢复版本 var updateCount = db.Updateable <DmeModelVersion>() .UpdateColumns(mv => new DmeModelVersion() { Status = 1 }) .Where(it => it.ModelId == model.Id).ExecuteCommand(); LOG.Info($"共更新[{updateCount}]条版本记录"); return true; }).Data; })); }
public async Task <Boolean> DeleteModelAsync(string modelCode) { return(await Task.Run <Boolean>(() => { var db = Repository.GetDbContext(); return db.Ado.UseTran <Boolean>(() => { DmeModel model = db.Queryable <DmeModel>().Single(m => m.SysCode == modelCode); if (null == model) { throw new BusinessException((int)EnumSystemStatusCode.DME_FAIL, $"模型[{modelCode}]不存在"); } model.Status = 0; db.Updateable <DmeModel>(model).UpdateColumns(m => m.Status).ExecuteCommand(); // 级联逻辑删除版本 var updateCount = db.Updateable <DmeModelVersion>() .UpdateColumns(it => new DmeModelVersion() { Status = 0 }) .Where(it => it.ModelId == model.Id).ExecuteCommand(); LOG.Info($"共更新[{updateCount}]条版本记录"); // 暂停模型相关的任务 List <DmeTask> tasks = db.Queryable <DmeTask>().Where(t => t.ModelId == model.Id).ToList(); if (tasks?.Count > 0) { foreach (var subTask in tasks) { DmeQuartzScheduler <TaskRunnerJob> .PauseJob(subTask.SysCode, model.ModelTypeCode); } } return true; }).Data; })); }
/// <summary> /// 处理版本信息 /// </summary> /// <param name="db"></param> /// <param name="model"></param> /// <param name="dto"></param> /// <returns>返回版本编码数组</returns> private IList <string> HandleVersions(SqlSugarClient db, DmeModel model, ModelAddReqDTO dto) { IList <ModelVersionAddDTO> versions = dto.Versions; if (versions?.Count == 0) { throw new BusinessException("注册模型时,缺失版本信息。"); } IList <string> versionCodes = new List <string>(); foreach (var subVersion in versions) { DmeModelVersion mv = new DmeModelVersion { SysCode = GuidUtil.NewGuid(), Name = subVersion.Name, ModelId = model.Id, CreateTime = DateUtil.CurrentTimeMillis }; mv = db.Insertable <DmeModelVersion>(mv).ExecuteReturnEntity(); versionCodes.Add(mv.SysCode); // 添加步骤信息 IList <RuleStepAddDTO> stepsAdd = subVersion.Steps; if (stepsAdd?.Count == 0) { continue; } // 步骤名称与步骤实体的映射 IDictionary <string, DmeRuleStep> ruleStepMap = new Dictionary <string, DmeRuleStep>(); foreach (var subStepAdd in stepsAdd) { RuleStepTypeDTO ruleStepTypeDTO = subStepAdd.StepType; if (null == ruleStepTypeDTO) { throw new BusinessException("注册模型,缺失步骤类型元数据。"); } DmeRuleStepType dmeRuleStepType = db.Queryable <DmeRuleStepType>().Single(rst => rst.Code == ruleStepTypeDTO.Code); DmeRuleStep step = new DmeRuleStep { SysCode = GuidUtil.NewGuid(), ModelId = model.Id, VersionId = mv.Id, X = subStepAdd.X, Y = subStepAdd.Y, Name = subStepAdd.Name, Remark = subStepAdd.Remark, StepTypeId = dmeRuleStepType.Id }; step = db.Insertable <DmeRuleStep>(step).ExecuteReturnEntity(); ruleStepMap[subStepAdd.Name] = step; // 处理步骤属性 this.HandleAttributes(db, model, mv, subStepAdd, step); } // 处理步骤之间的连接关系 this.HandleHop(db, subVersion, model, mv, ruleStepMap); } return(versionCodes); }
public object OperateTask(string taskCode, int operation) { var db = Repository.GetDbContext(); DmeTask task = db.Queryable <DmeTask>().Single(t => t.SysCode == taskCode); if (null == task) { throw new BusinessException((int)EnumSystemStatusCode.DME_FAIL, $"任务[{taskCode}]不存在"); } DmeModel model = db.Queryable <DmeModel>().InSingle(task.ModelId); switch (operation) { case 0: LOG.Info($"停止任务[{taskCode}]"); task.Status = EnumUtil.GetEnumDisplayName(EnumSystemStatusCode.DME_STOP); task.LastTime = DateUtil.CurrentTimeMillis; db.Updateable <DmeTask>(task).UpdateColumns(t => new { task.Status, task.LastTime }).ExecuteCommand(); DmeQuartzScheduler <TaskRunnerJob> .PauseJob(task.SysCode, model.ModelTypeCode); break; case 1: // @TODO 有待完成 LOG.Info($"重启任务[{taskCode}]"); task.Status = EnumUtil.GetEnumDisplayName(EnumSystemStatusCode.DME_RUNNING); task.LastTime = DateUtil.CurrentTimeMillis; db.Updateable <DmeTask>(task).UpdateColumns(t => new { task.Status, task.LastTime }).ExecuteCommand(); DmeQuartzScheduler <TaskRunnerJob> .ResumeJob(task.SysCode, model.ModelTypeCode); break; case -1: LOG.Info($"删除任务[{taskCode}]"); // 删除与任务相关的步骤记录和结果记录 int count = db.Deleteable <DmeTaskRuleStep>(trs => trs.TaskId == task.Id).ExecuteCommand(); LOG.Info($"删除任务[{taskCode}]关联的[{count}]个步骤记录"); count = db.Deleteable <DmeTaskResult>(tr => tr.TaskId == task.Id).ExecuteCommand(); LOG.Info($"删除任务[{taskCode}]关联的[{count}]个结果记录"); // 从mongo中获取 var filter = Builders <TaskResultColl> .Filter.And( Builders <TaskResultColl> .Filter.Eq("TaskId", task.Id)); MongodbHelper <TaskResultColl> .DeleteMany(ServiceFactory.MongoDatabase, filter); db.Deleteable <DmeTask>(task).ExecuteCommand(); DmeQuartzScheduler <TaskRunnerJob> .DeleteJob(task.SysCode, model.ModelTypeCode); break; default: break; } return(true); }
private Task RunTaskAsyncEx(SqlSugarClient db, DmeModel model, DmeModelVersion modelVersion, DmeTask task, IList <DmeRuleStep> ruleSteps) { return(Task.Run(() => { return db.Ado.UseTran <DmeTask>(() => { try { // 查询步骤前后依赖关系 // 形成链表 IList <DmeRuleStepHop> hops = db.Queryable <DmeRuleStepHop>().Where(rsh => rsh.ModelId == model.Id && rsh.VersionId == modelVersion.Id).OrderBy("STEP_FROM_ID").ToList(); IList <RuleStepLinkedListNode <DmeRuleStep> > rulestepLinkedList = this.GetRuleStepNodeLinkedList(db, model, modelVersion, ruleSteps); foreach (var item in rulestepLinkedList) { // 开始计算步骤 this.RunRuleStepNode(db, task, item); } // 完成模型计算 task.Status = EnumUtil.GetEnumDisplayName(EnumSystemStatusCode.DME_SUCCESS); task.LastTime = DateUtil.CurrentTimeMillis; db.Updateable <DmeTask>(task).ExecuteCommand(); // @TODO 发送模型计算成功的消息 KafkaProducer.Send(nameof(EnumMessageType.TASK), new MessageBody() { // 创建者一致 From = "123", To = "123", ChannelType = EnumChannelType.P2P, MessageType = EnumMessageType.TASK, Payload = $"模型[{task.SysCode}]计算完成,状态[{task.Status}]" }); return task; } catch (Exception ex) { // 更改任务执行的状态 if (ex is BusinessException) { task.Status = EnumUtil.GetEnumDisplayName(EnumUtil.GetEnumObjByValue <EnumSystemStatusCode>(((BusinessException)ex).Code)); } else { task.Status = EnumUtil.GetEnumDisplayName(EnumSystemStatusCode.DME_ERROR); } task.LastTime = DateUtil.CurrentTimeMillis; db.Updateable <DmeTask>(task).ExecuteCommand(); // 添加日志 this.LogService.AddLogAsync(Base.Common.Log.EnumLogType.ENTITY, Base.Common.Log.EnumLogLevel.ERROR, nameof(DmeTask), task.SysCode, "", ex, "", NetAssist.GetLocalHost()); throw ex; } }); })); }
public object AddModel(ModelAddReqDTO dto) { if (string.IsNullOrEmpty(dto.SysCode)) { // 自动赋予一个唯一编码 dto.SysCode = GuidUtil.NewGuid(); } // 使用事务 DbResult <ModelRegisterRespDTO> dbResult = base.Db.Ado.UseTran <ModelRegisterRespDTO>(() => { // 查询单条没有数据返回NULL, Single超过1条会报错,First不会 // base.DmeModelDb.GetContext().Queryable<DmeModel>().Single(m => m.SysCode == dto.SysCode); DmeModel model = base.Db.Queryable <DmeModel>().Where(m => m.SysCode == dto.SysCode).Single(); if (null == model) { model = new DmeModel { SysCode = dto.SysCode, Name = dto.Name, Remark = dto.Remark, CreateTime = DateUtil.CurrentTimeMillis, IsPublish = 1, PublishTime = DateUtil.CurrentTimeMillis, Status = 1 }; // 模型类型 DmeModelType modelType = base.Db.Queryable <DmeModelType>().Single(mt => mt.SysCode == dto.TypeCode); if (modelType != null) { model.ModelTypeId = modelType.Id; model.ModelTypeCode = modelType.SysCode; } model = base.Db.Insertable <DmeModel>(model).ExecuteReturnEntity(); if (null == model) { throw new Exception(String.Format("创建模型失败,原因未明,编码:[{0}],名称:[{1}]。", dto.SysCode, dto.Name)); } } else { LOG.Info($"模型[{dto.SysCode}]已存在,作为新增版本信息"); } ModelRegisterRespDTO respDTO = ClassValueCopier <ModelRegisterRespDTO> .Copy(model); // 处理版本 respDTO.VersionCodes = this.HandleVersions(base.Db, model, dto); return(respDTO); }); return(dbResult.Data); }
/// <summary> /// 处理步骤属性信息 /// </summary> /// <param name="db"></param> /// <param name="model"></param> /// <param name="version"></param> /// <param name="subStepAdd"></param> /// <param name="step"></param> private void HandleAttributes(SqlSugarClient db, DmeModel model, DmeModelVersion version, RuleStepAddDTO subStepAdd, DmeRuleStep step) { IList <AttributeReqDTO> properties = subStepAdd.Attributes; if (properties?.Count == 0) { LOG.Warn("没有可处理的步骤属性信息"); return; } if (Register.RuleStepPluginsMap.ContainsKey(subStepAdd.StepType.Code)) { RuleStepPluginRegisterDTO ruleStepPluginRegisterDTO = Register.RuleStepPluginsMap[subStepAdd.StepType.Code]; String baseDir = AppDomain.CurrentDomain.BaseDirectory; string assemblyPath = Path.Combine(baseDir, ruleStepPluginRegisterDTO.Assembly); Assembly assembly = Assembly.LoadFile(assemblyPath); IRuleStepData ruleStepData = (IRuleStepData)assembly.CreateInstance(ruleStepPluginRegisterDTO.ClassId, true, BindingFlags.CreateInstance, null , new object[] { Repository, null, step }, null, null); if (null == ruleStepData) { LOG.Warn($"无法创建步骤实体[{ruleStepPluginRegisterDTO.ClassId}]"); return; } IDictionary <string, Property> attributes = new Dictionary <string, Property>(); // DmeRuleStepAttribute dmeRuleStepAttribute = null; foreach (var p in properties) { EnumValueMetaType enumValueMetaType = new EnumValueMetaType(); if (string.IsNullOrEmpty(p.DataTypeCode)) { enumValueMetaType = EnumValueMetaType.TYPE_UNKNOWN; } else { enumValueMetaType = EnumUtil.GetEnumObjByName <EnumValueMetaType>(p.DataTypeCode); } attributes[p.Name] = new Property(p.Name, p.Name, enumValueMetaType, p.Value, null, null, null, 1, 0, 1, p.DataSourceCode, p.IsNeedPrecursor, p.Type); } ruleStepData.SaveAttributes(attributes); } }
public object UpdateModelBasicInfo(ModelBasicInfoUpdateDTO dto) { DmeModel model = base.Db.Queryable <DmeModel>().Single(m => m.SysCode == dto.SysCode); if (null == model) { throw new BusinessException((int)EnumSystemStatusCode.DME_ERROR, $"模型[{dto.SysCode}]不存在"); } DmeModelType modelType = base.Db.Queryable <DmeModelType>().Single(mt => mt.SysCode == dto.TypeCode); if (null == modelType) { throw new BusinessException((int)EnumSystemStatusCode.DME_ERROR, $"模型类型[{dto.TypeCode}]不存在"); } model.Name = dto.Name; model.Remark = dto.Remark; model.ModelTypeId = modelType.Id; model.ModelTypeCode = modelType.SysCode; base.Db.Updateable <DmeModel>(model).ExecuteCommand(); return(true); }
/// <summary> /// 内部方法,获取模型元数据 /// </summary> /// <param name="model"></param> /// <param name="detail"></param> /// <returns></returns> private ModelDTO GetModelMetadata(DmeModel model, Boolean detail) { ModelDTO modelDTO = ClassValueCopier <ModelDTO> .Copy(model); if (!detail) { return(modelDTO); } var db = base.Repository.GetDbContext(); // 获取模型版本 IList <DmeModelVersion> versions = db.Queryable <DmeModelVersion>().Where(mv => mv.ModelId == model.Id).ToList(); if (null == versions || 0 == versions.Count) { return(modelDTO); } foreach (var v in versions) { ModelVersionDTO versionDTO = ClassValueCopier <ModelVersionDTO> .Copy(v); modelDTO.Versions.Add(versionDTO); IList <DmeRuleStep> ruleStepEntities = db.Queryable <DmeRuleStep>().Where(rs => rs.ModelId == model.Id && rs.VersionId == v.Id).ToList(); if (null == ruleStepEntities || 0 == ruleStepEntities.Count) { continue; } RuleStepDTO ruleStepDTO = null; foreach (var ruleStep in ruleStepEntities) { ruleStepDTO = ClassValueCopier <RuleStepDTO> .Copy(ruleStep); versionDTO.Steps.Add(ruleStepDTO); // 获取步骤类型实体 ruleStepDTO.StepType = db.Queryable <DmeRuleStepType>().Where(rst => rst.Id == ruleStep.StepTypeId).Single(); IRuleStepData ruleStepData = RuleStepFactory.GetRuleStepData(ruleStepDTO.StepType.Code, base.Repository, null, ruleStep); IDictionary <string, Property> attributeDic = ruleStepData.RuleStepMeta.ReadAttributes(); if (attributeDic?.Count > 0) { foreach (var item in attributeDic.Values) { ruleStepDTO.Attributes.Add(item); } } // 检索步骤的属性值信息 //IList<DmeRuleStepAttribute> attributes = db.Queryable<DmeRuleStepAttribute>().Where(rsa => rsa.RuleStepId == ruleStep.Id).ToList(); //if (null == attributes || 0 == attributes.Count) //{ // continue; //} //foreach (var att in attributes) //{ // ruleStepDTO.Attributes.Add(new KeyValuePair<string, object>(att.AttributeCode, att.AttributeValue)); //} } // 每个模型版本下的数据源(多表关联查询) // 当不需要用LEFT JOIN或者 RIGHT JOIN 只是单纯的INNER JOIN时我们还提供了更简单的语法实现多表查询 IList <DmeDataSource> datasources = db.Queryable <DmeDataSource, DmeRuleStepDataSource>((ds, rsds) => ds.Id == rsds.DataSourceId).Select(ds => new DmeDataSource { SysCode = ds.SysCode, Connection = ds.Connection, CreateTime = ds.CreateTime, Id = ds.Id, Name = ds.Name, Remark = ds.Remark, Type = ds.Type }).ToList(); if (datasources?.Count > 0) { foreach (var datasourceItem in datasources) { if (versionDTO.DataSources.ContainsKey(datasourceItem.SysCode)) { continue; } versionDTO.DataSources[datasourceItem.SysCode] = datasourceItem; } } // 每个模型版本下的节点向量信息 IList <DmeRuleStepHop> hops = db.Queryable <DmeRuleStepHop>() .Where(rsh => rsh.ModelId == model.Id && rsh.VersionId == v.Id) .OrderBy(rsh => rsh.StepFromId) .ToList(); if (hops?.Count > 0) { DmeRuleStep stepFromTemp = null; DmeRuleStep stepToTemp = null; foreach (var item in hops) { stepFromTemp = db.Queryable <DmeRuleStep>().InSingle(item.StepFromId); stepToTemp = db.Queryable <DmeRuleStep>().InSingle(item.StepToId); if (null == stepFromTemp || null == stepToTemp) { LOG.Warn($"开始步骤[{item.StepFromId}],或者结束步骤[{item.StepToId}]找不到对应实体信息"); continue; } versionDTO.Hops.Add(new RuleStepHopDTO(stepFromTemp.Name, stepToTemp.Name, item.Enabled, item.Name)); } } } return(modelDTO); }
public object GetModelMetadata(string modelCode, Boolean detail) { DmeModel model = base.Repository.GetDbContext().Queryable <DmeModel>().Single(m => m.SysCode == modelCode); return(this.GetModelMetadata(model, detail)); }
public async Task RunTaskScheduleAsync(string taskCode) { var db = Repository.GetDbContext(); DmeTask task = db.Queryable <DmeTask>().Single(t => t.SysCode == taskCode); if (null == task) { throw new BusinessException((int)EnumSystemStatusCode.DME_FAIL, $"任务[{taskCode}]不存在"); } // 查询模型版本 DmeModelVersion modelVersion = db.Queryable <DmeModelVersion>().InSingle(task.VersionId);//.Single(mv => mv.SysCode == dto.ModelVersionCode); if (null == modelVersion) { throw new BusinessException((int)EnumSystemStatusCode.DME_ERROR, $"模型的版本[{task.VersionId}]不存在"); } // Single方法,如果查询数据库多条数据,会抛出异常 DmeModel model = db.Queryable <DmeModel>().InSingle(task.ModelId); if (null == model) { throw new BusinessException((int)EnumSystemStatusCode.DME_ERROR, $"模型[{task.ModelId}]不存在"); } // 查找关联的算法信息 IList <DmeRuleStep> ruleSteps = db.Queryable <DmeRuleStep>().Where(rs => rs.ModelId == model.Id && rs.VersionId == modelVersion.Id).ToList(); if (0 == ruleSteps?.Count) { LOG.Warn($"模型[{model.SysCode}]的版本[{modelVersion.SysCode}]下没有可执行步骤,停止运行"); return; } try { // 清理掉任务之前的过程结果 // 包括DME_TASK_RESULT、DME_TASK_RULESTEP和mongo TaskResultColl db.Ado.UseTran(() => { int count = db.Deleteable <DmeTaskRuleStep>().Where(trs => trs.TaskId == task.Id).ExecuteCommand(); LOG.Info($"删除任务关联的步骤记录[{count}]条"); db.Deleteable <DmeTaskResult>().Where(tr => tr.TaskId == task.Id).ExecuteCommand(); LOG.Info($"删除任务结果记录[{count}]条"); var filter = Builders <TaskResultColl> .Filter.And( Builders <TaskResultColl> .Filter.Eq("TaskCode", task.SysCode)); DeleteResult deleteResult = MongodbHelper <TaskResultColl> .DeleteMany(ServiceFactory.MongoDatabase, filter); LOG.Info($"删除mongo记录[{deleteResult.DeletedCount}]条"); LOG.Info($"清理缓存计算数据"); IList <string> cacheKeys = new List <string>(); foreach (var item in ruleSteps) { cacheKeys.Add(HashUtil.Hash_2_MD5_32($"{task.SysCode}_{item.SysCode}")); } ServiceFactory.CacheService.RemoveAllAsync(cacheKeys); // 修改任务状态为running task.Status = EnumUtil.GetEnumDisplayName(EnumSystemStatusCode.DME_RUNNING); db.Updateable <DmeTask>(task).UpdateColumns(t => t.Status).ExecuteCommand(); }); // 此时不阻塞,返回类型为Task,为了能捕获到线程异常信息 await RunTaskAsyncEx(db, model, modelVersion, task, ruleSteps); } catch (Exception ex) { LOG.Error(ex, ex.Message); // 更改任务状态 task.Status = EnumUtil.GetEnumDisplayName(EnumSystemStatusCode.DME_ERROR); task.LastTime = DateUtil.CurrentTimeMillis; db.Updateable <DmeTask>(task).UpdateColumns(t => new { task.Status, task.LastTime }).ExecuteCommand(); } }
/// <summary> /// 构建步骤的链表信息 /// </summary> /// <param name="db"></param> /// <param name="ruleSteps"></param> /// <returns>多个链表</returns> private IList <RuleStepLinkedListNode <DmeRuleStep> > GetRuleStepNodeLinkedList(SqlSugarClient db, DmeModel model, DmeModelVersion modelVersion, IList <DmeRuleStep> ruleSteps) { IList <RuleStepLinkedListNode <DmeRuleStep> > newLinkedSteps = new List <RuleStepLinkedListNode <DmeRuleStep> >(); // 一次性构建步骤实体字典 IDictionary <int, RuleStepLinkedListNode <DmeRuleStep> > ruleStepDic = new Dictionary <int, RuleStepLinkedListNode <DmeRuleStep> >(); foreach (var subStep in ruleSteps) { ruleStepDic[subStep.Id] = new RuleStepLinkedListNode <DmeRuleStep>(subStep); newLinkedSteps.Add(ruleStepDic[subStep.Id]); } //IList<RuleStepLinkedListNode<DmeRuleStep>> multiLinkedList = new List<RuleStepLinkedListNode<DmeRuleStep>>(); IList <DmeRuleStepHop> hops = db.Queryable <DmeRuleStepHop>().Where(rsh => rsh.ModelId == model.Id && rsh.VersionId == modelVersion.Id).OrderBy(rsh => rsh.StepFromId).ToList(); if (0 == hops?.Count) { return(newLinkedSteps); } IDictionary <int, RuleStepLinkedListNode <DmeRuleStep> > newRuleStepLinkedNodeDic = new Dictionary <int, RuleStepLinkedListNode <DmeRuleStep> >(); // 已经使用的步骤id集合 IList <int> usedStepIds = new List <int>(); RuleStepLinkedListNode <DmeRuleStep> linkedStepFromNode = null; RuleStepLinkedListNode <DmeRuleStep> linkedStepToNode = null; // 反过来构建? foreach (var hop in hops) { if (!ruleStepDic.ContainsKey(hop.StepFromId) || !ruleStepDic.ContainsKey(hop.StepToId) || 0 == hop.Enabled) { continue; } linkedStepFromNode = ruleStepDic[hop.StepFromId]; linkedStepToNode = ruleStepDic[hop.StepToId]; linkedStepFromNode.Next.Add(linkedStepToNode); linkedStepToNode.Previous.Add(linkedStepFromNode); } return(newLinkedSteps); }
public async Task <DmeTask> CreateTaskAsync(NewTaskReqDTO dto) { // 验证数据库是否存在指定模型版本信息 SqlSugarClient db = base.Repository.GetDbContext(); // 查询模型版本 DmeModelVersion modelVersion = db.Queryable <DmeModelVersion>().Single(mv => mv.SysCode == dto.ModelVersionCode); if (null == modelVersion) { throw new BusinessException((int)EnumSystemStatusCode.DME_ERROR, $"模型的版本[{dto.ModelVersionCode}]不存在"); } // Single方法,如果查询数据库多条数据,会抛出异常 DmeModel model = db.Queryable <DmeModel>().Single(m => m.Id == modelVersion.ModelId); if (null == model) { throw new BusinessException((int)EnumSystemStatusCode.DME_ERROR, $"模型[{modelVersion.ModelId}]不存在"); } // 获取模型类型 DmeModelType modelType = db.Queryable <DmeModelType>().InSingle(model.ModelTypeId); // 查找关联的算法信息 IList <DmeRuleStep> ruleSteps = db.Queryable <DmeRuleStep>().Where(rs => rs.ModelId == model.Id && rs.VersionId == modelVersion.Id).ToList(); if (0 == ruleSteps?.Count) { LOG.Warn($"模型[{model.SysCode}]的版本[{dto.ModelVersionCode}]下没有可执行步骤,停止运行"); return(null); } DmeTask newTask = db.Ado.UseTran <DmeTask>(() => { // 每执行一次模型,生成一次任务 newTask = new DmeTask { SysCode = GuidUtil.NewGuid(), CreateTime = DateUtil.CurrentTimeMillis, Status = EnumUtil.GetEnumDisplayName(EnumSystemStatusCode.DME_WAITTING), ModelId = model.Id, VersionId = modelVersion.Id, Remark = dto.Remark, NodeServer = NetAssist.GetLocalHost() }; if (string.IsNullOrWhiteSpace(dto.Name)) { newTask.Name = "task-" + newTask.CreateTime; } else { newTask.Name = dto.Name; } newTask.LastTime = newTask.CreateTime; newTask = db.Insertable <DmeTask>(newTask).ExecuteReturnEntity(); return(newTask); }).Data; // 创建job await DmeQuartzScheduler <TaskRunnerJob> .NewJob( newTask.SysCode, modelType.SysCode, dto.CronExpression, new Dictionary <string, object> { { TaskRunnerJob.TASK_CODE_KEY, newTask.SysCode } }, 1 == dto.StartNow, DateTimeOffset.Now.AddSeconds(dto.InSeconds), dto.Remark); return(newTask); }