/// <summary> /// 添加一个节点任务 /// </summary> /// <param name="nodeID">脚本节点ID</param> /// <param name="isManual">是否手动</param> /// <param name="referenceDate">基准日期</param> /// <param name="beginDate">起始日期</param> /// <param name="endDate">结束日期</param> /// <param name="parameters">手动参数(手动任务有效)</param> /// <param name="remark">备注</param> /// <returns></returns> public int AddNodeTask(int nodeID, bool isManual, DateTime referenceDate, DateTime beginDate, DateTime endDate, string parameters, string remark) { if (nodeID < 1) { throw new Exception("节点ID不可小于1"); } BF_ST_NODE.Entity fe = BF_ST_NODE.Instance.GetEntityByKey <BF_ST_NODE.Entity>(nodeID); if (fe == null) { throw new Exception("节点ID【" + nodeID + "】不存在"); } if (fe.RUN_STATUS > (Int16)Enums.RunStatus.等待.GetHashCode() && fe.RUN_STATUS < (Int16)Enums.RunStatus.结束.GetHashCode()) { throw new Exception(string.Format("节点ID【{0}】的任务【{1}】仍未完成", nodeID, fe.LAST_TASK_ID)); } if (referenceDate.Year != endDate.Year || referenceDate.Month != endDate.Month) { throw new Exception("基准日期和截止日期必须同月"); } Dictionary <string, object> dic = new Dictionary <string, object>(); dic.Add("FLOW_ID", 0); dic.Add("NODE_ID", nodeID); dic.Add("IS_MANUAL", isManual ? 1 : 0); dic.Add("REFERENCE_DATE", referenceDate.Date); dic.Add("BEGIN_DATE", beginDate.Date); dic.Add("END_DATE", endDate.Date); dic.Add("PARAMETER", parameters); dic.Add("RETRY_TIMES", 0); dic.Add("REMARK", remark); dic.Add("CREATE_UID", isManual ? SystemSession.UserID : 0); dic.Add("UPDATE_UID", isManual ? SystemSession.UserID : 0); dic.Add("CREATE_TIME", DateTime.Now); dic.Add("UPDATE_TIME", DateTime.Now); dic.Add("RUN_STATUS", Enums.RunStatus.等待.GetHashCode()); return(Add(dic)); }
/// <summary> /// 根据任务创建任务节点 /// </summary> /// <param name="taskEntity">任务</param> /// <returns></returns> public int AddFromTask(BF_ST_TASK.Entity taskEntity) { if (taskEntity == null) { throw new Exception("任务不存在"); } DataTable dt = GetTable("TASK_ID=?", taskEntity.ID); if (dt != null && dt.Rows.Count > 0) { SetWait(taskEntity.ID); CS.Base.Log.BLog.Write(Base.Log.BLog.LogLevel.WARN, string.Format("任务【{0}】已经有【{1}】个节点实例,将不创建新的实例", taskEntity.ID, dt.Rows.Count)); return(dt.Rows.Count); } //单节点任务 if (taskEntity.NODE_ID > 0) { BF_ST_NODE.Entity ne = BF_ST_NODE.Instance.GetEntityByKey <BF_ST_NODE.Entity>(taskEntity.NODE_ID); if (ne == null) { throw new Exception(string.Format("任务【{0}】的节点【{1}】不存在", taskEntity.ID, taskEntity.NODE_ID)); } Entity entity = new Entity(); entity.TASK_ID = taskEntity.ID; entity.FLOW_ID = 0; entity.NODE_ID = taskEntity.NODE_ID; entity.CONTENT = ne.CONTENT; entity.RUN_STATUS = (Int16)Enums.RunStatus.等待.GetHashCode(); entity.RETRY_TIMES = 0; entity.REFERENCE_DATE = taskEntity.REFERENCE_DATE; entity.BEGIN_DATE = taskEntity.BEGIN_DATE; entity.END_DATE = taskEntity.END_DATE; entity.PARAMETER = taskEntity.PARAMETER; return(Add(entity)); } //脚本流任务 int i = 0; BF_ST_FLOW.Entity fe = BF_ST_FLOW.Instance.GetEntityByKey <BF_ST_FLOW.Entity>(taskEntity.FLOW_ID); if (fe == null) { throw new Exception(string.Format("任务【{0}】没有配置有效脚本", taskEntity.ID)); } IList <BF_ST_FLOW_NODE.Entity> fnList = BF_ST_FLOW_NODE.Instance.GetListByFlowID(taskEntity.FLOW_ID); if (fnList == null || fnList.Count < 1) { throw new Exception(string.Format("任务【{0}】没有配置有效节点", taskEntity.ID)); } //添加相关节点 foreach (BF_ST_FLOW_NODE.Entity fn in fnList) { BF_ST_NODE.Entity ne = BF_ST_NODE.Instance.GetEntityByKey <BF_ST_NODE.Entity>(fn.NODE_ID); if (ne == null) { throw new Exception(string.Format("任务【{0}】的相关节点【{1}】不存在", taskEntity.ID, fn.NODE_ID)); } Entity entity = new Entity(); entity.TASK_ID = taskEntity.ID; entity.FLOW_ID = taskEntity.FLOW_ID; entity.NODE_ID = fn.NODE_ID; if (string.IsNullOrWhiteSpace(fn.PRE_NODE_IDS) == false) { entity.PRE_NODE_IDS = fn.PRE_NODE_IDS.Trim(new char[] { ',', ' ' }); } entity.DIV_X = fn.DIV_X; entity.DIV_Y = fn.DIV_Y; entity.CONTENT = ne.CONTENT; entity.RUN_STATUS = (Int16)Enums.RunStatus.等待.GetHashCode(); entity.RETRY_TIMES = fe.RETRY_TIMES; entity.REFERENCE_DATE = taskEntity.REFERENCE_DATE; entity.BEGIN_DATE = taskEntity.BEGIN_DATE; entity.END_DATE = taskEntity.END_DATE; entity.PARAMETER = taskEntity.PARAMETER; i += Add(entity); } return(i); }
/// <summary> /// 强制启动任务 /// </summary> /// <param name="taskID">任务ID</param> /// <param name="message">返回信息</param> /// <returns></returns> public bool Start(int taskID, ref string message) { try { Entity te = GetEntityByKey <Entity>(taskID); if (te == null) { message = "没有找到任务【" + taskID + "】"; return(false); } if (te.RUN_STATUS == (short)Enums.RunStatus.运行) { message = "任务正处于运行中"; return(false); } if (te.RUN_STATUS == (short)Enums.RunStatus.等待) { message = "任务正处于等待运行中,请稍后再试"; return(false); } //单节点任务 if (te.NODE_ID > 0) { BF_ST_NODE.Entity ne = BF_ST_NODE.Instance.GetEntityByKey <BF_ST_NODE.Entity>(te.NODE_ID); if (ne == null) { message = "节点任务,但没有找到节点【" + te.NODE_ID + "】"; return(false); } if (ne.RUN_STATUS == (short)Enums.RunStatus.运行) { message = "节点任务,但节点【" + ne.NAME + "】正在执行任务【" + ne.LAST_TASK_ID + "】,当前任务无法启动"; return(false); } BF_ST_NODE.Instance.SetLastTaskID(te.NODE_ID, taskID); } else { BF_ST_FLOW.Entity fe = BF_ST_FLOW.Instance.GetEntityByKey <BF_ST_FLOW.Entity>(te.FLOW_ID); if (fe == null) { message = "无效任务,但没有找到脚本【" + te.FLOW_ID + "】"; return(false); } if (fe.RUN_STATUS == (short)Enums.RunStatus.运行) { message = "脚本任务,但脚本【" + fe.NAME + "】正在执行任务【" + fe.LAST_TASK_ID + "】,当前任务无法启动"; return(false); } BF_ST_FLOW.Instance.SetLastTaskID(te.NODE_ID, taskID); } Dictionary <string, object> dic = new Dictionary <string, object>(); dic.Add("RUN_STATUS", Enums.RunStatus.等待.GetHashCode()); dic.Add("UPDATE_TIME", DateTime.Now); dic.Add("START_TIME", null); dic.Add("FINISH_TIME", null); dic.Add("REMARK", "手动重启任务"); int i = Update(dic, "ID=?", taskID); if (i == 1) { message = "已重启任务,请等待执行"; return(true); } message = "启动任务失败"; } catch (Exception ex) { message = "启动任务出错,详见日志"; BLog.Write(BLog.LogLevel.WARN, "启动任务【" + taskID + "】出错:" + ex.ToString()); } return(false); }