Exemplo n.º 1
0
        /// <summary>
        /// 更新脚本实例运行状态为“停止”
        /// </summary>
        /// <param name="scriptCaseID">脚本实例ID</param>
        /// <param name="returnCode">执行结果</param>
        /// <returns></returns>
        public int SetStop(long scriptCaseID, Enums.ReturnCode returnCode)
        {
            Dictionary <string, object> dic = new Dictionary <string, object>();

            dic.Add("RUN_STATUS", Enums.RunStatus.Stop.GetHashCode());
            dic.Add("RETURN_CODE", returnCode.GetHashCode());
            dic.Add("END_TIME", DateTime.Now);
            return(Update(dic, "ID=?", scriptCaseID));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 添加并运行节点实例
        /// </summary>
        /// <param name="scriptID">脚本ID</param>
        /// <param name="scriptCaseID">脚本实例ID</param>
        /// <param name="maxTryTimes">出错最大尝试次数</param>
        /// <param name="err">错误信息</param>
        /// <returns></returns>
        private static bool AddAndRunNode(long scriptID, long scriptCaseID, int maxTryTimes, ref ErrorInfo err)
        {
            //已添加的节点实例
            Dictionary <long, BLL.EM_SCRIPT_NODE_CASE.Entity> nodeCaseList = BLL.EM_SCRIPT_NODE_CASE.Instance.GetDictionaryByScriptCaseID(scriptCaseID);
            //已添加的节点ID
            Dictionary <long, bool> dicExecutedNodeID = new Dictionary <long, bool>();

            //脚本流运行结果
            Enums.ReturnCode returnCode = Enums.ReturnCode.Success;
            //脚本流实例是否已经完成
            bool isComplete = true;

            //运行中的节点数 2018/12/13 注释
            //int runningCount = 0;
            //启动之前未完成的节点
            #region  2018/12/13 处理(取消了对执行中的脚本判断或重启)
            foreach (var kvp in nodeCaseList)
            {
                if (kvp.Value.RUN_STATUS != (short)Enums.RunStatus.Stop)
                {
                    isComplete = false;
                    #region 2018/12/13 注释
                    //Node node = new Node(kvp.Value.ID, maxTryTimes);
                    //bool isStarted = node.Start(true);
                    //if (isStarted == true)
                    //{
                    //    WriteLog(scriptCaseID, BLog.LogLevel.INFO, string.Format("脚本流【{0}】的实例【{1}】中的节点【{2}】之前未完成的实例【{3}】已经重新启动。", scriptID, scriptCaseID, kvp.Value.SCRIPT_NODE_ID, kvp.Value.ID));
                    //    runningCount++;
                    //}
                    #endregion
                }
                else if (kvp.Value.RUN_STATUS == (short)Enums.RunStatus.Stop)
                {
                    //已经结束的节点,记录状态,只要有一个失败则为失败
                    if (kvp.Value.RETURN_CODE == (short)Enums.ReturnCode.Fail)
                    {
                        returnCode = Enums.ReturnCode.Fail;
                    }
                    else if (kvp.Value.RETURN_CODE == (short)Enums.ReturnCode.Warn)
                    {
                        returnCode = Enums.ReturnCode.Warn;
                    }
                }

                //记录已经成功完成的节点
                if (dicExecutedNodeID.ContainsKey(kvp.Value.SCRIPT_NODE_ID) == false)
                {
                    dicExecutedNodeID.Add(kvp.Value.SCRIPT_NODE_ID, (kvp.Value.RUN_STATUS == (short)Enums.RunStatus.Stop) && (kvp.Value.RETURN_CODE == (short)Enums.ReturnCode.Success));
                }
            }

            // 2018/12/13 注释
            //if (runningCount > 0)
            //{
            //    WriteLog(scriptCaseID, BLog.LogLevel.INFO, string.Format("脚本流【{0}】的实例【{1}】启动了【{2}】个之前未完成的节点实例。", scriptID, scriptCaseID, runningCount));
            //}
            #endregion

            //所有节点及依赖关系
            Dictionary <long, List <long> > dicAllNodes = BLL.EM_SCRIPT_REF_NODE_FORCASE.Instance.GetNodeAndParents(scriptCaseID);

            int addCount = 0;
            //没有失败节点的情况下,添加新节点
            if (returnCode != Enums.ReturnCode.Fail)
            {
                foreach (var kvp in dicAllNodes)
                {
                    if (dicExecutedNodeID.ContainsKey(kvp.Key) == false)
                    {
                        //无父节点或所有父节点均已经执行完成
                        bool isNeedAdd = kvp.Value.Count < 1;
                        if (kvp.Value.Count > 0)
                        {
                            isNeedAdd = true;
                            foreach (long pNodeID in kvp.Value)
                            {
                                if (dicExecutedNodeID.ContainsKey(pNodeID) == false)
                                {
                                    isNeedAdd = false;
                                    break;
                                }
                                if (dicExecutedNodeID[pNodeID] == false)
                                {
                                    isNeedAdd = false;
                                    break;
                                }
                            }
                        }

                        //需要添加节点
                        if (isNeedAdd == true)
                        {
                            isComplete = false;
                            long nodeCaseID = BLL.EM_SCRIPT_NODE_CASE.Instance.AddReturnCaseID(scriptID, scriptCaseID, kvp.Key);
                            if (nodeCaseID > 0)
                            {
                                WriteLog(scriptCaseID, BLog.LogLevel.INFO, string.Format("脚本流【{0}】的实例【{1}】中的节点【{2}】成功添加节点实例【{3}】。", scriptID, scriptCaseID, kvp.Key, nodeCaseID));
                                //启动节点
                                Node node = new Node(nodeCaseID, maxTryTimes);

                                bool isStarted = node.Start();
                                if (isStarted == true)
                                {
                                    addCount++;
                                }
                            }
                            else
                            {
                                err.IsError = true;
                                err.Message = string.Format("脚本流【{0}】的实例【{1}】中的节点【{2}】添加节点实例【{3}】失败。", scriptID, scriptCaseID, kvp.Key, nodeCaseID);
                                return(false);
                            }
                        }
                    }
                }
                if (addCount > 0)
                {
                    WriteLog(scriptCaseID, BLog.LogLevel.INFO, string.Format("脚本流【{0}】的实例【{1}】添加了【{2}】个新的节点实例准备执行。", scriptID, scriptCaseID, addCount));
                }
            }

            //完成或者有失败节点时停止脚本流
            if (isComplete || returnCode == Enums.ReturnCode.Fail)
            {
                var sc = BLL.EM_SCRIPT_CASE.Instance.GetCase(scriptCaseID);
                BLL.EM_SCRIPT_CASE.Instance.SetStop(scriptCaseID, returnCode);
                if (sc != null && sc.IS_SUPERVENE.Value == 1)
                {
                    lock (dicAllNodes)
                    {
                        if (Main.CurUploadCount > 0)
                        {
                            Main.CurUploadCount--;
                        }
                        WriteLog(0, BLog.LogLevel.DEBUG, string.Format("完成一个并行任务,删除后当前MaxUploadCount{0},CurUploadCount{1}。", Main.MaxUploadCount.ToString(), Main.CurUploadCount));
                    }
                }
                else
                {
                    lock (dicAllNodes)
                    {
                        if (Main.CurMonitCount > 0)
                        {
                            Main.CurMonitCount--;
                        }
                        WriteLog(0, BLog.LogLevel.DEBUG, string.Format("完成一个监控任务,删除后当前CurMonitCount{0}", Main.CurMonitCount.ToString()));
                    }
                }

                WriteLog(scriptCaseID, BLog.LogLevel.DEBUG, string.Format("脚本流【{0}】的实例【{1}】所有节点已经执行完成,执行结果【{2}】。", scriptID, scriptCaseID, returnCode.ToString()));
            }
            return(true);
        }