/// <summary> /// 删除实体数据 /// <param name="keyValue">主键</param> /// <summary> /// <returns></returns> public void DeleteEntity(string keyValue) { TSProcessEntity entity = this.BaseRepository().FindEntity <TSProcessEntity>(t => t.F_SchemeInfoId == keyValue && (t.F_State == 1 || t.F_State == 2)); var db = this.BaseRepository().BeginTrans(); try { if (entity != null) { entity.F_State = 10; db.Update(entity); } TSSchemeInfoEntity tSSchemeInfoEntity = new TSSchemeInfoEntity() { F_Id = keyValue, F_DeleteMark = 1 }; db.Update(tSSchemeInfoEntity); db.Commit(); } catch (Exception ex) { db.Rollback(); if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } }
/// <summary> /// 获取页面显示列表数据 /// <summary> /// <param name="pagination">分页参数</param> /// <param name="queryJson">查询参数</param> /// <returns></returns> public IEnumerable <TSSchemeInfoEntity> GetPageList(Pagination pagination, string queryJson) { try { var list = (List <TSSchemeInfoEntity>)schemeService.GetPageList(pagination, queryJson); var list2 = list.FindAll(t => (t.F_State == 1 || t.F_State == 2) && t.F_EndTime < DateTime.Now); foreach (var item in list2) { item.F_State = 4; TSProcessEntity tSProcessEntity = new TSProcessEntity() { F_Id = item.F_PorcessId, F_State = 4 }; tSProcessIBLL.SaveEntity(item.F_PorcessId, tSProcessEntity); } return(list); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } }
/// <summary> /// 启动任务 /// </summary> /// <param name="processId">任务进程主键</param> public void EnAbleJob(string processId) { try { TSProcessEntity tSProcessEntity = tSProcessIBLL.GetProcessEntity(processId); TSSchemeEntity tSSchemeEntity = GetSchemeEntityByInfo(tSProcessEntity.F_SchemeInfoId); TSSchemeModel tSSchemeModel = tSSchemeEntity.F_Scheme.ToObject <TSSchemeModel>(); if (tSProcessEntity.F_SchemeId != tSSchemeEntity.F_Id) { tSProcessEntity.F_State = 10; tSProcessIBLL.SaveEntity(tSProcessEntity.F_Id, tSProcessEntity); // 如果模板更改需要重新创建一个任务进程 tSProcessEntity = new TSProcessEntity() { F_SchemeId = tSSchemeEntity.F_Id, F_SchemeInfoId = tSProcessEntity.F_SchemeInfoId, F_State = 2, F_EndType = tSSchemeModel.endType, F_EndTime = tSSchemeModel.endTime }; if (tSSchemeModel.startType == 1) { tSProcessEntity.F_BeginTime = DateTime.Now; } else { tSProcessEntity.F_BeginTime = tSSchemeModel.startTime; } if (tSSchemeModel.endType == 1) { tSProcessEntity.F_EndTime = DateTime.MaxValue; } tSProcessIBLL.SaveEntity("", tSProcessEntity); } else { tSProcessEntity.F_State = 2; tSProcessIBLL.SaveEntity(tSProcessEntity.F_Id, tSProcessEntity); } QuartzHelper.AddJob(tSProcessEntity.F_SchemeInfoId, tSProcessEntity.F_Id, tSSchemeModel); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } }
/// <summary> /// 保存实体数据(新增、修改) /// <param name="keyValue">主键</param> /// <summary> /// <returns></returns> public void SaveEntity(string keyValue, TSProcessEntity entity) { try { processService.SaveEntity(keyValue, entity); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } }
/// <summary> /// 任务执行方法 /// </summary> /// <param name="context"></param> public void Execute(IJobExecutionContext context) { try { JobDataMap dataMap = context.JobDetail.JobDataMap; string keyValue = dataMap.GetString("keyValue"); TSProcessEntity tSProcessEntity = null; TSSchemeEntity tSSchemeEntity = null; if (!_Execute(keyValue)) // 如果异常,需要重新执行一次 { tSProcessEntity = tSProcessIBLL.GetProcessEntity(keyValue); if (tSProcessEntity != null) { tSSchemeEntity = tSSchemeIBLL.GetSchemeEntity(tSProcessEntity.F_SchemeId); if (tSSchemeEntity != null) { TSSchemeModel tSSchemeModel = tSSchemeEntity.F_Scheme.ToObject <TSSchemeModel>(); if (tSSchemeModel.isRestart == 1) { for (int i = 0; i < tSSchemeModel.restartNum; i++) { Thread.Sleep(60 * 1000 * tSSchemeModel.restartMinute); // 停顿1000毫秒 if (_Execute(keyValue)) { break; } } } } } } } catch (Exception) { } }
/// <summary> /// 保存实体数据(新增、修改) /// <param name="keyValue">主键</param> /// <summary> /// <returns></returns> public void SaveEntity(string keyValue, TSSchemeInfoEntity schemeInfoEntity, TSSchemeEntity schemeEntity) { try { TSProcessEntity tSProcessEntity = schemeService.SaveEntity(keyValue, schemeInfoEntity, schemeEntity); if (tSProcessEntity != null) { QuartzHelper.DeleteJob(keyValue); QuartzHelper.AddJob(schemeInfoEntity.F_Id, tSProcessEntity.F_Id, schemeEntity.F_Scheme.ToObject <TSSchemeModel>()); } } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } }
/// <summary> /// 保存实体数据(新增、修改) /// <param name="keyValue">主键</param> /// <summary> /// <returns></returns> public void SaveEntity(string keyValue, TSProcessEntity entity) { var db = this.BaseRepository().BeginTrans(); try { if (!string.IsNullOrEmpty(keyValue)) { entity.Modify(keyValue); db.Update(entity); } else { var entityTmp = db.FindEntity <TSProcessEntity>("SELECT * FROM LR_TS_Process Where F_SchemeInfoId = @schemeId AND F_State != 10 ", new { schemeId = entity.F_SchemeInfoId }); if (entityTmp == null) { db.ExecuteBySql("delete FROM LR_TS_Process Where F_SchemeInfoId = @schemeId AND F_State != 10 ", new { schemeId = entity.F_SchemeInfoId }); } entity.Create(); db.Insert(entity); } db.Commit(); } catch (Exception ex) { db.Rollback(); if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } }
/// <summary> /// 暂停任务 /// </summary> /// <param name="processId">任务进程主键</param> public void PauseJob(string processId) { try { TSProcessEntity tSProcessEntity = tSProcessIBLL.GetProcessEntity(processId); if (tSProcessEntity.F_State == 1 || tSProcessEntity.F_State == 2) { tSProcessEntity.F_State = 3; tSProcessIBLL.SaveEntity(processId, tSProcessEntity); QuartzHelper.DeleteJob(tSProcessEntity.F_SchemeInfoId); } } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } }
/// <summary> /// 任务执行方法 /// </summary> /// <param name="keyValue">任务进程主键</param> /// <returns></returns> private bool _Execute(string keyValue) { bool isOk = true; string msg = "执行成功"; TSProcessEntity tSProcessEntity = null; TSSchemeEntity tSSchemeEntity = null; // 获取一个任务进程 try { tSProcessEntity = tSProcessIBLL.GetProcessEntity(keyValue); } catch (Exception ex) { isOk = false; msg = "获取任务进程异常:" + ex.Message; } if (tSProcessEntity != null && tSProcessEntity.F_State != 1 && tSProcessEntity.F_State != 2) { return(true); } // 获取对应的任务模板 if (isOk) { try { tSSchemeEntity = tSSchemeIBLL.GetSchemeEntity(tSProcessEntity.F_SchemeId); } catch (Exception ex) { isOk = false; msg = "获取任务模板异常:" + ex.Message; } } bool flag = false; // 执行任务 if (isOk) { try { TSSchemeModel tSSchemeModel = tSSchemeEntity.F_Scheme.ToObject <TSSchemeModel>(); switch (tSSchemeModel.methodType) { case 1: // sql databaseLinkIBLL.ExecuteBySql(tSSchemeModel.dbId, tSSchemeModel.strSql); break; case 2: // 存储过程 databaseLinkIBLL.ExecuteByProc(tSSchemeModel.dbId, tSSchemeModel.procName); break; case 3: // 接口 if (tSSchemeModel.urlType == "1") { HttpMethods.Get(tSSchemeModel.url); } else { HttpMethods.Post(tSSchemeModel.url); } break; case 4: // 依赖注入 if (!string.IsNullOrEmpty(tSSchemeModel.iocName) && UnityIocHelper.TsInstance.IsResolve <ITsMethod>(tSSchemeModel.iocName)) { ITsMethod iTsMethod = UnityIocHelper.TsInstance.GetService <ITsMethod>(tSSchemeModel.iocName); iTsMethod.Execute(); } break; } if (tSSchemeModel.executeType == 1) { flag = true; } } catch (Exception ex) { isOk = false; msg = "执行方法出错:" + ex.Message; } } try { // 新增一条任务日志 TSLogEntity logEntity = new TSLogEntity() { F_ExecuteResult = isOk ? 1 : 2, F_Des = msg, F_ProcessId = keyValue }; logEntity.Create(); tSLogIBLL.SaveEntity("", logEntity); if (tSProcessEntity.F_State == 1) { tSProcessEntity.F_State = 2; if (flag) { tSProcessEntity.F_State = 4; } tSProcessIBLL.SaveEntity(tSProcessEntity.F_Id, tSProcessEntity); } } catch (Exception) { } return(isOk); }
/// <summary> /// 保存实体数据(新增、修改) /// <param name="keyValue">主键</param> /// <summary> /// <returns></returns> public TSProcessEntity SaveEntity(string keyValue, TSSchemeInfoEntity schemeInfoEntity, TSSchemeEntity schemeEntity) { TSSchemeModel tSSchemeModel = schemeEntity.F_Scheme.ToObject <TSSchemeModel>(); TSProcessEntity tSProcessEntity = null; TSSchemeEntity schemeEntity2 = null; if (!string.IsNullOrEmpty(keyValue)) { schemeEntity2 = this.BaseRepository().FindEntity <TSSchemeEntity>(t => t.F_IsActive == 1 && t.F_SchemeInfoId == keyValue); } var db = this.BaseRepository().BeginTrans(); try { if (!string.IsNullOrEmpty(keyValue)) { schemeInfoEntity.Modify(keyValue); db.Update(schemeInfoEntity); if (schemeEntity2 == null || schemeEntity2.F_Scheme != schemeEntity.F_Scheme) { schemeEntity.Create(); schemeEntity.F_SchemeInfoId = schemeInfoEntity.F_Id; schemeEntity.F_IsActive = 1; db.Insert(schemeEntity); if (schemeEntity2 != null) { schemeEntity2.F_IsActive = 2; db.Update(schemeEntity2); } // 关闭老的任务进程 TSProcessEntity tSProcessOldEntity = this.BaseRepository().FindEntity <TSProcessEntity>(t => t.F_SchemeInfoId == keyValue && t.F_State != 10); if (tSProcessOldEntity.F_State != 3) { if (tSProcessOldEntity.F_State == 1 || tSProcessOldEntity.F_State == 2) { tSProcessOldEntity.F_State = 10; db.Update(tSProcessOldEntity); } // 新增一个任务进程 tSProcessEntity = new TSProcessEntity() { F_State = 1, F_SchemeId = schemeEntity.F_Id, F_SchemeInfoId = schemeInfoEntity.F_Id, F_EndType = tSSchemeModel.endType, F_EndTime = tSSchemeModel.endTime }; tSProcessEntity.Create(); if (tSSchemeModel.startType == 1) { tSProcessEntity.F_BeginTime = DateTime.Now; } else { tSProcessEntity.F_BeginTime = tSSchemeModel.startTime; } if (tSSchemeModel.endType == 1) { tSProcessEntity.F_EndTime = DateTime.MaxValue; } db.Insert(tSProcessEntity); } } } else { schemeInfoEntity.Create(); db.Insert(schemeInfoEntity); schemeEntity.Create(); schemeEntity.F_SchemeInfoId = schemeInfoEntity.F_Id; schemeEntity.F_IsActive = 1; db.Insert(schemeEntity); // 新增一个任务进程 tSProcessEntity = new TSProcessEntity() { F_State = 1, F_SchemeId = schemeEntity.F_Id, F_SchemeInfoId = schemeInfoEntity.F_Id, F_EndType = tSSchemeModel.endType, F_EndTime = tSSchemeModel.endTime }; tSProcessEntity.Create(); if (tSSchemeModel.startType == 1) { tSProcessEntity.F_BeginTime = DateTime.Now; } else { tSProcessEntity.F_BeginTime = tSSchemeModel.startTime; } if (tSSchemeModel.endType == 1) { tSProcessEntity.F_EndTime = DateTime.MaxValue; } db.Insert(tSProcessEntity); } db.Commit(); return(tSProcessEntity); } catch (Exception ex) { db.Rollback(); if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } }