/// <summary> /// 转换脚本 /// </summary> /// <param name="taskNodeEntity">节点实例</param> /// <param name="err">错误信息</param> /// <returns>转换后的脚本</returns> public string Trans(BF_ST_TASK_FLOW_NODE.Entity taskNodeEntity, ref ErrorInfo err) { if (taskNodeEntity == null) { return(string.Empty); } _referenceDateTime = taskNodeEntity.REFERENCE_DATE; string code = taskNodeEntity.CONTENT; string functions = string.Empty; try { //替换自定义函数 code = ReplaceFunctions('@', code); //替换参数 code = code.Replace("@BEGIN_DATE", taskNodeEntity.BEGIN_DATE.ToString("yyyy -MM-dd")); code = code.Replace("@END_DATE", taskNodeEntity.END_DATE.ToString("yyyy -MM-dd")); code = code.Replace("@PARAM", taskNodeEntity.PARAMETER); //拼凑节点执行代码块 return(GenerateCode(code, functions)); } catch (Exception ex) { err.IsError = true; err.Message = string.Format("脚本【{0}】的实例【{1}】中的节点【{2}】转换脚本,错误信息为:\r\n{3}\r\n原始脚本代码为:\r\n{4}", taskNodeEntity.FLOW_ID, taskNodeEntity.ID, taskNodeEntity.NODE_ID, ex.ToString(), code); return(string.Empty); } }
/// <summary> /// 运行脚本 /// </summary> /// <param name="code">脚本代码</param> /// <param name="taskEntity">任务实体</param> /// <param name="dbID">默认数据库ID</param> /// <param name="err">错误信息</param> /// <returns></returns> public static bool Run(string code, BF_ST_TASK_FLOW_NODE.Entity taskEntity, int dbID, ref ErrorInfo err) { //动态编译 CompilerResults cr = CompilerClass(code, ref err); if (cr == null) { //BF_ST_TASK_FLOW_NODE_LOG.Instance.Add(taskEntity.SCRIPT_ID, taskEntity.ID, Librarys.Log.BLog.LogLevel.WARN, "任务脚本编译失败:\r\n" + err.Message, code); return(false); } //BF_ST_TASK_FLOW_NODE_LOG.Instance.Add(taskEntity.SCRIPT_ID, taskEntity.ID, Librarys.Log.BLog.LogLevel.INFO, string.Format("任务【{0}】脚本编译成功", taskEntity.ID), code); //调用必备函数+执行实例代码 return(ExcuteScriptCaseCode(cr, taskEntity, dbID, ref err)); }
/// <summary> /// 列表页 /// </summary> /// <returns></returns> public ActionResult Index(int nodeId = 0, int TaskId = 0) { ViewBag.nodeId = nodeId; ViewBag.taskId = TaskId; BF_ST_TASK_FLOW_NODE.Entity entity = BF_ST_TASK_FLOW_NODE.Instance.GetEntity <BF_ST_TASK_FLOW_NODE.Entity>("TASK_ID=? AND NODE_ID=?", TaskId, nodeId); if (entity != null && string.IsNullOrWhiteSpace(entity.CODE) == false) { ViewBag.CODE = entity.CODE.Replace("<", "<").Replace(">", ">"); } else { ViewBag.CODE = "未查到代码"; } return(View()); }
/// <summary> /// 执行节点实例代码内容 /// </summary> /// <param name="cr"></param> /// <param name="taskEntity"></param> /// <param name="dbID">默认数据库ID</param> /// <param name="err"></param> public static bool ExcuteScriptCaseCode(CompilerResults cr, BF_ST_TASK_FLOW_NODE.Entity taskEntity, int dbID, ref ErrorInfo err) { // 通过反射,调用函数 Assembly objAssembly = cr.CompiledAssembly; object objScripRunner = objAssembly.CreateInstance("CS.ScriptService.Script.ScripRunner"); if (objScripRunner == null) { err.IsError = true; err.Message = "不能创建脚本的运行实例。"; return(false); } //初始化节点实例相关数据Initialize() var initialize = objScripRunner.GetType().GetMethod("Init").Invoke(objScripRunner, new object[] { taskEntity.ID, taskEntity.TASK_ID, taskEntity.FLOW_ID, taskEntity.NODE_ID }); //设置基准时间 var referenceDateTime = objScripRunner.GetType().GetMethod("SetReferenceDateTime").Invoke(objScripRunner, new object[] { taskEntity.REFERENCE_DATE }); //设置启动数据库编号 var setnowdbid = objScripRunner.GetType().GetMethod("setnowdbid").Invoke(objScripRunner, new object[] { dbID }); //运行脚本 var run = objScripRunner.GetType().GetMethod("Run").Invoke(objScripRunner, null); #region 获取错误信息GetErr() //外部job需要接收内部错误信息 //用于判断重试次数、用于是否再执行 var errorMsg = objScripRunner.GetType().GetMethod("GetErrorMessage").Invoke(objScripRunner, null); if (string.IsNullOrEmpty(errorMsg.ToString()) == false) { err.IsError = true; err.Message = errorMsg.ToString(); return(false); } #endregion return(true); }
/// <summary> /// 开始执行任务 /// </summary> /// <returns></returns> public bool Start() { try { //读取当前节点 _taskNodeEntity = BF_ST_TASK_FLOW_NODE.Instance.GetEntityByKey <BF_ST_TASK_FLOW_NODE.Entity>(_taskfnID); if (_taskNodeEntity == null) { WriteLog(_taskfnID, _taskID, _flowID, _nodeID, BLog.LogLevel.WARN, "没有获取任务节点实例对象,无法执行。"); return(false); } _taskID = _taskNodeEntity.TASK_ID; _flowID = _taskNodeEntity.FLOW_ID; _nodeID = _taskNodeEntity.NODE_ID; _retryTimes = _taskNodeEntity.RETRY_TIMES; _failTimes = _taskNodeEntity.FAIL_TIMES; //已经停止的节点,不再执行 if (_taskNodeEntity.RUN_STATUS == (short)Enums.RunStatus.结束) { WriteLog(_taskfnID, _taskID, _flowID, _nodeID, BLog.LogLevel.INFO, "节点实例运行状态为【停止】,本节点将不被执行。"); return(false); } _bw = new BackgroundWorker(); _bw.WorkerSupportsCancellation = true; _bw.DoWork += DoWork; _bw.RunWorkerAsync(); } catch (Exception ex) { WriteLog(_taskfnID, _taskID, _flowID, _nodeID, BLog.LogLevel.WARN, string.Format("启动节点实例出现了未知异常,错误信息为:\r\n{0}", ex.ToString())); return(false); } return(true); }